Module: Statesman::Machine
- Defined in:
- lib/statesman/machine.rb
Overview
The main module, that should be ‘extend`ed in to state machine classes.
Defined Under Namespace
Modules: ClassMethods
Class Method Summary collapse
- .included(base) ⇒ Object
-
.retry_conflicts(max_retries = 1) ⇒ Object
Retry any transitions that fail due to a TransitionConflictError.
Instance Method Summary collapse
- #allowed_transitions ⇒ Object
- #can_transition_to?(new_state, metadata = {}) ⇒ Boolean
- #current_state(force_reload: false) ⇒ Object
- #execute(phase, initial_state, new_state, transition) ⇒ Object
- #history ⇒ Object
- #in_state?(*states) ⇒ Boolean
- #initialize(object, options = { transition_class: Statesman::Adapters::MemoryTransition }) ⇒ Object
- #last_transition(force_reload: false) ⇒ Object
- #transition_to(new_state, metadata = {}) ⇒ Object
- #transition_to!(new_state, metadata = {}) ⇒ Object
Class Method Details
.included(base) ⇒ Object
10 11 12 13 |
# File 'lib/statesman/machine.rb', line 10 def self.included(base) base.extend(ClassMethods) base.send(:attr_reader, :object) end |
.retry_conflicts(max_retries = 1) ⇒ Object
Retry any transitions that fail due to a TransitionConflictError
16 17 18 19 20 21 22 23 24 25 |
# File 'lib/statesman/machine.rb', line 16 def self.retry_conflicts(max_retries = 1) retry_attempt = 0 begin yield rescue TransitionConflictError retry_attempt += 1 retry_attempt <= max_retries ? retry : raise end end |
Instance Method Details
#allowed_transitions ⇒ Object
191 192 193 194 195 |
# File 'lib/statesman/machine.rb', line 191 def allowed_transitions successors_for(current_state).select do |state| can_transition_to?(state) end end |
#can_transition_to?(new_state, metadata = {}) ⇒ Boolean
201 202 203 204 205 206 207 208 |
# File 'lib/statesman/machine.rb', line 201 def can_transition_to?(new_state, = {}) validate_transition(from: current_state, to: new_state, metadata: ) true rescue TransitionFailedError, GuardFailedError false end |
#current_state(force_reload: false) ⇒ Object
182 183 184 185 |
# File 'lib/statesman/machine.rb', line 182 def current_state(force_reload: false) last_action = last_transition(force_reload: force_reload) last_action ? last_action.to_state : self.class.initial_state end |
#execute(phase, initial_state, new_state, transition) ⇒ Object
227 228 229 230 |
# File 'lib/statesman/machine.rb', line 227 def execute(phase, initial_state, new_state, transition) callbacks = callbacks_for(phase, from: initial_state, to: new_state) callbacks.each { |cb| cb.call(@object, transition) } end |
#history ⇒ Object
210 211 212 |
# File 'lib/statesman/machine.rb', line 210 def history @storage_adapter.history end |
#in_state?(*states) ⇒ Boolean
187 188 189 |
# File 'lib/statesman/machine.rb', line 187 def in_state?(*states) states.flatten.any? { |state| current_state == state.to_s } end |
#initialize(object, options = { transition_class: Statesman::Adapters::MemoryTransition }) ⇒ Object
171 172 173 174 175 176 177 178 179 180 |
# File 'lib/statesman/machine.rb', line 171 def initialize(object, = { transition_class: Statesman::Adapters::MemoryTransition }) @object = object @transition_class = [:transition_class] @storage_adapter = adapter_class(@transition_class).new( @transition_class, object, self, ) send(:after_initialize) if respond_to? :after_initialize end |
#last_transition(force_reload: false) ⇒ Object
197 198 199 |
# File 'lib/statesman/machine.rb', line 197 def last_transition(force_reload: false) @storage_adapter.last(force_reload: force_reload) end |
#transition_to(new_state, metadata = {}) ⇒ Object
232 233 234 235 236 |
# File 'lib/statesman/machine.rb', line 232 def transition_to(new_state, = {}) self.transition_to!(new_state, ) rescue TransitionFailedError, GuardFailedError false end |
#transition_to!(new_state, metadata = {}) ⇒ Object
214 215 216 217 218 219 220 221 222 223 224 225 |
# File 'lib/statesman/machine.rb', line 214 def transition_to!(new_state, = {}) initial_state = current_state new_state = new_state.to_s validate_transition(from: initial_state, to: new_state, metadata: ) @storage_adapter.create(initial_state, new_state, ) true end |