Class: Stateful::State

Inherits:
Object
  • Object
show all
Defined in:
lib/Stateful/State.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, options = {}) ⇒ State

Returns a new instance of State.



13
14
15
16
17
# File 'lib/Stateful/State.rb', line 13

def initialize(name, options = {})
  @name = name
  @options = options
  @transitions = []
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



10
11
12
# File 'lib/Stateful/State.rb', line 10

def name
  @name
end

#transitionsObject

Returns the value of attribute transitions.



11
12
13
# File 'lib/Stateful/State.rb', line 11

def transitions
  @transitions
end

Instance Method Details

#event(event) ⇒ Object Also known as: on



19
20
21
22
23
24
# File 'lib/Stateful/State.rb', line 19

def event(event)
  event_name, new_state = event.keys.first, event.values.first
  transitions << Transition.new(event_name, new_state)
  transitions.shuffle if non_deterministic_event_ordering?
  transitions
end

#next_state_name(event_name) ⇒ Object



27
28
29
30
31
# File 'lib/Stateful/State.rb', line 27

def next_state_name(event_name)
  if matching_transition = transitions.detect{|transition| transition.event_name == event_name}
    matching_transition.next_state_name
  end
end