Module: Workflow::WorkflowInstanceMethods

Defined in:
lib/workflow.rb

Instance Method Summary collapse

Instance Method Details

#current_stateObject



137
138
139
140
141
# File 'lib/workflow.rb', line 137

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

#halt(reason = nil) ⇒ Object



175
176
177
178
# File 'lib/workflow.rb', line 175

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

#halt!(reason = nil) ⇒ Object

Raises:



180
181
182
183
184
# File 'lib/workflow.rb', line 180

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

#halted?Boolean

See the ‘Guards’ section in the README

Returns:

  • (Boolean)

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



145
146
147
# File 'lib/workflow.rb', line 145

def halted?
  @halted
end

#halted_becauseObject

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

Returns:

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



151
152
153
# File 'lib/workflow.rb', line 151

def halted_because
  @halted_because
end

#process_event!(name, *args) ⇒ Object



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/workflow.rb', line 155

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
  return_value = run_action(event.action, *args) || run_action_callback(event.name, *args)
  if @halted
    return false
  else
    check_transition(event)
    run_on_transition(current_state, spec.states[event.transitions_to], name, *args)
    transition_value = transition(
      current_state, spec.states[event.transitions_to], name, *args
    )
    return_value.nil? ? transition_value : return_value
  end
end

#specObject



186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/workflow.rb', line 186

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