Class: ComposableStateMachine::Behaviors
- Inherits:
-
Object
- Object
- ComposableStateMachine::Behaviors
- Defined in:
- lib/composable_state_machine/behaviors.rb
Overview
Defines the behaviors of a state machine.
Instance Method Summary collapse
-
#call(runner, behavior, *args) ⇒ self
Runs callbacks for a behavior with a runner.
-
#initialize(behaviors = {}, callbacks_factory = Callbacks) ⇒ Behaviors
constructor
Creates a Behaviors object.
-
#on(behavior, *args, &block) ⇒ self
Adds callbacks for a behavior.
Constructor Details
#initialize(behaviors = {}, callbacks_factory = Callbacks) ⇒ Behaviors
Creates a ComposableStateMachine::Behaviors object.
9 10 11 12 13 14 15 16 |
# File 'lib/composable_state_machine/behaviors.rb', line 9 def initialize(behaviors = {}, callbacks_factory = Callbacks) @callbacks_factory = callbacks_factory @behaviors = behaviors.reduce({}) do |memo, (behavior, value)| handler = value.respond_to?(:call) ? value : @callbacks_factory.new(value) memo[behavior] = handler memo end end |
Instance Method Details
#call(runner, behavior, *args) ⇒ self
Runs callbacks for a behavior with a runner.
Selects the callback manager for the behavior and forwards to its #call method.
40 41 42 43 44 45 |
# File 'lib/composable_state_machine/behaviors.rb', line 40 def call(runner, behavior, *args) @behaviors[behavior].tap do |handler| handler.call(runner, *args) if handler end self end |
#on(behavior, *args, &block) ⇒ self
Adds callbacks for a behavior.
Selects the callback manager for the behavior and forwards to its #on method.
26 27 28 29 |
# File 'lib/composable_state_machine/behaviors.rb', line 26 def on(behavior, *args, &block) (@behaviors[behavior] ||= @callbacks_factory.new).on(*args, &block) self end |