Class: Lab42::StateMachine::State
- Inherits:
-
Object
- Object
- Lab42::StateMachine::State
- Defined in:
- lib/lab42/state_machine/state.rb
Instance Attribute Summary collapse
-
#designation ⇒ Object
readonly
Returns the value of attribute designation.
-
#triggers ⇒ Object
readonly
Returns the value of attribute triggers.
Instance Method Summary collapse
- #_match(input, trigger) ⇒ Object
- #add(trigger, action, new_state) ⇒ Object
- #freeze ⇒ Object
- #transition(accumulator, input) ⇒ Object
Instance Attribute Details
#designation ⇒ Object (readonly)
Returns the value of attribute designation.
5 6 7 |
# File 'lib/lab42/state_machine/state.rb', line 5 def designation @designation end |
#triggers ⇒ Object (readonly)
Returns the value of attribute triggers.
5 6 7 |
# File 'lib/lab42/state_machine/state.rb', line 5 def triggers @triggers end |
Instance Method Details
#_match(input, trigger) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/lab42/state_machine/state.rb', line 24 def _match(input, trigger) case trigger when Lab42::Match m =trigger.match(input) m.success? && m when TrueClass true when FalseClass raise StopIteration when Symbol input.send trigger end end |
#add(trigger, action, new_state) ⇒ Object
7 8 9 10 11 |
# File 'lib/lab42/state_machine/state.rb', line 7 def add(trigger, action, new_state) trigger = Lab42::Match.new(trigger) if Regexp === trigger new_state ||= designation @triggers << [trigger, action, new_state] end |
#freeze ⇒ Object
38 39 40 41 |
# File 'lib/lab42/state_machine/state.rb', line 38 def freeze super @triggers.freeze end |
#transition(accumulator, input) ⇒ Object
13 14 15 16 17 18 19 20 21 22 |
# File 'lib/lab42/state_machine/state.rb', line 13 def transition(accumulator, input) triggers.each do |trigger, action, new_state| match = _match input, trigger next unless match output, new_acc, new_state1 = _apply(match, accumulator, input: input, to: action) output = output.string if Lab42::Match === output return [output, new_acc || accumulator, new_state1 || new_state || designation] end [input, accumulator, designation] end |