Module: Statesman::Machine::ClassMethods
- Defined in:
- lib/statesman/machine.rb
Instance Attribute Summary collapse
-
#initial_state ⇒ Object
readonly
Returns the value of attribute initial_state.
Instance Method Summary collapse
- #after_callbacks ⇒ Object
- #after_transition(options = { from: nil, to: nil }, &block) ⇒ Object
- #before_callbacks ⇒ Object
- #before_transition(options = { from: nil, to: nil }, &block) ⇒ Object
- #guard_transition(options = { from: nil, to: nil }, &block) ⇒ Object
- #guards ⇒ Object
- #state(name, options = { initial: false }) ⇒ Object
- #states ⇒ Object
- #successors ⇒ Object
- #transition(options = { from: nil, to: nil }) ⇒ Object
- #validate_callback_condition(options = { from: nil, to: nil }) ⇒ Object
-
#validate_from_and_to_state(from, to) ⇒ Object
Check that the transition is valid when ‘from’ and ‘to’ are given.
-
#validate_not_from_terminal_state(from) ⇒ Object
Check that the ‘from’ state is not terminal.
-
#validate_not_to_initial_state(to) ⇒ Object
Check that the ‘to’ state is not initial.
Instance Attribute Details
#initial_state ⇒ Object (readonly)
Returns the value of attribute initial_state.
16 17 18 |
# File 'lib/statesman/machine.rb', line 16 def initial_state @initial_state end |
Instance Method Details
#after_callbacks ⇒ Object
39 40 41 |
# File 'lib/statesman/machine.rb', line 39 def after_callbacks @after_callbacks ||= [] end |
#after_transition(options = { from: nil, to: nil }, &block) ⇒ Object
66 67 68 69 70 71 72 |
# File 'lib/statesman/machine.rb', line 66 def after_transition( = { from: nil, to: nil }, &block) from = to_s_or_nil([:from]) to = to_s_or_nil([:to]) validate_callback_condition(from: from, to: to) after_callbacks << Callback.new(from: from, to: to, callback: block) end |
#before_callbacks ⇒ Object
35 36 37 |
# File 'lib/statesman/machine.rb', line 35 def before_callbacks @before_callbacks ||= [] end |
#before_transition(options = { from: nil, to: nil }, &block) ⇒ Object
58 59 60 61 62 63 64 |
# File 'lib/statesman/machine.rb', line 58 def before_transition( = { from: nil, to: nil }, &block) from = to_s_or_nil([:from]) to = to_s_or_nil([:to]) validate_callback_condition(from: from, to: to) before_callbacks << Callback.new(from: from, to: to, callback: block) end |
#guard_transition(options = { from: nil, to: nil }, &block) ⇒ Object
74 75 76 77 78 79 80 |
# File 'lib/statesman/machine.rb', line 74 def guard_transition( = { from: nil, to: nil }, &block) from = to_s_or_nil([:from]) to = to_s_or_nil([:to]) validate_callback_condition(from: from, to: to) guards << Guard.new(from: from, to: to, callback: block) end |
#guards ⇒ Object
43 44 45 |
# File 'lib/statesman/machine.rb', line 43 def guards @guards ||= [] end |
#state(name, options = { initial: false }) ⇒ Object
22 23 24 25 26 27 28 29 |
# File 'lib/statesman/machine.rb', line 22 def state(name, = { initial: false }) name = name.to_s if [:initial] validate_initial_state(name) @initial_state = name end states << name end |
#states ⇒ Object
18 19 20 |
# File 'lib/statesman/machine.rb', line 18 def states @states ||= [] end |
#successors ⇒ Object
31 32 33 |
# File 'lib/statesman/machine.rb', line 31 def successors @successors ||= {} end |
#transition(options = { from: nil, to: nil }) ⇒ Object
47 48 49 50 51 52 53 54 55 56 |
# File 'lib/statesman/machine.rb', line 47 def transition( = { from: nil, to: nil }) from = to_s_or_nil([:from]) to = Array([:to]).map { |item| to_s_or_nil(item) } successors[from] ||= [] ([from] + to).each { |state| validate_state(state) } successors[from] += to end |
#validate_callback_condition(options = { from: nil, to: nil }) ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/statesman/machine.rb', line 82 def validate_callback_condition( = { from: nil, to: nil }) from = to_s_or_nil([:from]) to = to_s_or_nil([:to]) [from, to].compact.each { |state| validate_state(state) } return if from.nil? && to.nil? validate_not_from_terminal_state(from) validate_not_to_initial_state(to) return if from.nil? || to.nil? validate_from_and_to_state(from, to) end |
#validate_from_and_to_state(from, to) ⇒ Object
Check that the transition is valid when ‘from’ and ‘to’ are given
114 115 116 117 118 119 |
# File 'lib/statesman/machine.rb', line 114 def validate_from_and_to_state(from, to) unless successors.fetch(from, []).include?(to) raise InvalidTransitionError, "Cannot transition from '#{from}' to '#{to}'" end end |
#validate_not_from_terminal_state(from) ⇒ Object
Check that the ‘from’ state is not terminal
98 99 100 101 102 103 |
# File 'lib/statesman/machine.rb', line 98 def validate_not_from_terminal_state(from) unless from.nil? || successors.keys.include?(from) raise InvalidTransitionError, "Cannont transition away from terminal state '#{from}'" end end |
#validate_not_to_initial_state(to) ⇒ Object
Check that the ‘to’ state is not initial
106 107 108 109 110 111 |
# File 'lib/statesman/machine.rb', line 106 def validate_not_to_initial_state(to) unless to.nil? || successors.values.flatten.include?(to) raise InvalidTransitionError, "Cannont transition to initial state '#{to}'" end end |