Skip to content

Commit e639523

Browse files
author
Vladimir Dobriakov
committed
gh-219, gh-194 Add test+README example for reflection on event list
1 parent ace6a11 commit e639523

File tree

2 files changed

+50
-10
lines changed

2 files changed

+50
-10
lines changed

README.adoc

+14-10
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,8 @@ Advanced usage
342342
You can easily reflect on workflow specification programmatically - for
343343
the whole class or for the current object. Examples:
344344
345+
include::test/reflection_test.rb[tag=reflect]
346+
345347
article2.current_state.events # lists possible events from here
346348
article2.current_state.events[:reject].transitions_to # => :rejected
347349
@@ -375,18 +377,20 @@ representation of the workflow. See below.
375377
376378
Conditions can be a "method name symbol" with a corresponding instance method, a `proc` or `lambda` which are added to events, like so:
377379
378-
state :off
379-
event :turn_on, :transition_to => :on,
380-
:if => :sufficient_battery_level?
380+
....
381+
state :off
382+
event :turn_on, :transition_to => :on,
383+
:if => :sufficient_battery_level?
381384
382-
event :turn_on, :transition_to => :low_battery,
383-
:if => proc { |device| device.battery_level > 0 }
384-
end
385+
event :turn_on, :transition_to => :low_battery,
386+
:if => proc { |device| device.battery_level > 0 }
387+
end
385388
386-
# corresponding instance method
387-
def sufficient_battery_level?
388-
battery_level > 10
389-
end
389+
# corresponding instance method
390+
def sufficient_battery_level?
391+
battery_level > 10
392+
end
393+
....
390394
391395
When calling a `device.can_<fire_event>?` check, or attempting a `device.<event>!`, each event is checked in turn:
392396

test/reflection_test.rb

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
require File.join(File.dirname(__FILE__), 'test_helper')
2+
require 'workflow'
3+
4+
# Example from the README, TODO: integrate other way around via asciidoctor code inclusion
5+
class Article
6+
include Workflow
7+
workflow do
8+
state :new do
9+
event :submit, :transitions_to => :awaiting_review
10+
end
11+
state :awaiting_review do
12+
event :review, :transitions_to => :being_reviewed
13+
end
14+
state :being_reviewed do
15+
event :accept, :transitions_to => :accepted
16+
event :reject, :transitions_to => :rejected
17+
end
18+
state :accepted
19+
state :rejected
20+
end
21+
end
22+
23+
class MainTest < Minitest::Test
24+
test 'reflection' do
25+
article2 = Article.new
26+
article2.submit!
27+
article2.review!
28+
assert_equal 2, article2.current_state.events.length
29+
# Please note the usage of `first`, since coditional event transitions can
30+
# define multiple event definitions with the same name
31+
32+
# tag::reflect[]
33+
assert_equal :rejected, article2.current_state.events[:reject].first.transitions_to
34+
# end::reflect[]
35+
end
36+
end

0 commit comments

Comments
 (0)