geeks

Module: Workflow::WorkflowInstanceMethods

Defined in:
lib/workflow.rb

Instance Method Summary

Instance Method Details

- (Object) current_state



146
147
148
149
150
# File 'lib/workflow.rb', line 146

def current_state
  loaded_state = load_workflow_state
  res = spec.states[loaded_state.to_sym] if loaded_state
  res || spec.initial_state
end

- (Object) halt(reason = nil)



196
197
198
199
# File 'lib/workflow.rb', line 196

def halt(reason = nil)
  @halted_because = reason
  @halted = true
end

- (Object) halt!(reason = nil)

Raises:



201
202
203
204
205
# File 'lib/workflow.rb', line 201

def halt!(reason = nil)
  @halted_because = reason
  @halted = true
  raise TransitionHalted.new(reason)
end

- (Boolean) halted?

See the ‘Guards’ section in the README

Returns:

  • (Boolean)

    true if the last transition was halted by one of the transition callbacks.



154
155
156
# File 'lib/workflow.rb', line 154

def halted?
  @halted
end

- (Object) halted_because

call of `halt` or `halt!` method.

Returns:

  • the reason of the last transition abort as set by the previous



160
161
162
# File 'lib/workflow.rb', line 160

def halted_because
  @halted_because
end

- (Object) process_event!(name, *args)



164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/workflow.rb', line 164

def process_event!(name, *args)
  event = current_state.events[name.to_sym]
  raise NoTransitionAllowed.new(
    "There is no event #{name.to_sym} defined for the #{current_state} state") \
    if event.nil?
  @halted_because = nil
  @halted = false

  check_transition(event)

  from = current_state
  to = spec.states[event.transitions_to]

  run_before_transition(current_state, spec.states[event.transitions_to], name, *args)
  return false if @halted

  return_value = run_action(event.action, *args) || run_action_callback(event.name, *args)
  return false if @halted

  run_on_transition(from, to, name, *args)

  run_on_exit(from, to, name, *args)

  transition_value = persist_workflow_state to.to_s

  run_on_entry(to, from, name, *args)

  run_after_transition(from, to, name, *args)

  return_value.nil? ? transition_value : return_value
end

- (Object) spec



207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/workflow.rb', line 207

def spec
  # check the singleton class first
  class << self
    return workflow_spec if workflow_spec
  end

  c = self.class
  # using a simple loop instead of class_inheritable_accessor to avoid
  # dependency on Rails' ActiveSupport
  until c.workflow_spec || !(c.include? Workflow)
    c = c.superclass
  end
  c.workflow_spec
end