Module: Workflow::InstanceMethods

Defined in:
lib/workflow.rb

Instance Method Summary collapse

Instance Method Details

#current_stateObject



91
92
93
94
95
96
97
98
99
# File 'lib/workflow.rb', line 91

def current_state
  loaded_state_value = load_workflow_state
  if loaded_state_value
    loaded_state_name = spec.states.select{|k,v| v.value.to_s == loaded_state_value.to_s}.keys.first
  end

  res = spec.states[loaded_state_name.to_sym] if loaded_state_name
  res || spec.initial_state
end

#halt(reason = nil) ⇒ Object



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

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

#halt!(reason = nil) ⇒ Object

Raises:



156
157
158
159
160
# File 'lib/workflow.rb', line 156

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.



103
104
105
# File 'lib/workflow.rb', line 103

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



109
110
111
# File 'lib/workflow.rb', line 109

def halted_because
  @halted_because
end

#process_event!(name, *args) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/workflow.rb', line 113

def process_event!(name, *args)
  event = current_state.events.first_applicable(name, self)
  if event.nil?
    return run_on_unavailable_transition(current_state, name, *args)
  end
  @halted_because = nil
  @halted = false

  check_transition(event)

  from = current_state
  to_state = spec.states[event.transitions_to]
  to_value = to_state.value

  run_before_transition(from, to_state, name, *args)
  return false if @halted

  begin
    return_value = run_action(event.action, *args) || run_action_callback(event.name, *args)
  rescue StandardError => e
    run_on_error(e, from, to_state, name, *args)
  end

  return false if @halted

  run_on_transition(from, to_state, name, *args)

  run_on_exit(from, to_state, name, *args)

  transition_value = persist_workflow_state to_value

  run_on_entry(to_state, from, name, *args)

  run_after_transition(from, to_state, name, *args)

  return_value.nil? ? transition_value : return_value
end

#specObject



162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/workflow.rb', line 162

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