Module: Warren::Helpers::StateMachine
Overview
Provides an incredibly simple state machine. It merely lets you define
states with #state which defines two methods {state}! to transition
into the state and {state}? to query if we are in the state.
Usage:
Instance Method Summary collapse
-
#state(state_name) ⇒ Void
Define a new state, generates two methods
{state}!to transition into the state and{state}?to query if we are in the state. -
#push2(state_name, ...) ⇒ Void
Define new states, generates two methods for each state
{state}!to transition into the state and{state}?to query if we are in the state.
Instance Method Details
#state(state_name) ⇒ Void
Define a new state, generates two methods {state}! to transition
into the state and {state}? to query if we are in the state.
35 36 37 38 |
# File 'lib/warren/helpers/state_machine.rb', line 35 def state(state_name) define_method(:"#{state_name}!") { @state = state_name } define_method(:"#{state_name}?") { @state == state_name } end |
#push2(state_name, ...) ⇒ Void
Define new states, generates two methods for each state {state}! to
transition into the state and {state}? to query if we are in the state.
50 51 52 |
# File 'lib/warren/helpers/state_machine.rb', line 50 def states(*state_names) state_names.each { |state_name| state(state_name) } end |