├── .gems ├── makefile ├── garfio.gemspec ├── LICENSE ├── README.md ├── lib └── garfio.rb └── test └── test_garfio.rb /.gems: -------------------------------------------------------------------------------- 1 | cutest -v 1.1.3 -------------------------------------------------------------------------------- /makefile: -------------------------------------------------------------------------------- 1 | .PHONY: test 2 | 3 | test: 4 | cutest test/*.rb -------------------------------------------------------------------------------- /garfio.gemspec: -------------------------------------------------------------------------------- 1 | require "./lib/garfio" 2 | 3 | Gem::Specification.new do |s| 4 | s.name = "garfio" 5 | s.version = Garfio::VERSION 6 | s.summary = "Garfio helps you to build hooks in your ruby objects" 7 | s.description = "With few lines of code, one method compilation and no method missing Garfio with an easy way lets you to launch callbacks before and after of the methods of your objects." 8 | s.authors = ["Julio Lopez"] 9 | s.email = ["ljuliom@gmail.com"] 10 | s.homepage = "http://github.com/TheBlasfem/garfio" 11 | s.files = Dir[ 12 | "LICENSE", 13 | "README.md", 14 | "lib/**/*.rb", 15 | "*.gemspec", 16 | "test/**/*.rb" 17 | ] 18 | s.license = "MIT" 19 | s.add_development_dependency "cutest", "1.1.3" 20 | end -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Julio Lopez 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Garfio 2 | ==== 3 | 4 | Garfio helps you to build hooks in your ruby objects, is minimalist and framework-agnostic. 5 | 6 | ## Introduction 7 | 8 | With few lines of code, one method compilation, no monkey patching and no `method missing` Garfio lets you launch callbacks before and after of your methods as soon as you run them. 9 | 10 | ## Installation 11 | 12 | Installing Garfio is as simple as running: 13 | 14 | ``` 15 | $ gem install garfio 16 | ``` 17 | 18 | Include Garfio in your Gemfile with gem 'garfio' or require it with require 'garfio'. 19 | 20 | Usage 21 | ----- 22 | 23 | Once you have extend Garfio, you can define your before and after callbacks in the `set_hook` method, your callback recognize the method sending his name as symbol 24 | 25 | ```ruby 26 | class User 27 | extend Garfio 28 | 29 | def send_greeting 30 | puts "preparing the welcome message" 31 | end 32 | 33 | def register_user 34 | puts "registering user" 35 | end 36 | 37 | def send_mailer 38 | puts "sending to your email" 39 | end 40 | 41 | set_hook :register_user do 42 | before :send_greeting 43 | after :send_mailer 44 | end 45 | end 46 | 47 | User.new.register_user 48 | #=> preparing the welcome message 49 | #=> registering user 50 | #=> sending to your email 51 | ``` 52 | 53 | Is not neccesary indicate both callbacks, you can work with just before or after 54 | 55 | ```ruby 56 | set_hook :register_user do 57 | before :send_greeting 58 | end 59 | ``` 60 | 61 | Also you can send blocks to your callbacks 62 | ```ruby 63 | set_hook :register_user do 64 | before { puts "hello user!" } 65 | after { puts "Ok, bye" } 66 | end 67 | 68 | User.new.register_user 69 | #=> hello user! 70 | #=> registering user 71 | #=> Ok, bye 72 | ``` 73 | 74 | And you can take advantage of them to do more complex stuff 75 | 76 | ```ruby 77 | set_hook :register_user do 78 | after do 79 | update_status_with("activate") 80 | send_mailer 81 | notify_friends 82 | end 83 | end 84 | ``` -------------------------------------------------------------------------------- /lib/garfio.rb: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2015 Julio Lopez 2 | 3 | # Permission is hereby granted, free of charge, to any person obtaining a copy 4 | # of this software and associated documentation files (the "Software"), to deal 5 | # in the Software without restriction, including without limitation the rights 6 | # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | # copies of the Software, and to permit persons to whom the Software is 8 | # furnished to do so, subject to the following conditions: 9 | 10 | # The above copyright notice and this permission notice shall be included in all 11 | # copies or substantial portions of the Software. 12 | 13 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 19 | # SOFTWARE. 20 | module Garfio 21 | VERSION = "1.0.0" 22 | 23 | class GarfioHooks 24 | attr_reader :hook_before, :hook_after 25 | def initialize(&block) 26 | instance_eval &block 27 | end 28 | 29 | private 30 | [:before, :after].each do |name| 31 | define_method name do |method_name = nil, &block| 32 | instance_variable_set("@hook_#{name}", method_name || block) 33 | end 34 | end 35 | end 36 | 37 | def set_hook(original_method, _v = "", &block) 38 | gar = GarfioHooks.new &block 39 | send :alias_method, "old_#{original_method}", original_method 40 | send :define_method, original_method do |*args| 41 | _v.is_a?(Proc) ? instance_eval(&_v) : send(_v) if _v = gar.hook_before 42 | return_value = send "old_#{original_method}", *args 43 | _v.is_a?(Proc) ? instance_eval(&_v) : send(_v) if _v = gar.hook_after 44 | return_value 45 | end 46 | end 47 | end -------------------------------------------------------------------------------- /test/test_garfio.rb: -------------------------------------------------------------------------------- 1 | require File.expand_path("../lib/garfio", File.dirname(__FILE__)) 2 | 3 | class User 4 | extend Garfio 5 | 6 | def initialize 7 | @@sum = 0 8 | end 9 | 10 | def send_greeting 11 | @@sum += 1 12 | end 13 | 14 | def register_user(n = 2) 15 | @@sum += n 16 | end 17 | 18 | def send_mail 19 | @@sum += 3 20 | end 21 | 22 | def some_method(n = 4) 23 | @@sum += n 24 | end 25 | 26 | def some_other_method(n = 5) 27 | @@sum += n 28 | end 29 | 30 | def get_sum 31 | @@sum 32 | end 33 | end 34 | 35 | scope do 36 | test "hook before" do 37 | u = Class.new(User) do 38 | set_hook :register_user do 39 | before :send_greeting 40 | end 41 | end 42 | u_instance = u.new 43 | u_instance.register_user 44 | 45 | assert_equal 3, u_instance.get_sum 46 | end 47 | 48 | test "hook after" do 49 | u = Class.new(User) do 50 | set_hook :register_user do 51 | after :send_mail 52 | end 53 | end 54 | u_instance = u.new 55 | u_instance.register_user 56 | 57 | assert_equal 5, u_instance.get_sum 58 | end 59 | 60 | test "hook before and after" do 61 | u = Class.new(User) do 62 | set_hook :register_user do 63 | before :send_greeting 64 | after :send_mail 65 | end 66 | end 67 | u_instance = u.new 68 | u_instance.register_user 69 | 70 | assert_equal 6, u_instance.get_sum 71 | end 72 | 73 | test "respecting the sending variable" do 74 | u = Class.new(User) do 75 | set_hook :register_user do 76 | before :send_greeting 77 | after :send_mail 78 | end 79 | end 80 | u_instance = u.new 81 | u_instance.register_user(10) 82 | 83 | assert_equal 14, u_instance.get_sum 84 | end 85 | 86 | test "sending a block in before" do 87 | u = Class.new(User) do 88 | set_hook :register_user do 89 | before { some_method } 90 | end 91 | end 92 | u_instance = u.new 93 | u_instance.register_user 94 | 95 | assert_equal 6, u_instance.get_sum 96 | end 97 | 98 | test "sending a complex block in before" do 99 | u = Class.new(User) do 100 | set_hook :register_user do 101 | before do 102 | some_method 5 103 | some_other_method 8 104 | end 105 | end 106 | end 107 | u_instance = u.new 108 | u_instance.register_user 109 | 110 | assert_equal 15, u_instance.get_sum 111 | end 112 | 113 | test "sending a block in after" do 114 | u = Class.new(User) do 115 | set_hook :register_user do 116 | after { some_method } 117 | end 118 | end 119 | u_instance = u.new 120 | u_instance.register_user 121 | 122 | assert_equal 6, u_instance.get_sum 123 | end 124 | 125 | test "sending a complex block in after" do 126 | u = Class.new(User) do 127 | set_hook :register_user do 128 | after do 129 | some_method 5 130 | some_other_method 8 131 | end 132 | end 133 | end 134 | u_instance = u.new 135 | u_instance.register_user 136 | 137 | assert_equal 15, u_instance.get_sum 138 | end 139 | 140 | test "hook method return a value" do 141 | u = Class.new(User) do 142 | set_hook :register_user do 143 | after do 144 | some_method 5 145 | some_other_method 8 146 | end 147 | end 148 | end 149 | u_instance = u.new 150 | assert_equal 2, u_instance.register_user 151 | end 152 | end --------------------------------------------------------------------------------