Module: Capricorn::Actor::Actions::ClassMethods

Defined in:
lib/capricorn/actor/actions.rb

Instance Method Summary collapse

Instance Method Details

#action(*names) ⇒ Object

define one or more actions.



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/capricorn/actor/actions.rb', line 54

def action(*names)
  names.each do |name|
    module_eval %{
      def self.before_#{name}(meth)
        before(:#{name}, meth)
      end
      def self.on_#{name}(meth)
        on(:#{name}, meth)
      end
      def self.after_#{name}(meth)
        after(:#{name}, meth)
      end
      def run_#{name}_callbacks!
        run_callbacks!(:#{name})
      end
    }
  end
end

#after(action, meth) ⇒ Object

register a callback to be run after the action.



38
39
40
# File 'lib/capricorn/actor/actions.rb', line 38

def after(action, meth)
  methods_for(action, :after).push(meth.to_sym)
end

#before(action, meth) ⇒ Object

register a callback to be run before the action.



28
29
30
# File 'lib/capricorn/actor/actions.rb', line 28

def before(action, meth)
  methods_for(action, :before).push(meth.to_sym)
end

#methods_for(action, fase) ⇒ Object

get the registered callbacks for the action and face.



43
44
45
46
47
48
49
50
51
# File 'lib/capricorn/actor/actions.rb', line 43

def methods_for(action, fase)
  action = action.to_sym
  fase   = fase.to_sym
  
  @actions ||= {}
  @actions[action] ||= {}
  @actions[action][fase] ||= []
  @actions[action][fase]
end

#on(action, meth) ⇒ Object

register a callback to be run on the action.



33
34
35
# File 'lib/capricorn/actor/actions.rb', line 33

def on(action, meth)
  methods_for(action, :on).push(meth.to_sym)
end