Class: BigSpoon::Hook
- Inherits:
-
Object
- Object
- BigSpoon::Hook
- Defined in:
- lib/big_spoon/hook.rb
Instance Attribute Summary collapse
-
#klass ⇒ Object
Returns the value of attribute klass.
Class Method Summary collapse
Instance Method Summary collapse
-
#after(method_to_hook, method_to_call = nil, options = {}, &block) ⇒ Object
Define a method to execute after another.
-
#before(method_to_hook, method_to_call = nil, options = {}, &block) ⇒ Object
Define a method to execute before another.
-
#execute_after(method_to_hook, instance) ⇒ Object
Execute after a method.
-
#execute_before(method_to_hook, instance) ⇒ Object
Execute before a method.
-
#hook!(method_to_hook) ⇒ Object
Alias a method for hookable-ness.
-
#initialize(klass) ⇒ Hook
constructor
A new instance of Hook.
- #should_hook?(method_to_hook) ⇒ Boolean
Constructor Details
#initialize(klass) ⇒ Hook
Returns a new instance of Hook.
60 61 62 |
# File 'lib/big_spoon/hook.rb', line 60 def initialize(klass) self.klass = klass end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *args) ⇒ Object (private)
139 140 141 142 143 144 145 146 147 148 |
# File 'lib/big_spoon/hook.rb', line 139 def method_missing(method_name, *args) case method_name.to_s when /^after_(.+)$/ after $1, *args when /^before_(.+)$/ before $1, *args else super end end |
Instance Attribute Details
#klass ⇒ Object
Returns the value of attribute klass.
14 15 16 |
# File 'lib/big_spoon/hook.rb', line 14 def klass @klass end |
Class Method Details
.for(klass) ⇒ Object
4 5 6 |
# File 'lib/big_spoon/hook.rb', line 4 def for(klass) all[klass.to_s] ||= new(klass) end |
Instance Method Details
#after(method_to_hook, method_to_call = nil, options = {}, &block) ⇒ Object
Define a method to execute after another
17 18 19 |
# File 'lib/big_spoon/hook.rb', line 17 def after(method_to_hook, method_to_call = nil, = {}, &block) add(:after, method_to_hook, method_to_call, , &block) end |
#before(method_to_hook, method_to_call = nil, options = {}, &block) ⇒ Object
Define a method to execute before another
22 23 24 |
# File 'lib/big_spoon/hook.rb', line 22 def before(method_to_hook, method_to_call = nil, = {}, &block) add(:before, method_to_hook, method_to_call, , &block) end |
#execute_after(method_to_hook, instance) ⇒ Object
Execute after a method
34 35 36 |
# File 'lib/big_spoon/hook.rb', line 34 def execute_after(method_to_hook, instance) execute(:after, method_to_hook, instance) end |
#execute_before(method_to_hook, instance) ⇒ Object
Execute before a method
39 40 41 |
# File 'lib/big_spoon/hook.rb', line 39 def execute_before(method_to_hook, instance) execute(:before, method_to_hook, instance) end |
#hook!(method_to_hook) ⇒ Object
Alias a method for hookable-ness
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/big_spoon/hook.rb', line 44 def hook!(method_to_hook) hooked_method = hooked_method(method_to_hook) original_method = original_method(method_to_hook) line = __LINE__; alias_these_hooks = <<-hooks alias :#{original_method} :#{method_to_hook} def #{hooked_method}(*args) Hook.for(self.class).execute_before(:#{method_to_hook}, self) result = #{original_method}(*args) Hook.for(self.class).execute_after(:#{method_to_hook}, self) result end alias :#{method_to_hook} :#{hooked_method} hooks klass.class_eval alias_these_hooks, __FILE__, line.succ end |
#should_hook?(method_to_hook) ⇒ Boolean
64 65 66 |
# File 'lib/big_spoon/hook.rb', line 64 def should_hook?(method_to_hook) methods[method_to_hook.to_sym] && hookable?(method_to_hook) && !hooked?(method_to_hook) end |