Class: StateMachine::State::TransitionDefinitionDSL
- Inherits:
-
Object
- Object
- StateMachine::State::TransitionDefinitionDSL
- Defined in:
- lib/motion-state-machine/state.rb
Instance Method Summary collapse
-
#die(options) ⇒ Array<StateMachine::Transition>
Defines a transition to a terminating state when a specified event happens.
-
#initialize(source_state, state_machine) {|state| ... } ⇒ StateMachine::State::TransitionDefinitionDSL
constructor
private
Initializes a new object that provides methods for configuring state transitions.
-
#on_entry(&block) ⇒ Object
Defines a block that will be called without parameters when the source state is entered.
-
#on_exit(&block) ⇒ Object
Defines a block that will be called without parameters when the source state is exited.
-
#transition_to(destination_state_symbol, options) ⇒ Array<StateMachine::Transition>
Creates transitions to another state when defined events happen.
Constructor Details
#initialize(source_state, state_machine) {|state| ... } ⇒ StateMachine::State::TransitionDefinitionDSL
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Initializes a new object that provides methods for configuring state transitions.
See Base#when for an explanation how to use the DSL.
124 125 126 127 128 |
# File 'lib/motion-state-machine/state.rb', line 124 def initialize(source_state, state_machine, &block) @state_machine = state_machine @state = source_state yield(self) end |
Instance Method Details
#die(options) ⇒ Array<StateMachine::Transition>
Defines a transition to a terminating state when a specified event happens. Works analog to #transition_to, but creates a terminating destination state automatically.
230 231 232 233 234 235 236 237 238 239 240 241 242 243 |
# File 'lib/motion-state-machine/state.rb', line 230 def die() termination_states = @state_machine.states.select(&:terminating?) symbol = "terminated_#{termination_states.count}".to_sym termination_state = @state_machine.state symbol termination_state.terminating = true transitions = transition_to(symbol, ) event_texts = transitions.collect(&:event_log_text).join(" or ") termination_state.name = "terminated (internal state) #{event_texts}" transitions end |
#on_entry(&block) ⇒ Object
Defines a block that will be called without parameters when the source state is entered.
251 252 253 |
# File 'lib/motion-state-machine/state.rb', line 251 def on_entry(&block) @state.entry_actions << block end |
#on_exit(&block) ⇒ Object
Defines a block that will be called without parameters when the source state is exited.
261 262 263 |
# File 'lib/motion-state-machine/state.rb', line 261 def on_exit(&block) @state.exit_actions << block end |
#transition_to(destination_state_symbol, options) ⇒ Array<StateMachine::Transition>
Creates transitions to another state when defined events happen.
If multiple trigger events are defined, any of them will create its own Transition object.
You can specify guard blocks that can prevent a transition from happening.
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 |
# File 'lib/motion-state-machine/state.rb', line 183 def transition_to(destination_state_symbol, ) unless destination_state_symbol.is_a? Symbol raise ArgumentError, "No destination state given "\ "(got #{destination_state_symbol.inspect})" end .merge! from: @state.symbol, to: destination_state_symbol defined_event_types = Transition.types.select do |type| ![type].nil? end if defined_event_types.empty? raise ArgumentError, "No trigger event found in #{}. "\ "Valid trigger event keys: #{Transition.types}." end transitions = [] defined_event_types.each do |type| event_trigger_value = [type] if event_trigger_value .merge! state_machine: @state_machine, type: type transition = Transition.make @state.register(transition) transitions << transition end end transitions end |