-
Notifications
You must be signed in to change notification settings - Fork 206
/
workflow.rb
272 lines (224 loc) · 8.48 KB
/
workflow.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
require 'rubygems'
require 'workflow/specification'
# See also README for documentation
module Workflow
module ClassMethods
attr_reader :workflow_spec
# Workflow does not provide any state persistence - it is the job of particular
# persistence libraries for workflow and activerecord or remodel.
# But it still makes sense to provide a default name and override feature.
def workflow_column(column_name=nil)
if column_name
@workflow_state_column_name = column_name.to_sym
end
if !instance_variable_defined?('@workflow_state_column_name') && superclass.respond_to?(:workflow_column)
@workflow_state_column_name = superclass.workflow_column
end
@workflow_state_column_name ||= :workflow_state
end
def workflow(&specification)
assign_workflow Specification.new(Hash.new, &specification)
end
private
# Creates the convinience methods like `my_transition!`
def assign_workflow(specification_object)
# Merging two workflow specifications can **not** be done automically, so
# just make the latest specification win. Same for inheritance -
# definition in the subclass wins.
if respond_to? :inherited_workflow_spec # undefine methods defined by the old workflow_spec
inherited_workflow_spec.states.values.each do |state|
state_name = state.name
module_eval do
undef_method "#{state_name}?"
end
state.events.flat.each do |event|
event_name = event.name
module_eval do
undef_method "#{event_name}!".to_sym
undef_method "can_#{event_name}?"
end
end
end
end
@workflow_spec = specification_object
@workflow_spec.states.values.each do |state|
state_name = state.name
module_eval do
define_method "#{state_name}?" do
state_name == current_state.name
end
end
state.events.flat.each do |event|
event_name = event.name
module_eval do
define_method "#{event_name}!".to_sym do |*args, **kwargs|
process_event!(event_name, *args, **kwargs)
end
define_method "can_#{event_name}?".to_sym do |*args, **kwargs|
return !!current_state.events.first_applicable(event_name, self, args)
end
end
end
end
end
end
module InstanceMethods
def current_state
loaded_state = load_workflow_state
res = spec.states[loaded_state.to_sym] if loaded_state
res || spec.initial_state
end
# See the 'Guards' section in the README
# @return true if the last transition was halted by one of the transition callbacks.
def halted?
@halted
end
# @return the reason of the last transition abort as set by the previous
# call of `halt` or `halt!` method.
def halted_because
@halted_because
end
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
def halt(reason = nil)
@halted_because = reason
@halted = true
end
def halt!(reason = nil)
@halted_because = reason
@halted = true
raise TransitionHalted.new(reason)
end
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
private
def check_transition(event)
# Create a meaningful error message instead of
# "undefined method `on_entry' for nil:NilClass"
# Reported by Kyle Burton
if !spec.states[event.transitions_to]
raise WorkflowError.new("Event[#{event.name}]'s " +
"transitions_to[#{event.transitions_to}] is not a declared state.")
end
end
def run_before_transition(from, to, event, *args, **kwargs)
instance_exec(from.name, to.name, event, *args, **kwargs, &spec.before_transition_proc) if
spec.before_transition_proc
end
def run_on_error(error, from, to, event, *args, **kwargs)
if spec.on_error_proc
instance_exec(error, from.name, to.name, event, *args, **kwargs, &spec.on_error_proc)
halt(error.message)
else
raise error
end
end
def run_on_transition(from, to, event, *args, **kwargs)
instance_exec(from.name, to.name, event, *args, **kwargs, &spec.on_transition_proc) if spec.on_transition_proc
end
def run_after_transition(from, to, event, *args, **kwargs)
instance_exec(from.name, to.name, event, *args, **kwargs, &spec.after_transition_proc) if
spec.after_transition_proc
end
def run_action(action, *args, **kwargs)
instance_exec(*args, **kwargs, &action) if action
end
def has_callback?(action)
# 1. public callback method or
# 2. protected method somewhere in the class hierarchy or
# 3. private in the immediate class (parent classes ignored)
action = action.to_sym
self.respond_to?(action) or
self.class.protected_method_defined?(action) or
self.private_methods(false).map(&:to_sym).include?(action)
end
def run_action_callback(action_name, *args, **kwargs)
action = action_name.to_sym
self.send(action, *args, **kwargs) if has_callback?(action)
end
def run_on_entry(state, prior_state, triggering_event, *args, **kwargs)
if state.on_entry
instance_exec(prior_state.name, triggering_event, *args, **kwargs, &state.on_entry)
else
hook_name = "on_#{state}_entry"
self.send hook_name, prior_state, triggering_event, *args, **kwargs if has_callback?(hook_name)
end
end
def run_on_exit(state, new_state, triggering_event, *args, **kwargs)
if state
if state.on_exit
instance_exec(new_state.name, triggering_event, *args, **kwargs, &state.on_exit)
else
hook_name = "on_#{state}_exit"
self.send hook_name, new_state, triggering_event, *args, **kwargs if has_callback?(hook_name)
end
end
end
# load_workflow_state and persist_workflow_state
# can be overriden to handle the persistence of the workflow state.
#
# Default (non ActiveRecord) implementation stores the current state
# in a variable.
#
# Default ActiveRecord implementation uses a 'workflow_state' database column.
def load_workflow_state
@workflow_state if instance_variable_defined? :@workflow_state
end
def persist_workflow_state(new_value)
@workflow_state = new_value
end
end
def self.included(klass)
klass.send :include, InstanceMethods
# backup the parent workflow spec, making accessible through #inherited_workflow_spec
if klass.superclass.respond_to?(:workflow_spec, true)
klass.module_eval do
# see http://stackoverflow.com/a/2495650/111995 for implementation explanation
pro = Proc.new { klass.superclass.workflow_spec }
singleton_class = class << self; self; end
singleton_class.send(:define_method, :inherited_workflow_spec) do
pro.call
end
end
end
klass.extend ClassMethods
# Look for a hook; otherwise detect based on ancestor class.
if klass.respond_to?(:workflow_adapter)
klass.send :include, klass.workflow_adapter
end
end
end