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_guard_failure(options = {}, &block) ⇒ Object
- #after_transition(options = { after_commit: false }, &block) ⇒ Object
- #after_transition_failure(options = {}, &block) ⇒ Object
- #before_transition(options = {}, &block) ⇒ Object
- #callbacks ⇒ Object
- #guard_transition(options = {}, &block) ⇒ Object
- #state(name, options = { initial: false }) ⇒ Object
- #states ⇒ Object
- #successors ⇒ Object
- #transition(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.
28 29 30 |
# File 'lib/statesman/machine.rb', line 28 def initial_state @initial_state end |
Instance Method Details
#after_guard_failure(options = {}, &block) ⇒ Object
93 94 95 96 |
# File 'lib/statesman/machine.rb', line 93 def after_guard_failure( = {}, &block) add_callback(callback_type: :after_guard_failure, callback_class: Callback, from: [:from], to: [:to], &block) end |
#after_transition(options = { after_commit: false }, &block) ⇒ Object
81 82 83 84 85 86 |
# File 'lib/statesman/machine.rb', line 81 def after_transition( = { after_commit: false }, &block) callback_type = [:after_commit] ? :after_commit : :after add_callback(callback_type: callback_type, callback_class: Callback, from: [:from], to: [:to], &block) end |
#after_transition_failure(options = {}, &block) ⇒ Object
88 89 90 91 |
# File 'lib/statesman/machine.rb', line 88 def after_transition_failure( = {}, &block) add_callback(callback_type: :after_transition_failure, callback_class: Callback, from: [:from], to: [:to], &block) end |
#before_transition(options = {}, &block) ⇒ Object
71 72 73 74 |
# File 'lib/statesman/machine.rb', line 71 def before_transition( = {}, &block) add_callback(callback_type: :before, callback_class: Callback, from: [:from], to: [:to], &block) end |
#callbacks ⇒ Object
47 48 49 50 51 52 53 54 55 56 |
# File 'lib/statesman/machine.rb', line 47 def callbacks @callbacks ||= { before: [], after: [], after_transition_failure: [], after_guard_failure: [], after_commit: [], guards: [], } end |
#guard_transition(options = {}, &block) ⇒ Object
76 77 78 79 |
# File 'lib/statesman/machine.rb', line 76 def guard_transition( = {}, &block) add_callback(callback_type: :guards, callback_class: Guard, from: [:from], to: [:to], &block) end |
#state(name, options = { initial: false }) ⇒ Object
34 35 36 37 38 39 40 41 |
# File 'lib/statesman/machine.rb', line 34 def state(name, = { initial: false }) name = name.to_s if [:initial] validate_initial_state(name) @initial_state = name end states << name end |
#states ⇒ Object
30 31 32 |
# File 'lib/statesman/machine.rb', line 30 def states @states ||= [] end |
#successors ⇒ Object
43 44 45 |
# File 'lib/statesman/machine.rb', line 43 def successors @successors ||= {} end |
#transition(from: nil, to: nil) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/statesman/machine.rb', line 58 def transition(from: nil, to: nil) from = to_s_or_nil(from) to = array_to_s_or_nil(to) raise InvalidStateError, "No to states provided." if to.empty? successors[from] ||= [] ([from] + to).each { |state| validate_state(state) } successors[from] += to end |
#validate_callback_condition(options = { from: nil, to: nil }) ⇒ Object
98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/statesman/machine.rb', line 98 def validate_callback_condition( = { from: nil, to: nil }) from = to_s_or_nil([:from]) to = array_to_s_or_nil([:to]) ([from] + to).compact.each { |state| validate_state(state) } return if from.nil? && to.empty? validate_not_from_terminal_state(from) to.each { |state| validate_not_to_initial_state(state) } return if from.nil? || to.empty? to.each { |state| validate_from_and_to_state(from, state) } end |
#validate_from_and_to_state(from, to) ⇒ Object
Check that the transition is valid when ‘from’ and ‘to’ are given
130 131 132 133 134 135 |
# File 'lib/statesman/machine.rb', line 130 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
114 115 116 117 118 119 |
# File 'lib/statesman/machine.rb', line 114 def validate_not_from_terminal_state(from) unless from.nil? || successors.key?(from) raise InvalidTransitionError, "Cannot transition away from terminal state '#{from}'" end end |
#validate_not_to_initial_state(to) ⇒ Object
Check that the ‘to’ state is not initial
122 123 124 125 126 127 |
# File 'lib/statesman/machine.rb', line 122 def validate_not_to_initial_state(to) unless to.nil? || successors.values.flatten.include?(to) raise InvalidTransitionError, "Cannot transition to initial state '#{to}'" end end |