Skip to content

Commit

Permalink
gh-219, gh-194 Add test+README example for reflection on event list
Browse files Browse the repository at this point in the history
  • Loading branch information
Vladimir Dobriakov committed Aug 2, 2019
1 parent ace6a11 commit e639523
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 10 deletions.
24 changes: 14 additions & 10 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,8 @@ Advanced usage
You can easily reflect on workflow specification programmatically - for
the whole class or for the current object. Examples:
include::test/reflection_test.rb[tag=reflect]
article2.current_state.events # lists possible events from here
article2.current_state.events[:reject].transitions_to # => :rejected
Expand Down Expand Up @@ -375,18 +377,20 @@ representation of the workflow. See below.
Conditions can be a "method name symbol" with a corresponding instance method, a `proc` or `lambda` which are added to events, like so:
state :off
event :turn_on, :transition_to => :on,
:if => :sufficient_battery_level?
....
state :off
event :turn_on, :transition_to => :on,
:if => :sufficient_battery_level?
event :turn_on, :transition_to => :low_battery,
:if => proc { |device| device.battery_level > 0 }
end
event :turn_on, :transition_to => :low_battery,
:if => proc { |device| device.battery_level > 0 }
end
# corresponding instance method
def sufficient_battery_level?
battery_level > 10
end
# corresponding instance method
def sufficient_battery_level?
battery_level > 10
end
....
When calling a `device.can_<fire_event>?` check, or attempting a `device.<event>!`, each event is checked in turn:
Expand Down
36 changes: 36 additions & 0 deletions test/reflection_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
require File.join(File.dirname(__FILE__), 'test_helper')
require 'workflow'

# Example from the README, TODO: integrate other way around via asciidoctor code inclusion
class Article
include Workflow
workflow do
state :new do
event :submit, :transitions_to => :awaiting_review
end
state :awaiting_review do
event :review, :transitions_to => :being_reviewed
end
state :being_reviewed do
event :accept, :transitions_to => :accepted
event :reject, :transitions_to => :rejected
end
state :accepted
state :rejected
end
end

class MainTest < Minitest::Test
test 'reflection' do
article2 = Article.new
article2.submit!
article2.review!
assert_equal 2, article2.current_state.events.length
# Please note the usage of `first`, since coditional event transitions can
# define multiple event definitions with the same name

# tag::reflect[]
assert_equal :rejected, article2.current_state.events[:reject].first.transitions_to
# end::reflect[]
end
end

0 comments on commit e639523

Please sign in to comment.