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

Instance Method Summary collapse

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(metadata = {}) ⇒ Object



188
189
190
191
192
# File 'lib/statesman/machine.rb', line 188

def allowed_transitions( = {})
  successors_for(current_state).select do |state|
    can_transition_to?(state, )
  end
end

#can_transition_to?(new_state, metadata = {}) ⇒ Boolean

Returns:

  • (Boolean)


198
199
200
201
202
203
204
205
# File 'lib/statesman/machine.rb', line 198

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



179
180
181
182
# File 'lib/statesman/machine.rb', line 179

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



224
225
226
227
# File 'lib/statesman/machine.rb', line 224

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

#historyObject



207
208
209
# File 'lib/statesman/machine.rb', line 207

def history
  @storage_adapter.history
end

#in_state?(*states) ⇒ Boolean

Returns:

  • (Boolean)


184
185
186
# File 'lib/statesman/machine.rb', line 184

def in_state?(*states)
  states.flatten.any? { |state| current_state == state.to_s }
end

#initialize(object, options = { transition_class: Statesman::Adapters::MemoryTransition, }) ⇒ Object



167
168
169
170
171
172
173
174
175
176
177
# File 'lib/statesman/machine.rb', line 167

def initialize(object,
               options = {
                 transition_class: Statesman::Adapters::MemoryTransition,
               })
  @object = object
  @transition_class = options[:transition_class]
  @storage_adapter = adapter_class(@transition_class).new(
    @transition_class, object, self, options
  )
  send(:after_initialize) if respond_to? :after_initialize
end

#last_transition(force_reload: false) ⇒ Object



194
195
196
# File 'lib/statesman/machine.rb', line 194

def last_transition(force_reload: false)
  @storage_adapter.last(force_reload: force_reload)
end

#transition_to(new_state, metadata = {}) ⇒ Object



229
230
231
232
233
# File 'lib/statesman/machine.rb', line 229

def transition_to(new_state,  = {})
  transition_to!(new_state, )
rescue TransitionFailedError, GuardFailedError
  false
end

#transition_to!(new_state, metadata = {}) ⇒ Object



211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/statesman/machine.rb', line 211

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