Module: Callbacks::ClassMethods
- Defined in:
- lib/classmethods.rb
Instance Method Summary collapse
- #add_callback_action(type, method, *code, &block) ⇒ Object
- #add_callback_methods(*callbacks) ⇒ Object
- #build_after_callback_method(method) ⇒ Object
- #build_before_callback_method(method) ⇒ Object
- #build_callback_method(type, method) ⇒ Object
- #build_callback_methods(method) ⇒ Object
- #callback_actions ⇒ Object
-
#callback_actions_for(method, type) ⇒ Object
TODO: make this nicer.
- #callback_methods ⇒ Object
- #remove_callback_method(method) ⇒ Object
Instance Method Details
#add_callback_action(type, method, *code, &block) ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/classmethods.rb', line 73 def add_callback_action(type, method, *code, &block) ca = self.callback_actions ca[method] = {} if ca[method].nil? ca[method][type] = [] if ca[method][type].nil? if block_given? callback = Callback.new(method, nil, &block) else code.each do |c| callback = Callback.new(method, c) end end ca[method][type] << callback return callback end |
#add_callback_methods(*callbacks) ⇒ Object
14 15 16 17 18 19 |
# File 'lib/classmethods.rb', line 14 def add_callback_methods(*callbacks) callbacks.each do |callback| self.callback_methods << callback.to_sym self.build_callback_methods(callback) end end |
#build_after_callback_method(method) ⇒ Object
94 95 96 |
# File 'lib/classmethods.rb', line 94 def build_after_callback_method(method) build_callback_method(:after, method) end |
#build_before_callback_method(method) ⇒ Object
90 91 92 |
# File 'lib/classmethods.rb', line 90 def build_before_callback_method(method) build_callback_method(:before, method) end |
#build_callback_method(type, method) ⇒ Object
98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/classmethods.rb', line 98 def build_callback_method(type, method) method = "\n# def \#{type}_\#{method}(*callbacks, &block)\n# self.class.add_callback_action(:\#{type}, :\#{method}, *callbacks, &block)\n# end\n\n def self.\#{type}_\#{method}(*callbacks, &block)\n add_callback_action(:\#{type}, :\#{method}, *callbacks, &block)\n end\n\n end_eval\n module_eval(method)\nend\n" |
#build_callback_methods(method) ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/classmethods.rb', line 56 def build_callback_methods(method) build_before_callback_method(method) build_after_callback_method(method) class_eval " def \#{method}_with_callbacks\n self.trigger_callback_actions(:\#{method}, :before) \n retval = self.send(\"\#{method}_without_callbacks\")\n self.trigger_callback_actions(:\#{method}, :after)\n return retval\n end\n end_eval\n \n send :alias_method, \"\#{method}_without_callbacks\", method\n send :alias_method, method, \"\#{method}_with_callbacks\"\nend\n" |
#callback_actions ⇒ Object
9 10 11 12 |
# File 'lib/classmethods.rb', line 9 def callback_actions #Use one @, else it gets stored in the module and then it will be avaiable in every class that uses callback @callback_actions ||= CallbackChain.new end |
#callback_actions_for(method, type) ⇒ Object
TODO: make this nicer
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/classmethods.rb', line 34 def callback_actions_for(method, type) if self.callback_actions[method].nil? callback_actions = [] else callback_actions = self.callback_actions[method][type] ||= [] end #Removed this because this didn't work #callback_actions_for is called four times with every callback method: # before, before metaclass, after, after metaclass # And a class and metaclass both now the #{type}_#{method} method # So it got called twice # if self.instance_methods.include?("#{type}_#{method}") # if not callback_actions.include? "#{type}_#{method}".to_sym # callback = self.add_callback_action(type, method, "#{type}_#{method}".to_sym) # callback_actions << callback # end # end return callback_actions end |
#callback_methods ⇒ Object
4 5 6 7 |
# File 'lib/classmethods.rb', line 4 def callback_methods #Use one @, else it gets stored in the module and then it will be avaiable in every class that uses callback @callback_methods ||= [] end |
#remove_callback_method(method) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/classmethods.rb', line 21 def remove_callback_method(method) self. do remove_method("before_#{method}") remove_method("after_#{method}") end remove_method("#{method}_with_callbacks") remove_method(method) #This is the method_with_callbacks alias_method(method, "#{method}_without_callbacks") self.callback_methods.delete(method.to_sym) end |