Class: ComposableStateMachine::Model
- Inherits:
-
Object
- Object
- ComposableStateMachine::Model
- Defined in:
- lib/composable_state_machine/model.rb
Overview
An immutable state machine model that can be shared across many instances.
Instance Attribute Summary collapse
-
#callback_runner ⇒ Object
readonly
Returns the value of attribute callback_runner.
-
#initial_state ⇒ Object
readonly
Returns the value of attribute initial_state.
Instance Method Summary collapse
-
#initialize(options = {}) ⇒ Model
constructor
Creates a state machine model.
-
#run_callbacks(callback_runner, current_state, event, new_state, arguments, &block) ⇒ Object
Runs the callbacks for all behaviors for a state transition.
-
#run_callbacks_for(callback_runner, behavior, *args) ⇒ Object
Runs the callbacks for one behavior for a start transition.
-
#transition(current_state, event, arguments = [], callback_runner = nil) {|Object| ... } ⇒ Object?
Performs a transition of the machine, executing behaviors as needed.
Constructor Details
#initialize(options = {}) ⇒ Model
Creates a state machine model.
15 16 17 18 19 20 |
# File 'lib/composable_state_machine/model.rb', line 15 def initialize( = {}) @initial_state = [:initial_state] @transitions = [:transitions] @behaviors = [:behaviors] || proc {} @callback_runner = [:callback_runner] || DefaultCallbackRunner end |
Instance Attribute Details
#callback_runner ⇒ Object (readonly)
Returns the value of attribute callback_runner.
6 7 8 |
# File 'lib/composable_state_machine/model.rb', line 6 def callback_runner @callback_runner end |
#initial_state ⇒ Object (readonly)
Returns the value of attribute initial_state.
5 6 7 |
# File 'lib/composable_state_machine/model.rb', line 5 def initial_state @initial_state end |
Instance Method Details
#run_callbacks(callback_runner, current_state, event, new_state, arguments, &block) ⇒ Object
Runs the callbacks for all behaviors for a state transition
44 45 46 47 |
# File 'lib/composable_state_machine/model.rb', line 44 def run_callbacks(callback_runner, current_state, event, new_state, arguments, &block) run_callbacks_for(callback_runner, :enter, new_state, current_state, event, new_state, *arguments) end |
#run_callbacks_for(callback_runner, behavior, *args) ⇒ Object
Runs the callbacks for one behavior for a start transition
50 51 52 |
# File 'lib/composable_state_machine/model.rb', line 50 def run_callbacks_for(callback_runner, behavior, *args) @behaviors.call(callback_runner, behavior, *args) end |
#transition(current_state, event, arguments = [], callback_runner = nil) {|Object| ... } ⇒ Object?
Performs a transition of the machine, executing behaviors as needed.
33 34 35 36 37 38 39 40 41 |
# File 'lib/composable_state_machine/model.rb', line 33 def transition(current_state, event, arguments = [], callback_runner = nil) @transitions.transition(current_state, event).tap do |new_state| if new_state && new_state != current_state callback_runner ||= @callback_runner run_callbacks(callback_runner, current_state, event, new_state, arguments) yield new_state if block_given? end end end |