Class | StateMachine::State |
In: |
lib/state_machine/state.rb
|
Parent: | Object |
A state defines a value that an attribute can be in after being transitioned 0 or more times. States can represent a value of any type in Ruby, though the most common (and default) type is String.
In addition to defining the machine‘s value, a state can also define a behavioral context for an object when that object is in the state. See StateMachine::Machine#state for more information about how state-driven behavior can be utilized.
initial | -> | initial? |
cache | [RW] | Whether this state‘s value should be cached after being evaluated |
human_name | [W] | The human-readable name for the state |
initial | [RW] | Whether or not this state is the initial state to use for new objects |
machine | [RW] | The state machine for which this state is defined |
matcher | [RW] | A custom lambda block for determining whether a given value matches this state |
methods | [R] |
Tracks all of the methods that have been defined for the machine‘s
owner class when objects are in this state.
Maps :method_name => UnboundMethod |
name | [R] | The unique identifier for the state used in event and callback definitions |
qualified_name | [R] | The fully-qualified identifier for the state, scoped by the machine‘s namespace |
value | [W] | The value that is written to a machine‘s attribute when an object transitions into this state |
Calls a method defined in this state‘s context on the given object. All arguments and any block will be passed into the method defined.
If the method has never been defined for this state, then a NoMethodError will be raised.
Generates a human-readable description of this state‘s name / value:
For example,
State.new(machine, :parked).description # => "parked" State.new(machine, :parked, :value => :parked).description # => "parked" State.new(machine, :parked, :value => nil).description # => "parked (nil)" State.new(machine, :parked, :value => 1).description # => "parked (1)" State.new(machine, :parked, :value => lambda {Time.now}).description # => "parked (*)
Draws a representation of this state on the given machine. This will create a new node on the graph with the following properties:
The actual node generated on the graph will be returned.
Determines whether there are any states that can be transitioned to from this state. If there are none, then this state is considered final. Any objects in a final state will remain so forever given the current machine‘s definition.
Transforms the state name into a more human-readable format, such as "first gear" instead of "first_gear"
Generates a nicely formatted description of this state‘s contents.
For example,
state = StateMachine::State.new(machine, :parked, :value => 1, :initial => true) state # => #<StateMachine::State name=:parked value=1 initial=true context=[]>
Determines whether this state matches the given value. If no matcher is configured, then this will check whether the values are equivalent. Otherwise, the matcher will determine the result.
For example,
# Without a matcher state = State.new(machine, :parked, :value => 1) state.matches?(1) # => true state.matches?(2) # => false # With a matcher state = State.new(machine, :parked, :value => lambda {Time.now}, :if => lambda {|value| !value.nil?}) state.matches?(nil) # => false state.matches?(Time.now) # => true
The value that represents this state. This will optionally evaluate the original block if it‘s a lambda block. Otherwise, the static value is returned.
For example,
State.new(machine, :parked, :value => 1).value # => 1 State.new(machine, :parked, :value => lambda {Time.now}).value # => Tue Jan 01 00:00:00 UTC 2008 State.new(machine, :parked, :value => lambda {Time.now}).value(false) # => <Proc:0xb6ea7ca0@...>