module Warren::Helpers::StateMachine
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:¶ ↑
@example Basic usage
class Machine extend Warren::Helpers::StateMachine states :started, :started end machine = Machine.new machine.started! machine.started? # => true machine.stopped? # => false machine.stopped! machine.started? # => false machine.stopped? # => stopped
Public Instance Methods
state(state_name)
click to toggle source
Define a new state, generates two methods `{state}!` to transition into the state and `{state}?` to query if we are in the state.
@param state_name [Symbol, String] The name of the state
@return [Void]
# 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
states(*state_names)
click to toggle source
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.
@overload push2(state_name, …)
@param [Symbol, String] state_name The name of the state @param [Symbol, String] ... More states
@return [Void]
# File lib/warren/helpers/state_machine.rb, line 50 def states(*state_names) state_names.each { |state_name| state(state_name) } end