Class: Statum::StateDefiner

Inherits:
Object
  • Object
show all
Defined in:
lib/statum/state_definer.rb

Overview

Class for create hash options for machine

Instance Method Summary collapse

Constructor Details

#initialize(klass, field, options) ⇒ StateDefiner

Creates an instance of definer

Parameters:

  • klass (Class)

    Class that includes Statum

  • field (String|Symbol)

    Field that will be used for storing current state

  • options (Hash)

    Hash options

Options Hash (options):

  • initial (Symbol)

    Initial value



10
11
12
13
14
15
16
17
18
# 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

Instance Method Details

#any_stateSymbol

Returns any state identifier

Returns:

  • (Symbol)


42
43
44
# File 'lib/statum/state_definer.rb', line 42

def any_state
  Statum::ANY_STATE_NAME
end

#event(name, options) ⇒ Object

Define a new event

First key-value pair must be ‘from’ and ‘to’ transition states Other pairs are event options

Parameters:

  • name (String|Symbol)

    Event name

  • options (Hash)

    Options hash



52
53
54
55
56
57
# 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) ⇒ Object

Define a new state

Parameters:

  • name (Symbol)

    State name



35
36
37
# File 'lib/statum/state_definer.rb', line 35

def state(name)
  @states << name.to_sym unless @states.include?(name.to_sym)
end

#state_machineStatum::Machine

Returns state machine

Returns:



23
24
25
26
27
28
29
30
# File 'lib/statum/state_definer.rb', line 23

def state_machine
  Statum::Machine.new(
    field:   @field,
    initial: @initial,
    states:  @states,
    events:  @events
  )
end