branston 0.6.1 → 0.6.2
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +1 -1
- data/lib/branston/Gemfile +25 -0
- data/lib/branston/Gemfile.lock +76 -0
- data/lib/branston/app/controllers/application_controller.rb +1 -1
- data/lib/branston/app/controllers/outcomes_controller.rb +2 -0
- data/lib/branston/app/controllers/stories_controller.rb +82 -86
- data/lib/branston/app/controllers/users_controller.rb +69 -11
- data/lib/branston/app/helpers/iterations_helper.rb +13 -13
- data/lib/branston/app/models/iteration.rb +3 -1
- data/lib/branston/app/models/release.rb +0 -1
- data/lib/branston/app/models/story.rb +30 -28
- data/lib/branston/app/models/user.rb +46 -1
- data/lib/branston/app/views/layouts/_header.html.erb +8 -3
- data/lib/branston/app/views/layouts/user_roles.html.erb +5 -5
- data/lib/branston/app/views/sessions/new.html.erb +8 -14
- data/lib/branston/app/views/users/_admin_controls.html.erb +14 -0
- data/lib/branston/app/views/users/_form.html.erb +27 -0
- data/lib/branston/app/views/users/edit.html.erb +9 -0
- data/lib/branston/app/views/users/index.html.erb +14 -0
- data/lib/branston/app/views/users/new.html.erb +3 -22
- data/lib/branston/config/boot.rb +20 -0
- data/lib/branston/config/environment.rb +2 -7
- data/lib/branston/config/environments/test.rb +0 -8
- data/lib/branston/config/preinitializer.rb +21 -0
- data/lib/branston/config/routes.rb +15 -10
- data/lib/branston/db/development.sqlite3 +0 -0
- data/lib/branston/db/development_structure.sql +21 -8
- data/lib/branston/db/migrate/20100723161424_add_state_to_user.rb +12 -0
- data/lib/branston/db/migrate/20100726150322_add_activation_fields_to_user.rb +12 -0
- data/lib/branston/db/migrate/20100729125551_set_default_user_state_to_pending.rb +10 -0
- data/lib/branston/db/migrate/20100812133837_add_is_admin_property_to_user.rb +10 -0
- data/lib/branston/db/migrate/20100812140532_set_default_user_state_to_active.rb +10 -0
- data/lib/branston/db/migrate/20100812143455_add_default_admin_user.rb +17 -0
- data/lib/branston/db/migrate/20110408162438_remove_is_admin_property_and_add_role_instead.rb +12 -0
- data/lib/branston/db/pristine.sqlite3 +0 -0
- data/lib/branston/db/schema.rb +6 -8
- data/lib/branston/db/test.sqlite3 +0 -0
- data/lib/branston/log/development.log +1181 -433
- data/lib/branston/log/test.log +145306 -52026
- data/lib/branston/test/blueprints.rb +22 -28
- data/lib/branston/test/functional/iterations_controller_test.rb +149 -113
- data/lib/branston/test/functional/outcomes_controller_test.rb +94 -60
- data/lib/branston/test/functional/preconditions_controller_test.rb +101 -67
- data/lib/branston/test/functional/releases_controller_test.rb +85 -49
- data/lib/branston/test/functional/scenarios_controller_test.rb +104 -70
- data/lib/branston/test/functional/stories_controller_test.rb +41 -12
- data/lib/branston/test/functional/users_controller_test.rb +364 -43
- data/lib/branston/test/unit/iteration_test.rb +37 -6
- data/lib/branston/test/unit/outcome_test.rb +2 -2
- data/lib/branston/test/unit/participation_test.rb +2 -2
- data/lib/branston/test/unit/precondition_test.rb +3 -3
- data/lib/branston/test/unit/release_test.rb +4 -0
- data/lib/branston/test/unit/scenario_test.rb +4 -4
- data/lib/branston/test/unit/story_test.rb +62 -40
- data/lib/branston/test/unit/user_test.rb +195 -5
- metadata +136 -156
- data/lib/branston/app/controllers/user_roles_controller.rb +0 -105
- data/lib/branston/app/helpers/user_roles_helper.rb +0 -2
- data/lib/branston/app/models/user_role.rb +0 -21
- data/lib/branston/app/views/layouts/outcomes.html.erb +0 -17
- data/lib/branston/app/views/layouts/preconditions.html.erb +0 -17
- data/lib/branston/app/views/layouts/releases.html.erb +0 -17
- data/lib/branston/app/views/user_roles/edit.html.erb +0 -16
- data/lib/branston/app/views/user_roles/index.html.erb +0 -20
- data/lib/branston/app/views/user_roles/new.html.erb +0 -15
- data/lib/branston/app/views/user_roles/show.html.erb +0 -8
- data/lib/branston/coverage/app-controllers-application_controller_rb.html +0 -231
- data/lib/branston/coverage/app-controllers-iterations_controller_rb.html +0 -801
- data/lib/branston/coverage/app-controllers-outcomes_controller_rb.html +0 -759
- data/lib/branston/coverage/app-controllers-preconditions_controller_rb.html +0 -783
- data/lib/branston/coverage/app-controllers-releases_controller_rb.html +0 -705
- data/lib/branston/coverage/app-controllers-scenarios_controller_rb.html +0 -777
- data/lib/branston/coverage/app-controllers-sessions_controller_rb.html +0 -411
- data/lib/branston/coverage/app-controllers-stories_controller_rb.html +0 -1071
- data/lib/branston/coverage/app-controllers-user_roles_controller_rb.html +0 -693
- data/lib/branston/coverage/app-controllers-users_controller_rb.html +0 -315
- data/lib/branston/coverage/app-helpers-application_helper_rb.html +0 -327
- data/lib/branston/coverage/app-helpers-iterations_helper_rb.html +0 -363
- data/lib/branston/coverage/app-helpers-outcomes_helper_rb.html +0 -75
- data/lib/branston/coverage/app-helpers-preconditions_helper_rb.html +0 -75
- data/lib/branston/coverage/app-helpers-releases_helper_rb.html +0 -75
- data/lib/branston/coverage/app-helpers-sessions_helper_rb.html +0 -75
- data/lib/branston/coverage/app-helpers-stories_helper_rb.html +0 -75
- data/lib/branston/coverage/app-helpers-user_roles_helper_rb.html +0 -75
- data/lib/branston/coverage/app-models-iteration_rb.html +0 -321
- data/lib/branston/coverage/app-models-outcome_rb.html +0 -243
- data/lib/branston/coverage/app-models-participation_rb.html +0 -189
- data/lib/branston/coverage/app-models-precondition_rb.html +0 -243
- data/lib/branston/coverage/app-models-release_rb.html +0 -195
- data/lib/branston/coverage/app-models-scenario_rb.html +0 -231
- data/lib/branston/coverage/app-models-story_rb.html +0 -621
- data/lib/branston/coverage/app-models-user_rb.html +0 -513
- data/lib/branston/coverage/app-models-user_role_rb.html +0 -189
- data/lib/branston/coverage/index.html +0 -570
- data/lib/branston/coverage/jquery-1.3.2.min.js +0 -19
- data/lib/branston/coverage/jquery.tablesorter.min.js +0 -15
- data/lib/branston/coverage/lib-client_rb.html +0 -537
- data/lib/branston/coverage/lib-faker_extras_rb.html +0 -207
- data/lib/branston/coverage/lib-story_generator_rb.html +0 -873
- data/lib/branston/coverage/print.css +0 -12
- data/lib/branston/coverage/rcov.js +0 -42
- data/lib/branston/coverage/screen.css +0 -270
- data/lib/branston/db/migrate/20091127131037_create_user_roles.rb +0 -13
- data/lib/branston/db/migrate/20091127172950_add_story_id_to_user_role.rb +0 -10
- data/lib/branston/test/functional/user_roles_controller_test.rb +0 -71
- data/lib/branston/test/unit/helpers/user_roles_helper_test.rb +0 -4
- data/lib/branston/test/unit/user_role_test.rb +0 -9
- data/lib/branston/tmp/performance/BrowsingTest#test_homepage_process_time_flat.txt +0 -8
- data/lib/branston/tmp/performance/BrowsingTest#test_homepage_process_time_graph.html +0 -6718
- data/lib/branston/tmp/performance/BrowsingTest#test_homepage_process_time_tree.txt +0 -9942
- data/lib/branston/vendor/plugins/state_machine/CHANGELOG.rdoc +0 -298
- data/lib/branston/vendor/plugins/state_machine/LICENSE +0 -20
- data/lib/branston/vendor/plugins/state_machine/README.rdoc +0 -466
- data/lib/branston/vendor/plugins/state_machine/Rakefile +0 -98
- data/lib/branston/vendor/plugins/state_machine/examples/AutoShop_state.png +0 -0
- data/lib/branston/vendor/plugins/state_machine/examples/Car_state.png +0 -0
- data/lib/branston/vendor/plugins/state_machine/examples/TrafficLight_state.png +0 -0
- data/lib/branston/vendor/plugins/state_machine/examples/Vehicle_state.png +0 -0
- data/lib/branston/vendor/plugins/state_machine/examples/auto_shop.rb +0 -11
- data/lib/branston/vendor/plugins/state_machine/examples/car.rb +0 -19
- data/lib/branston/vendor/plugins/state_machine/examples/merb-rest/controller.rb +0 -51
- data/lib/branston/vendor/plugins/state_machine/examples/merb-rest/model.rb +0 -28
- data/lib/branston/vendor/plugins/state_machine/examples/merb-rest/view_edit.html.erb +0 -24
- data/lib/branston/vendor/plugins/state_machine/examples/merb-rest/view_index.html.erb +0 -23
- data/lib/branston/vendor/plugins/state_machine/examples/merb-rest/view_new.html.erb +0 -13
- data/lib/branston/vendor/plugins/state_machine/examples/merb-rest/view_show.html.erb +0 -17
- data/lib/branston/vendor/plugins/state_machine/examples/rails-rest/controller.rb +0 -43
- data/lib/branston/vendor/plugins/state_machine/examples/rails-rest/migration.rb +0 -11
- data/lib/branston/vendor/plugins/state_machine/examples/rails-rest/model.rb +0 -23
- data/lib/branston/vendor/plugins/state_machine/examples/rails-rest/view_edit.html.erb +0 -25
- data/lib/branston/vendor/plugins/state_machine/examples/rails-rest/view_index.html.erb +0 -23
- data/lib/branston/vendor/plugins/state_machine/examples/rails-rest/view_new.html.erb +0 -14
- data/lib/branston/vendor/plugins/state_machine/examples/rails-rest/view_show.html.erb +0 -17
- data/lib/branston/vendor/plugins/state_machine/examples/traffic_light.rb +0 -7
- data/lib/branston/vendor/plugins/state_machine/examples/vehicle.rb +0 -31
- data/lib/branston/vendor/plugins/state_machine/init.rb +0 -1
- data/lib/branston/vendor/plugins/state_machine/lib/state_machine.rb +0 -388
- data/lib/branston/vendor/plugins/state_machine/lib/state_machine/assertions.rb +0 -36
- data/lib/branston/vendor/plugins/state_machine/lib/state_machine/callback.rb +0 -189
- data/lib/branston/vendor/plugins/state_machine/lib/state_machine/condition_proxy.rb +0 -94
- data/lib/branston/vendor/plugins/state_machine/lib/state_machine/eval_helpers.rb +0 -67
- data/lib/branston/vendor/plugins/state_machine/lib/state_machine/event.rb +0 -252
- data/lib/branston/vendor/plugins/state_machine/lib/state_machine/event_collection.rb +0 -122
- data/lib/branston/vendor/plugins/state_machine/lib/state_machine/extensions.rb +0 -149
- data/lib/branston/vendor/plugins/state_machine/lib/state_machine/guard.rb +0 -230
- data/lib/branston/vendor/plugins/state_machine/lib/state_machine/integrations.rb +0 -68
- data/lib/branston/vendor/plugins/state_machine/lib/state_machine/integrations/active_record.rb +0 -492
- data/lib/branston/vendor/plugins/state_machine/lib/state_machine/integrations/active_record/locale.rb +0 -11
- data/lib/branston/vendor/plugins/state_machine/lib/state_machine/integrations/active_record/observer.rb +0 -41
- data/lib/branston/vendor/plugins/state_machine/lib/state_machine/integrations/data_mapper.rb +0 -351
- data/lib/branston/vendor/plugins/state_machine/lib/state_machine/integrations/data_mapper/observer.rb +0 -139
- data/lib/branston/vendor/plugins/state_machine/lib/state_machine/integrations/sequel.rb +0 -322
- data/lib/branston/vendor/plugins/state_machine/lib/state_machine/machine.rb +0 -1467
- data/lib/branston/vendor/plugins/state_machine/lib/state_machine/machine_collection.rb +0 -155
- data/lib/branston/vendor/plugins/state_machine/lib/state_machine/matcher.rb +0 -123
- data/lib/branston/vendor/plugins/state_machine/lib/state_machine/matcher_helpers.rb +0 -54
- data/lib/branston/vendor/plugins/state_machine/lib/state_machine/node_collection.rb +0 -152
- data/lib/branston/vendor/plugins/state_machine/lib/state_machine/state.rb +0 -249
- data/lib/branston/vendor/plugins/state_machine/lib/state_machine/state_collection.rb +0 -112
- data/lib/branston/vendor/plugins/state_machine/lib/state_machine/transition.rb +0 -394
- data/lib/branston/vendor/plugins/state_machine/state_machine.gemspec +0 -30
- data/lib/branston/vendor/plugins/state_machine/tasks/state_machine.rake +0 -1
- data/lib/branston/vendor/plugins/state_machine/tasks/state_machine.rb +0 -30
- data/lib/branston/vendor/plugins/state_machine/test/classes/switch.rb +0 -11
- data/lib/branston/vendor/plugins/state_machine/test/functional/state_machine_test.rb +0 -941
- data/lib/branston/vendor/plugins/state_machine/test/test_helper.rb +0 -4
- data/lib/branston/vendor/plugins/state_machine/test/unit/assertions_test.rb +0 -40
- data/lib/branston/vendor/plugins/state_machine/test/unit/callback_test.rb +0 -455
- data/lib/branston/vendor/plugins/state_machine/test/unit/condition_proxy_test.rb +0 -328
- data/lib/branston/vendor/plugins/state_machine/test/unit/eval_helpers_test.rb +0 -120
- data/lib/branston/vendor/plugins/state_machine/test/unit/event_collection_test.rb +0 -326
- data/lib/branston/vendor/plugins/state_machine/test/unit/event_test.rb +0 -743
- data/lib/branston/vendor/plugins/state_machine/test/unit/guard_test.rb +0 -908
- data/lib/branston/vendor/plugins/state_machine/test/unit/integrations/active_record_test.rb +0 -1367
- data/lib/branston/vendor/plugins/state_machine/test/unit/integrations/data_mapper_test.rb +0 -962
- data/lib/branston/vendor/plugins/state_machine/test/unit/integrations/sequel_test.rb +0 -859
- data/lib/branston/vendor/plugins/state_machine/test/unit/integrations_test.rb +0 -42
- data/lib/branston/vendor/plugins/state_machine/test/unit/invalid_event_test.rb +0 -7
- data/lib/branston/vendor/plugins/state_machine/test/unit/invalid_transition_test.rb +0 -7
- data/lib/branston/vendor/plugins/state_machine/test/unit/machine_collection_test.rb +0 -938
- data/lib/branston/vendor/plugins/state_machine/test/unit/machine_test.rb +0 -2004
- data/lib/branston/vendor/plugins/state_machine/test/unit/matcher_helpers_test.rb +0 -37
- data/lib/branston/vendor/plugins/state_machine/test/unit/matcher_test.rb +0 -155
- data/lib/branston/vendor/plugins/state_machine/test/unit/node_collection_test.rb +0 -207
- data/lib/branston/vendor/plugins/state_machine/test/unit/state_collection_test.rb +0 -280
- data/lib/branston/vendor/plugins/state_machine/test/unit/state_machine_test.rb +0 -31
- data/lib/branston/vendor/plugins/state_machine/test/unit/state_test.rb +0 -795
- data/lib/branston/vendor/plugins/state_machine/test/unit/transition_test.rb +0 -1212
@@ -1,122 +0,0 @@
|
|
1
|
-
module StateMachine
|
2
|
-
# Represents a collection of events in a state machine
|
3
|
-
class EventCollection < NodeCollection
|
4
|
-
def initialize(machine) #:nodoc:
|
5
|
-
super(machine, :index => [:name, :qualified_name])
|
6
|
-
end
|
7
|
-
|
8
|
-
# Gets the list of events that can be fired on the given object.
|
9
|
-
#
|
10
|
-
# == Examples
|
11
|
-
#
|
12
|
-
# class Vehicle
|
13
|
-
# state_machine :initial => :parked do
|
14
|
-
# event :park do
|
15
|
-
# transition :idling => :parked
|
16
|
-
# end
|
17
|
-
#
|
18
|
-
# event :ignite do
|
19
|
-
# transition :parked => :idling
|
20
|
-
# end
|
21
|
-
# end
|
22
|
-
# end
|
23
|
-
#
|
24
|
-
# events = Vehicle.state_machine(:state).events
|
25
|
-
#
|
26
|
-
# vehicle = Vehicle.new # => #<Vehicle:0xb7c464b0 @state="parked">
|
27
|
-
# events.valid_for(vehicle) # => [#<StateMachine::Event name=:ignite transitions=[:parked => :idling]>]
|
28
|
-
#
|
29
|
-
# vehicle.state = 'idling'
|
30
|
-
# events.valid_for(vehicle) # => [#<StateMachine::Event name=:park transitions=[:idling => :parked]>]
|
31
|
-
def valid_for(object)
|
32
|
-
select {|event| event.can_fire?(object)}
|
33
|
-
end
|
34
|
-
|
35
|
-
# Gets the list of transitions that can be run on the given object.
|
36
|
-
#
|
37
|
-
# Valid requirement options:
|
38
|
-
# * <tt>:from</tt> - One or more states being transitioned from. If none
|
39
|
-
# are specified, then this will be the object's current state.
|
40
|
-
# * <tt>:to</tt> - One or more states being transitioned to. If none are
|
41
|
-
# specified, then this will match any to state.
|
42
|
-
# * <tt>:on</tt> - One or more events that fire the transition. If none
|
43
|
-
# are specified, then this will match any event.
|
44
|
-
#
|
45
|
-
# == Examples
|
46
|
-
#
|
47
|
-
# class Vehicle
|
48
|
-
# state_machine :initial => :parked do
|
49
|
-
# event :park do
|
50
|
-
# transition :idling => :parked
|
51
|
-
# end
|
52
|
-
#
|
53
|
-
# event :ignite do
|
54
|
-
# transition :parked => :idling
|
55
|
-
# end
|
56
|
-
# end
|
57
|
-
# end
|
58
|
-
#
|
59
|
-
# events = Vehicle.state_machine.events
|
60
|
-
#
|
61
|
-
# vehicle = Vehicle.new # => #<Vehicle:0xb7c464b0 @state="parked">
|
62
|
-
# events.transitions_for(vehicle) # => [#<StateMachine::Transition attribute=:state event=:ignite from="parked" from_name=:parked to="idling" to_name=:idling>]
|
63
|
-
#
|
64
|
-
# vehicle.state = 'idling'
|
65
|
-
# events.transitions_for(vehicle) # => [#<StateMachine::Transition attribute=:state event=:park from="idling" from_name=:idling to="parked" to_name=:parked>]
|
66
|
-
#
|
67
|
-
# # Search for explicit transitions regardless of the current state
|
68
|
-
# events.transitions_for(vehicle, :from => :parked) # => [#<StateMachine::Transition attribute=:state event=:ignite from="parked" from_name=:parked to="idling" to_name=:idling>]
|
69
|
-
def transitions_for(object, requirements = {})
|
70
|
-
map {|event| event.transition_for(object, requirements)}.compact
|
71
|
-
end
|
72
|
-
|
73
|
-
# Gets the transition that should be performed for the event stored in the
|
74
|
-
# given object's event attribute. This also takes an additional parameter
|
75
|
-
# for automatically invalidating the object if the event or transition are
|
76
|
-
# invalid. By default, this is turned off.
|
77
|
-
#
|
78
|
-
# *Note* that if a transition has already been generated for the event, then
|
79
|
-
# that transition will be used.
|
80
|
-
#
|
81
|
-
# == Examples
|
82
|
-
#
|
83
|
-
# class Vehicle < ActiveRecord::Base
|
84
|
-
# state_machine :initial => :parked do
|
85
|
-
# event :ignite do
|
86
|
-
# transition :parked => :idling
|
87
|
-
# end
|
88
|
-
# end
|
89
|
-
# end
|
90
|
-
#
|
91
|
-
# vehicle = Vehicle.new # => #<Vehicle id: nil, state: "parked">
|
92
|
-
# events = Vehicle.state_machine.events
|
93
|
-
#
|
94
|
-
# vehicle.state_event = nil
|
95
|
-
# events.attribute_transition_for(vehicle) # => nil # Event isn't defined
|
96
|
-
#
|
97
|
-
# vehicle.state_event = 'invalid'
|
98
|
-
# events.attribute_transition_for(vehicle) # => false # Event is invalid
|
99
|
-
#
|
100
|
-
# vehicle.state_event = 'ignite'
|
101
|
-
# events.attribute_transition_for(vehicle) # => #<StateMachine::Transition attribute=:state event=:ignite from="parked" from_name=:parked to="idling" to_name=:idling>
|
102
|
-
def attribute_transition_for(object, invalidate = false)
|
103
|
-
return unless machine.action
|
104
|
-
|
105
|
-
result = machine.read(object, :event_transition) || if event_name = machine.read(object, :event)
|
106
|
-
if event = self[event_name.to_sym, :name]
|
107
|
-
event.transition_for(object) || begin
|
108
|
-
# No valid transition: invalidate
|
109
|
-
machine.invalidate(object, :event, :invalid_event, [[:state, machine.states.match!(object).name || 'nil']]) if invalidate
|
110
|
-
false
|
111
|
-
end
|
112
|
-
else
|
113
|
-
# Event is unknown: invalidate
|
114
|
-
machine.invalidate(object, :event, :invalid) if invalidate
|
115
|
-
false
|
116
|
-
end
|
117
|
-
end
|
118
|
-
|
119
|
-
result
|
120
|
-
end
|
121
|
-
end
|
122
|
-
end
|
@@ -1,149 +0,0 @@
|
|
1
|
-
require 'state_machine/machine_collection'
|
2
|
-
|
3
|
-
module StateMachine
|
4
|
-
module ClassMethods
|
5
|
-
def self.extended(base) #:nodoc:
|
6
|
-
base.class_eval do
|
7
|
-
@state_machines = MachineCollection.new
|
8
|
-
end
|
9
|
-
end
|
10
|
-
|
11
|
-
# Gets the current list of state machines defined for this class. This
|
12
|
-
# class-level attribute acts like an inheritable attribute. The attribute
|
13
|
-
# is available to each subclass, each having a copy of its superclass's
|
14
|
-
# attribute.
|
15
|
-
#
|
16
|
-
# The hash of state machines maps <tt>:attribute</tt> => +machine+, e.g.
|
17
|
-
#
|
18
|
-
# Vehicle.state_machines # => {:state => #<StateMachine::Machine:0xb6f6e4a4 ...>}
|
19
|
-
def state_machines
|
20
|
-
@state_machines ||= superclass.state_machines.dup
|
21
|
-
end
|
22
|
-
end
|
23
|
-
|
24
|
-
module InstanceMethods
|
25
|
-
# Runs one or more events in parallel. All events will run through the
|
26
|
-
# following steps:
|
27
|
-
# * Before callbacks
|
28
|
-
# * Persist state
|
29
|
-
# * Invoke action
|
30
|
-
# * After callbacks
|
31
|
-
#
|
32
|
-
# For example, if two events (for state machines A and B) are run in
|
33
|
-
# parallel, the order in which steps are run is:
|
34
|
-
# * A - Before transition callbacks
|
35
|
-
# * B - Before transition callbacks
|
36
|
-
# * A - Persist new state
|
37
|
-
# * B - Persist new state
|
38
|
-
# * A - Invoke action
|
39
|
-
# * B - Invoke action (only if different than A's action)
|
40
|
-
# * A - After transition callbacks
|
41
|
-
# * B - After transition callbacks
|
42
|
-
#
|
43
|
-
# *Note* that multiple events on the same state machine / attribute cannot
|
44
|
-
# be run in parallel. If this is attempted, an ArgumentError will be
|
45
|
-
# raised.
|
46
|
-
#
|
47
|
-
# == Halting callbacks
|
48
|
-
#
|
49
|
-
# When running multiple events in parallel, special consideration should
|
50
|
-
# be taken with regard to how halting within callbacks affects the flow.
|
51
|
-
#
|
52
|
-
# For *before* callbacks, any <tt>:halt</tt> error that's thrown will
|
53
|
-
# immediately cancel the perform for all transitions. As a result, it's
|
54
|
-
# possible for one event's transition to affect the continuation of
|
55
|
-
# another.
|
56
|
-
#
|
57
|
-
# On the other hand, any <tt>:halt</tt> error that's thrown within an
|
58
|
-
# *after* callback with only affect that event's transition. Other
|
59
|
-
# transitions will continue to run their own callbacks.
|
60
|
-
#
|
61
|
-
# == Example
|
62
|
-
#
|
63
|
-
# class Vehicle
|
64
|
-
# state_machine :initial => :parked do
|
65
|
-
# event :ignite do
|
66
|
-
# transition :parked => :idling
|
67
|
-
# end
|
68
|
-
#
|
69
|
-
# event :park do
|
70
|
-
# transition :idling => :parked
|
71
|
-
# end
|
72
|
-
# end
|
73
|
-
#
|
74
|
-
# state_machine :alarm_state, :namespace => 'alarm', :initial => :on do
|
75
|
-
# event :enable do
|
76
|
-
# transition all => :active
|
77
|
-
# end
|
78
|
-
#
|
79
|
-
# event :disable do
|
80
|
-
# transition all => :off
|
81
|
-
# end
|
82
|
-
# end
|
83
|
-
# end
|
84
|
-
#
|
85
|
-
# vehicle = Vehicle.new # => #<Vehicle:0xb7c02850 @state="parked", @alarm_state="active">
|
86
|
-
# vehicle.state # => "parked"
|
87
|
-
# vehicle.alarm_state # => "active"
|
88
|
-
#
|
89
|
-
# vehicle.fire_events(:ignite, :disable_alarm) # => true
|
90
|
-
# vehicle.state # => "idling"
|
91
|
-
# vehicle.alarm_state # => "off"
|
92
|
-
#
|
93
|
-
# # If any event fails, the entire event chain fails
|
94
|
-
# vehicle.fire_events(:ignite, :enable_alarm) # => false
|
95
|
-
# vehicle.state # => "idling"
|
96
|
-
# vehicle.alarm_state # => "off"
|
97
|
-
#
|
98
|
-
# # Exception raised on invalid event
|
99
|
-
# vehicle.fire_events(:park, :invalid) # => StateMachine::InvalidEvent: :invalid is an unknown event
|
100
|
-
# vehicle.state # => "idling"
|
101
|
-
# vehicle.alarm_state # => "off"
|
102
|
-
def fire_events(*events)
|
103
|
-
self.class.state_machines.fire_events(self, *events)
|
104
|
-
end
|
105
|
-
|
106
|
-
# Run one or more events in parallel. If any event fails to run, then
|
107
|
-
# a StateMachine::InvalidTransition exception will be raised.
|
108
|
-
#
|
109
|
-
# See StateMachine::InstanceMethods#fire_events for more information.
|
110
|
-
#
|
111
|
-
# == Example
|
112
|
-
#
|
113
|
-
# class Vehicle
|
114
|
-
# state_machine :initial => :parked do
|
115
|
-
# event :ignite do
|
116
|
-
# transition :parked => :idling
|
117
|
-
# end
|
118
|
-
#
|
119
|
-
# event :park do
|
120
|
-
# transition :idling => :parked
|
121
|
-
# end
|
122
|
-
# end
|
123
|
-
#
|
124
|
-
# state_machine :alarm_state, :namespace => 'alarm', :initial => :active do
|
125
|
-
# event :enable do
|
126
|
-
# transition all => :active
|
127
|
-
# end
|
128
|
-
#
|
129
|
-
# event :disable do
|
130
|
-
# transition all => :off
|
131
|
-
# end
|
132
|
-
# end
|
133
|
-
# end
|
134
|
-
#
|
135
|
-
# vehicle = Vehicle.new # => #<Vehicle:0xb7c02850 @state="parked", @alarm_state="active">
|
136
|
-
# vehicle.fire_events(:ignite, :disable_alarm) # => true
|
137
|
-
#
|
138
|
-
# vehicle.fire_events!(:ignite, :disable_alarm) # => StateMachine::InvalidTranstion: Cannot run events in parallel: ignite, disable_alarm
|
139
|
-
def fire_events!(*events)
|
140
|
-
run_action = [true, false].include?(events.last) ? events.pop : true
|
141
|
-
fire_events(*(events + [run_action])) || raise(StateMachine::InvalidTransition, "Cannot run events in parallel: #{events * ', '}")
|
142
|
-
end
|
143
|
-
|
144
|
-
protected
|
145
|
-
def initialize_state_machines(options = {}) #:nodoc:
|
146
|
-
self.class.state_machines.initialize_states(self, options)
|
147
|
-
end
|
148
|
-
end
|
149
|
-
end
|
@@ -1,230 +0,0 @@
|
|
1
|
-
require 'state_machine/matcher'
|
2
|
-
require 'state_machine/eval_helpers'
|
3
|
-
require 'state_machine/assertions'
|
4
|
-
|
5
|
-
module StateMachine
|
6
|
-
# Represents a set of requirements that must be met in order for a transition
|
7
|
-
# or callback to occur. Guards verify that the event, from state, and to
|
8
|
-
# state of the transition match, in addition to if/unless conditionals for
|
9
|
-
# an object's state.
|
10
|
-
class Guard
|
11
|
-
include Assertions
|
12
|
-
include EvalHelpers
|
13
|
-
|
14
|
-
# The condition that must be met on an object
|
15
|
-
attr_reader :if_condition
|
16
|
-
|
17
|
-
# The condition that must *not* be met on an object
|
18
|
-
attr_reader :unless_condition
|
19
|
-
|
20
|
-
# The requirement for verifying the event being guarded
|
21
|
-
attr_reader :event_requirement
|
22
|
-
|
23
|
-
# One or more requirements for verifying the states being guarded. All
|
24
|
-
# requirements contain a mapping of {:from => matcher, :to => matcher}.
|
25
|
-
attr_reader :state_requirements
|
26
|
-
|
27
|
-
# The requirement for verifying the success of the event
|
28
|
-
attr_reader :success_requirement
|
29
|
-
|
30
|
-
# A list of all of the states known to this guard. This will pull states
|
31
|
-
# from the following options (in the same order):
|
32
|
-
# * +from+ / +except_from+
|
33
|
-
# * +to+ / +except_to+
|
34
|
-
attr_reader :known_states
|
35
|
-
|
36
|
-
# Creates a new guard
|
37
|
-
def initialize(options = {}) #:nodoc:
|
38
|
-
# Build conditionals
|
39
|
-
@if_condition = options.delete(:if)
|
40
|
-
@unless_condition = options.delete(:unless)
|
41
|
-
|
42
|
-
# Build event requirement
|
43
|
-
@event_requirement = build_matcher(options, :on, :except_on)
|
44
|
-
|
45
|
-
# Build success requirement
|
46
|
-
@success_requirement = options.delete(:include_failures) ? AllMatcher.instance : WhitelistMatcher.new([true])
|
47
|
-
|
48
|
-
if (options.keys - [:from, :to, :on, :except_from, :except_to, :except_on]).empty?
|
49
|
-
# Explicit from/to requirements specified
|
50
|
-
@state_requirements = [{:from => build_matcher(options, :from, :except_from), :to => build_matcher(options, :to, :except_to)}]
|
51
|
-
else
|
52
|
-
# Separate out the event requirement
|
53
|
-
options.delete(:on)
|
54
|
-
options.delete(:except_on)
|
55
|
-
|
56
|
-
# Implicit from/to requirements specified
|
57
|
-
@state_requirements = options.collect do |from, to|
|
58
|
-
from = WhitelistMatcher.new(from) unless from.is_a?(Matcher)
|
59
|
-
to = WhitelistMatcher.new(to) unless to.is_a?(Matcher)
|
60
|
-
{:from => from, :to => to}
|
61
|
-
end
|
62
|
-
end
|
63
|
-
|
64
|
-
# Track known states. The order that requirements are iterated is based
|
65
|
-
# on the priority in which tracked states should be added.
|
66
|
-
@known_states = []
|
67
|
-
@state_requirements.each do |state_requirement|
|
68
|
-
[:from, :to].each {|option| @known_states |= state_requirement[option].values}
|
69
|
-
end
|
70
|
-
end
|
71
|
-
|
72
|
-
# Determines whether the given object / query matches the requirements
|
73
|
-
# configured for this guard. In addition to matching the event, from state,
|
74
|
-
# and to state, this will also check whether the configured :if/:unless
|
75
|
-
# conditions pass on the given object.
|
76
|
-
#
|
77
|
-
# == Examples
|
78
|
-
#
|
79
|
-
# guard = StateMachine::Guard.new(:parked => :idling, :on => :ignite)
|
80
|
-
#
|
81
|
-
# # Successful
|
82
|
-
# guard.matches?(object, :on => :ignite) # => true
|
83
|
-
# guard.matches?(object, :from => nil) # => true
|
84
|
-
# guard.matches?(object, :from => :parked) # => true
|
85
|
-
# guard.matches?(object, :to => :idling) # => true
|
86
|
-
# guard.matches?(object, :from => :parked, :to => :idling) # => true
|
87
|
-
# guard.matches?(object, :on => :ignite, :from => :parked, :to => :idling) # => true
|
88
|
-
#
|
89
|
-
# # Unsuccessful
|
90
|
-
# guard.matches?(object, :on => :park) # => false
|
91
|
-
# guard.matches?(object, :from => :idling) # => false
|
92
|
-
# guard.matches?(object, :to => :first_gear) # => false
|
93
|
-
# guard.matches?(object, :from => :parked, :to => :first_gear) # => false
|
94
|
-
# guard.matches?(object, :on => :park, :from => :parked, :to => :idling) # => false
|
95
|
-
def matches?(object, query = {})
|
96
|
-
!match(object, query).nil?
|
97
|
-
end
|
98
|
-
|
99
|
-
# Attempts to match the given object / query against the set of requirements
|
100
|
-
# configured for this guard. In addition to matching the event, from state,
|
101
|
-
# and to state, this will also check whether the configured :if/:unless
|
102
|
-
# conditions pass on the given object.
|
103
|
-
#
|
104
|
-
# If a match is found, then the event/state requirements that the query
|
105
|
-
# passed successfully will be returned. Otherwise, nil is returned if there
|
106
|
-
# was no match.
|
107
|
-
#
|
108
|
-
# Query options:
|
109
|
-
# * <tt>:from</tt> - One or more states being transitioned from. If none
|
110
|
-
# are specified, then this will always match.
|
111
|
-
# * <tt>:to</tt> - One or more states being transitioned to. If none are
|
112
|
-
# specified, then this will always match.
|
113
|
-
# * <tt>:on</tt> - One or more events that fired the transition. If none
|
114
|
-
# are specified, then this will always match.
|
115
|
-
#
|
116
|
-
# == Examples
|
117
|
-
#
|
118
|
-
# guard = StateMachine::Guard.new(:parked => :idling, :on => :ignite)
|
119
|
-
#
|
120
|
-
# guard.match(object, :on => :ignite) # => {:to => ..., :from => ..., :on => ...}
|
121
|
-
# guard.match(object, :on => :park) # => nil
|
122
|
-
def match(object, query = {})
|
123
|
-
if (match = match_query(query)) && matches_conditions?(object)
|
124
|
-
match
|
125
|
-
end
|
126
|
-
end
|
127
|
-
|
128
|
-
# Draws a representation of this guard on the given graph. This will draw
|
129
|
-
# an edge between every state this guard matches *from* to either the
|
130
|
-
# configured to state or, if none specified, then a loopback to the from
|
131
|
-
# state.
|
132
|
-
#
|
133
|
-
# For example, if the following from states are configured:
|
134
|
-
# * +idling+
|
135
|
-
# * +first_gear+
|
136
|
-
# * +backing_up+
|
137
|
-
#
|
138
|
-
# ...and the to state is +parked+, then the following edges will be created:
|
139
|
-
# * +idling+ -> +parked+
|
140
|
-
# * +first_gear+ -> +parked+
|
141
|
-
# * +backing_up+ -> +parked+
|
142
|
-
#
|
143
|
-
# Each edge will be labeled with the name of the event that would cause the
|
144
|
-
# transition.
|
145
|
-
#
|
146
|
-
# The collection of edges generated on the graph will be returned.
|
147
|
-
def draw(graph, event, valid_states)
|
148
|
-
state_requirements.inject([]) do |edges, state_requirement|
|
149
|
-
# From states determined based on the known valid states
|
150
|
-
from_states = state_requirement[:from].filter(valid_states)
|
151
|
-
|
152
|
-
# If a to state is not specified, then it's a loopback and each from
|
153
|
-
# state maps back to itself
|
154
|
-
if state_requirement[:to].values.empty?
|
155
|
-
loopback = true
|
156
|
-
else
|
157
|
-
to_state = state_requirement[:to].values.first
|
158
|
-
to_state = to_state ? to_state.to_s : 'nil'
|
159
|
-
loopback = false
|
160
|
-
end
|
161
|
-
|
162
|
-
# Generate an edge between each from and to state
|
163
|
-
from_states.each do |from_state|
|
164
|
-
from_state = from_state ? from_state.to_s : 'nil'
|
165
|
-
edges << graph.add_edge(from_state, loopback ? from_state : to_state, :label => event.to_s)
|
166
|
-
end
|
167
|
-
|
168
|
-
edges
|
169
|
-
end
|
170
|
-
end
|
171
|
-
|
172
|
-
protected
|
173
|
-
# Builds a matcher strategy to use for the given options. If neither a
|
174
|
-
# whitelist nor a blacklist option is specified, then an AllMatcher is
|
175
|
-
# built.
|
176
|
-
def build_matcher(options, whitelist_option, blacklist_option)
|
177
|
-
assert_exclusive_keys(options, whitelist_option, blacklist_option)
|
178
|
-
|
179
|
-
if options.include?(whitelist_option)
|
180
|
-
WhitelistMatcher.new(options[whitelist_option])
|
181
|
-
elsif options.include?(blacklist_option)
|
182
|
-
BlacklistMatcher.new(options[blacklist_option])
|
183
|
-
else
|
184
|
-
AllMatcher.instance
|
185
|
-
end
|
186
|
-
end
|
187
|
-
|
188
|
-
# Verifies that all configured requirements (event and state) match the
|
189
|
-
# given query. If a match is found, then a hash containing the
|
190
|
-
# event/state requirements that passed will be returned; otherwise, nil.
|
191
|
-
def match_query(query)
|
192
|
-
query ||= {}
|
193
|
-
|
194
|
-
if match_success(query) && match_event(query) && (state_requirement = match_states(query))
|
195
|
-
state_requirement.merge(:on => event_requirement)
|
196
|
-
end
|
197
|
-
end
|
198
|
-
|
199
|
-
# Verifies that the success requirement matches the given query
|
200
|
-
def match_success(query)
|
201
|
-
matches_requirement?(query, :success, success_requirement)
|
202
|
-
end
|
203
|
-
|
204
|
-
# Verifies that the event requirement matches the given query
|
205
|
-
def match_event(query)
|
206
|
-
matches_requirement?(query, :on, event_requirement)
|
207
|
-
end
|
208
|
-
|
209
|
-
# Verifies that the state requirements match the given query. If a
|
210
|
-
# matching requirement is found, then it is returned.
|
211
|
-
def match_states(query)
|
212
|
-
state_requirements.detect do |state_requirement|
|
213
|
-
[:from, :to].all? {|option| matches_requirement?(query, option, state_requirement[option])}
|
214
|
-
end
|
215
|
-
end
|
216
|
-
|
217
|
-
# Verifies that an option in the given query matches the values required
|
218
|
-
# for that option
|
219
|
-
def matches_requirement?(query, option, requirement)
|
220
|
-
!query.include?(option) || requirement.matches?(query[option], query)
|
221
|
-
end
|
222
|
-
|
223
|
-
# Verifies that the conditionals for this guard evaluate to true for the
|
224
|
-
# given object
|
225
|
-
def matches_conditions?(object)
|
226
|
-
Array(if_condition).all? {|condition| evaluate_method(object, condition)} &&
|
227
|
-
!Array(unless_condition).any? {|condition| evaluate_method(object, condition)}
|
228
|
-
end
|
229
|
-
end
|
230
|
-
end
|