class Statum::StateDefiner

Class for create hash options for machine

Public Class Methods

new(klass, field, options) click to toggle source

Creates an instance of definer

@param [Class] klass Class that includes Statum @param [String|Symbol] field Field that will be used for storing current state @param [Hash] options Hash options @option options [Symbol] initial Initial value

# File lib/statum/state_definer.rb, line 10
def initialize(klass, field, options)
  @klass   = klass
  @field   = field.to_sym
  @initial = options.fetch(:initial, nil)
  @states  = []
  @events  = {}

  state(@initial) unless @initial.nil?
end

Public Instance Methods

any_state() click to toggle source

Returns any state identifier

@return [Symbol]

# File lib/statum/state_definer.rb, line 42
def any_state
  Statum::ANY_STATE_NAME
end
event(name, options) click to toggle source

Define a new event

@param [String|Symbol] name Event name @param [Hash] options Options hash First key-value pair must be 'from' and 'to' transition states Other pairs are event options

# File lib/statum/state_definer.rb, line 52
def event(name, options)
  return if @events.key?(name.to_sym)

  from, to = options.shift
  @events[name.to_sym] = Statum::Event.new(from, to, options)
end
state(name) click to toggle source

Define a new state

@param [Symbol] name State name

# File lib/statum/state_definer.rb, line 35
def state(name)
  @states << name.to_sym unless @states.include?(name.to_sym)
end
state_machine() click to toggle source

Returns state machine

@return [Statum::Machine]

# File lib/statum/state_definer.rb, line 23
def state_machine
  Statum::Machine.new(
    field:   @field,
    initial: @initial,
    states:  @states,
    events:  @events
  )
end