Class: Statum::StateDefiner
- Inherits:
-
Object
- Object
- Statum::StateDefiner
- Defined in:
- lib/statum/state_definer.rb
Overview
Class for create hash options for machine
Instance Method Summary collapse
-
#any_state ⇒ Symbol
Returns any state identifier.
-
#event(name, options) ⇒ Object
Define a new event.
-
#initialize(klass, field, options) ⇒ StateDefiner
constructor
Creates an instance of definer.
-
#state(name) ⇒ Object
Define a new state.
-
#state_machine ⇒ Statum::Machine
Returns state machine.
Constructor Details
#initialize(klass, field, options) ⇒ StateDefiner
Creates an instance of definer
10 11 12 13 14 15 16 17 18 |
# File 'lib/statum/state_definer.rb', line 10 def initialize(klass, field, ) @klass = klass @field = field.to_sym @initial = .fetch(:initial, nil) @states = [] @events = {} state(@initial) unless @initial.nil? end |
Instance Method Details
#any_state ⇒ Symbol
Returns any state identifier
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
52 53 54 55 56 57 |
# File 'lib/statum/state_definer.rb', line 52 def event(name, ) return if @events.key?(name.to_sym) from, to = .shift @events[name.to_sym] = Statum::Event.new(from, to, ) end |
#state(name) ⇒ Object
Define a new state
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_machine ⇒ Statum::Machine
Returns state machine
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 |