class Stately::State

A Stately::State object contains the configuration and other information about a defined state.

It's made up of a name (which is saved to the parent instance's state attribute), the name of an action (which is a method called to transition into this state), and a DSL to define allowed transitions, callbacks, and validations.

Constants

ACTIONS

Attributes

action[R]
after_transitions[R]
allow_from_states[R]
before_transitions[R]
name[R]
prevent_from_states[R]
validations[R]

Public Class Methods

new(name, action=nil, &block) click to toggle source

Sets up and returns a new Stately::State object.

@param [String] name The name of the state @param [String] action The method name that's called to transition to this state. Some method

names can be inferred based on the state's name.
# File lib/stately/state.rb, line 19
def initialize(name, action=nil, &block)
  @action = (action || guess_action_for(name)).to_s
  @name = name

  @allow_from_states = []
  @prevent_from_states = []

  @before_transitions = []
  @after_transitions = []
  @validations = []

  if block_given?
    configuration = StateConfigurator.new(&block)

    @allow_from_states = configuration.allow_from_states || []
    @prevent_from_states = configuration.prevent_from_states || []

    @after_transitions = configuration.after_transitions || []
    @before_transitions = configuration.before_transitions || []
    @validations = configuration.validations || []
  end
end

Public Instance Methods

to_s() click to toggle source

@return [String] The state name as a string

# File lib/stately/state.rb, line 43
def to_s
  @name.to_s
end
to_sym() click to toggle source

@return [Symbol] The state name as a string

# File lib/stately/state.rb, line 48
def to_sym
  @name.to_sym
end

Private Instance Methods

guess_action_for(name) click to toggle source
# File lib/stately/state.rb, line 58
def guess_action_for(name)
  ACTIONS.fetch(name.to_sym, name)
end