Module: Workflow::InstanceMethods

Defined in:
lib/workflow.rb

Instance Method Summary collapse

Instance Method Details

#current_stateObject



78
79
80
81
82
# File 'lib/workflow.rb', line 78

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



133
134
135
136
# File 'lib/workflow.rb', line 133

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

#halt!(reason = nil) ⇒ Object

Raises:



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

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.



86
87
88
# File 'lib/workflow.rb', line 86

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



92
93
94
# File 'lib/workflow.rb', line 92

def halted_because
  @halted_because
end

#process_event!(name, *args) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/workflow.rb', line 96

def process_event!(name, *args)
  event = current_state.events.first_applicable(name, self)
  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(from, to, 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, name, *args)
  end

  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

#specObject



144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/workflow.rb', line 144

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