workflow 0.4.2 → 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.markdown +10 -2
- data/VERSION +1 -1
- data/lib/workflow.rb +22 -26
- data/test/main_test.rb +4 -1
- metadata +4 -4
data/README.markdown
CHANGED
@@ -304,7 +304,8 @@ logging then you can use the universal `on_transition` hook:
|
|
304
304
|
### Guards
|
305
305
|
|
306
306
|
If you want to halt the transition conditionally, you can just raise an
|
307
|
-
exception
|
307
|
+
exception in your [transition event handler](#transition_event_handler).
|
308
|
+
There is a helper called `halt!`, which raises the
|
308
309
|
Workflow::TransitionHalted exception. You can provide an additional
|
309
310
|
`halted_because` parameter.
|
310
311
|
|
@@ -317,6 +318,8 @@ The traditional `halt` (without the exclamation mark) is still supported
|
|
317
318
|
too. This just prevents the state change without raising an
|
318
319
|
exception.
|
319
320
|
|
321
|
+
You can check `halted?` and `halted_because` values later.
|
322
|
+
|
320
323
|
### Hook order
|
321
324
|
|
322
325
|
The whole event sequence is as follows:
|
@@ -372,6 +375,11 @@ when using both a block and a callback method for an event, the block executes p
|
|
372
375
|
Changelog
|
373
376
|
---------
|
374
377
|
|
378
|
+
### New in the version 0.5.0
|
379
|
+
|
380
|
+
* change the behaviour of halt! to immediately raise an exception. See
|
381
|
+
also http://github.com/geekq/workflow/issues/#issue/3
|
382
|
+
|
375
383
|
### New in the version 0.4.0
|
376
384
|
|
377
385
|
* completely rewritten the documentation to match my branch
|
@@ -425,7 +433,7 @@ Support
|
|
425
433
|
About
|
426
434
|
-----
|
427
435
|
|
428
|
-
Author: Vladimir Dobriakov, http://www.innoq.com/blog/vd
|
436
|
+
Author: Vladimir Dobriakov, <http://www.innoq.com/blog/vd>, <http://blog.geekq.net/>
|
429
437
|
|
430
438
|
Copyright (c) 2008-2009 Vodafone
|
431
439
|
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.5.0
|
data/lib/workflow.rb
CHANGED
@@ -135,10 +135,14 @@ module Workflow
|
|
135
135
|
res || spec.initial_state
|
136
136
|
end
|
137
137
|
|
138
|
+
# See the 'Guards' section in the README
|
139
|
+
# @return true if the last transition was halted by one of the transition callbacks.
|
138
140
|
def halted?
|
139
141
|
@halted
|
140
142
|
end
|
141
143
|
|
144
|
+
# @return the reason of the last transition abort as set by the previous
|
145
|
+
# call of `halt` or `halt!` method.
|
142
146
|
def halted_because
|
143
147
|
@halted_because
|
144
148
|
end
|
@@ -148,18 +152,11 @@ module Workflow
|
|
148
152
|
raise NoTransitionAllowed.new(
|
149
153
|
"There is no event #{name.to_sym} defined for the #{current_state} state") \
|
150
154
|
if event.nil?
|
151
|
-
# This three member variables are a relict from the old workflow library
|
152
|
-
# TODO: refactor some day
|
153
155
|
@halted_because = nil
|
154
156
|
@halted = false
|
155
|
-
@raise_exception_on_halt = false
|
156
157
|
return_value = run_action(event.action, *args) || run_action_callback(event.name, *args)
|
157
158
|
if @halted
|
158
|
-
|
159
|
-
raise TransitionHalted.new(@halted_because)
|
160
|
-
else
|
161
|
-
false
|
162
|
-
end
|
159
|
+
return false
|
163
160
|
else
|
164
161
|
check_transition(event)
|
165
162
|
run_on_transition(current_state, spec.states[event.transitions_to], name, *args)
|
@@ -168,16 +165,15 @@ module Workflow
|
|
168
165
|
end
|
169
166
|
end
|
170
167
|
|
171
|
-
|
168
|
+
def halt(reason = nil)
|
169
|
+
@halted_because = reason
|
170
|
+
@halted = true
|
171
|
+
end
|
172
172
|
|
173
|
-
def
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
if !spec.states[event.transitions_to]
|
178
|
-
raise WorkflowError.new("Event[#{event.name}]'s " +
|
179
|
-
"transitions_to[#{event.transitions_to}] is not a declared state.")
|
180
|
-
end
|
173
|
+
def halt!(reason = nil)
|
174
|
+
@halted_because = reason
|
175
|
+
@halted = true
|
176
|
+
raise TransitionHalted.new(reason)
|
181
177
|
end
|
182
178
|
|
183
179
|
def spec
|
@@ -190,16 +186,16 @@ module Workflow
|
|
190
186
|
c.workflow_spec
|
191
187
|
end
|
192
188
|
|
193
|
-
|
194
|
-
@halted_because = reason
|
195
|
-
@halted = true
|
196
|
-
@raise_exception_on_halt = false
|
197
|
-
end
|
189
|
+
private
|
198
190
|
|
199
|
-
def
|
200
|
-
|
201
|
-
|
202
|
-
|
191
|
+
def check_transition(event)
|
192
|
+
# Create a meaningful error message instead of
|
193
|
+
# "undefined method `on_entry' for nil:NilClass"
|
194
|
+
# Reported by Kyle Burton
|
195
|
+
if !spec.states[event.transitions_to]
|
196
|
+
raise WorkflowError.new("Event[#{event.name}]'s " +
|
197
|
+
"transitions_to[#{event.transitions_to}] is not a declared state.")
|
198
|
+
end
|
203
199
|
end
|
204
200
|
|
205
201
|
def transition(from, to, name, *args)
|
data/test/main_test.rb
CHANGED
@@ -390,9 +390,10 @@ class MainTest < Test::Unit::TestCase
|
|
390
390
|
assert_equal 'too fast', joe.halted_because
|
391
391
|
end
|
392
392
|
|
393
|
-
test 'halt! raises exception' do
|
393
|
+
test 'halt! raises exception immediately' do
|
394
394
|
article_class = Class.new do
|
395
395
|
include Workflow
|
396
|
+
attr_accessor :too_far
|
396
397
|
workflow do
|
397
398
|
state :new do
|
398
399
|
event :reject, :transitions_to => :rejected
|
@@ -403,6 +404,7 @@ class MainTest < Test::Unit::TestCase
|
|
403
404
|
def reject(reason)
|
404
405
|
halt! 'We do not reject articles unless the reason is important' \
|
405
406
|
unless reason =~ /important/i
|
407
|
+
self.too_far = "This line should not be executed"
|
406
408
|
end
|
407
409
|
end
|
408
410
|
|
@@ -411,6 +413,7 @@ class MainTest < Test::Unit::TestCase
|
|
411
413
|
assert_raise Workflow::TransitionHalted do
|
412
414
|
article.reject! 'Too funny'
|
413
415
|
end
|
416
|
+
assert_nil article.too_far
|
414
417
|
assert article.new?, 'Transition should have been halted'
|
415
418
|
article.reject! 'Important: too short'
|
416
419
|
assert article.rejected?, 'Transition should happen now'
|
metadata
CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
|
|
4
4
|
prerelease: false
|
5
5
|
segments:
|
6
6
|
- 0
|
7
|
-
-
|
8
|
-
-
|
9
|
-
version: 0.
|
7
|
+
- 5
|
8
|
+
- 0
|
9
|
+
version: 0.5.0
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Vladimir Dobriakov
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2010-
|
17
|
+
date: 2010-07-01 00:00:00 +02:00
|
18
18
|
default_executable:
|
19
19
|
dependencies: []
|
20
20
|
|