Module: Workflow::InstanceMethods

Defined in:
lib/workflow.rb

Instance Method Summary collapse

Instance Method Details

#current_stateObject



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

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



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

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

#halt!(reason = nil) ⇒ Object

Raises:



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

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.



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

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



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

def halted_because
  @halted_because
end

#process_event!(name, *args, **kwargs) ⇒ Object



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
132
# File 'lib/workflow.rb', line 97

def process_event!(name, *args, **kwargs)
  event = current_state.events.first_applicable(name, self, args)
  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, **kwargs)
  return false if @halted

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

  return false if @halted

  run_on_transition(from, to, name, *args, **kwargs)

  run_on_exit(from, to, name, *args, **kwargs)

  transition_value = persist_workflow_state to.to_s

  run_on_entry(to, from, name, *args, **kwargs)

  run_after_transition(from, to, name, *args, **kwargs)

  return_value.nil? ? transition_value : return_value
end

#specObject



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

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