Module: Callbacks::ClassMethods

Defined in:
lib/classmethods.rb

Instance Method Summary collapse

Instance Method Details

#add_callback_action(type, method, *code, &block) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/classmethods.rb', line 50

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_callback_methods(method) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/classmethods.rb', line 33

def build_callback_methods(method)
  build_before_callback_method(method)
  build_after_callback_method(method)
  
  class_eval "    def \#{method}_with_callbacks\n      trigger_callback_actions(:\#{method}, :before)\n      retval = self.send(\"\#{method}_without_callbacks\")\n      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_actionsObject



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_methodsObject



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.meta_eval 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