Class: Philiprehberger::StateMachine::CallbackSet
- Inherits:
-
Object
- Object
- Philiprehberger::StateMachine::CallbackSet
- Defined in:
- lib/philiprehberger/state_machine/callbacks.rb
Overview
Stores and filters callbacks for state transitions.
Instance Method Summary collapse
-
#add(type:, conditions: {}, &block) ⇒ Object
Register a callback.
-
#execute(type:, from:, to:, context:, payload: {}) ⇒ Object
Execute all matching callbacks for the given transition.
-
#initialize ⇒ CallbackSet
constructor
A new instance of CallbackSet.
Constructor Details
#initialize ⇒ CallbackSet
Returns a new instance of CallbackSet.
14 15 16 |
# File 'lib/philiprehberger/state_machine/callbacks.rb', line 14 def initialize @callbacks = [] end |
Instance Method Details
#add(type:, conditions: {}, &block) ⇒ Object
Register a callback.
23 24 25 |
# File 'lib/philiprehberger/state_machine/callbacks.rb', line 23 def add(type:, conditions: {}, &block) @callbacks << Callback.new(type: type, block: block, conditions: conditions) end |
#execute(type:, from:, to:, context:, payload: {}) ⇒ Object
Execute all matching callbacks for the given transition.
34 35 36 37 38 39 40 41 42 |
# File 'lib/philiprehberger/state_machine/callbacks.rb', line 34 def execute(type:, from:, to:, context:, payload: {}) matching(type: type, from: from, to: to).each do |callback| if [0, 1].include?(callback.block.arity) callback.block.call(context) else callback.block.call(context, payload) end end end |