Class: ComposableStateMachine::MachineWithExternalState
- Inherits:
-
Object
- Object
- ComposableStateMachine::MachineWithExternalState
- Defined in:
- lib/composable_state_machine/machine_with_external_state.rb
Overview
State machine instance that manages its state via a reader and writer objects.
Direct Known Subclasses
Instance Method Summary collapse
-
#==(other_state) ⇒ TrueClass, FalseClass
Checks whether the state of the machine is equal to another state.
-
#initialize(model, state_reader, state_writer, options = {}) ⇒ MachineWithExternalState
constructor
Creates an instance.
-
#trigger(event, *args) ⇒ Object?
Executes the transition and behaviors associated with an event.
Constructor Details
#initialize(model, state_reader, state_writer, options = {}) ⇒ MachineWithExternalState
Creates an instance.
13 14 15 16 17 18 19 20 |
# File 'lib/composable_state_machine/machine_with_external_state.rb', line 13 def initialize(model, state_reader, state_writer, = {}) @model = model @state_reader = state_reader @state_writer = state_writer @callback_runner = [:callback_runner] || model.callback_runner initial_state = [:state] || model.initial_state @state_writer.call(initial_state) end |
Instance Method Details
#==(other_state) ⇒ TrueClass, FalseClass
Checks whether the state of the machine is equal to another state
36 37 38 |
# File 'lib/composable_state_machine/machine_with_external_state.rb', line 36 def ==(other_state) @state_reader.call == other_state end |
#trigger(event, *args) ⇒ Object?
Executes the transition and behaviors associated with an event.
28 29 30 31 |
# File 'lib/composable_state_machine/machine_with_external_state.rb', line 28 def trigger(event, *args) current_state = @state_reader.call @model.transition(current_state, event, args, @callback_runner, &@state_writer) end |