branston 0.3.6 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/branston/app/controllers/scenarios_controller.rb +6 -5
- data/lib/branston/app/controllers/stories_controller.rb +101 -89
- data/lib/branston/app/models/story.rb +30 -1
- data/lib/branston/app/models/user.rb +4 -0
- data/lib/branston/app/views/iterations/index.html.erb +1 -1
- data/lib/branston/app/views/layouts/_header.html.erb +1 -2
- data/lib/branston/app/views/scenarios/_scenario.html.erb +6 -3
- data/lib/branston/app/views/scenarios/_scenarios.html.erb +4 -2
- data/lib/branston/app/views/stories/_form.html.erb +15 -4
- data/lib/branston/app/views/stories/_story.html.erb +26 -6
- data/lib/branston/app/views/stories/edit.html.erb +3 -3
- data/lib/branston/app/views/stories/index.html.erb +22 -3
- data/lib/branston/app/views/stories/new.html.erb +2 -2
- data/lib/branston/app/views/stories/show.html.erb +3 -3
- data/lib/branston/config/routes.rb +7 -4
- data/lib/branston/coverage/app-controllers-application_controller_rb.html +1 -1
- data/lib/branston/coverage/app-controllers-iterations_controller_rb.html +1 -1
- data/lib/branston/coverage/app-controllers-outcomes_controller_rb.html +1 -1
- data/lib/branston/coverage/app-controllers-preconditions_controller_rb.html +1 -1
- data/lib/branston/coverage/app-controllers-releases_controller_rb.html +1 -1
- data/lib/branston/coverage/app-controllers-scenarios_controller_rb.html +18 -12
- data/lib/branston/coverage/app-controllers-sessions_controller_rb.html +1 -1
- data/lib/branston/coverage/app-controllers-stories_controller_rb.html +193 -121
- data/lib/branston/coverage/app-controllers-user_roles_controller_rb.html +1 -1
- data/lib/branston/coverage/app-controllers-users_controller_rb.html +1 -1
- data/lib/branston/coverage/app-helpers-application_helper_rb.html +1 -1
- data/lib/branston/coverage/app-helpers-iterations_helper_rb.html +1 -1
- data/lib/branston/coverage/app-helpers-outcomes_helper_rb.html +1 -1
- data/lib/branston/coverage/app-helpers-preconditions_helper_rb.html +1 -1
- data/lib/branston/coverage/app-helpers-releases_helper_rb.html +1 -1
- data/lib/branston/coverage/app-helpers-sessions_helper_rb.html +1 -1
- data/lib/branston/coverage/app-helpers-stories_helper_rb.html +1 -1
- data/lib/branston/coverage/app-helpers-user_roles_helper_rb.html +1 -1
- data/lib/branston/coverage/app-models-iteration_rb.html +1 -1
- data/lib/branston/coverage/app-models-outcome_rb.html +1 -1
- data/lib/branston/coverage/app-models-participation_rb.html +1 -1
- data/lib/branston/coverage/app-models-precondition_rb.html +1 -1
- data/lib/branston/coverage/app-models-release_rb.html +1 -1
- data/lib/branston/coverage/app-models-scenario_rb.html +1 -1
- data/lib/branston/coverage/app-models-story_rb.html +192 -18
- data/lib/branston/coverage/app-models-user_rb.html +33 -9
- data/lib/branston/coverage/app-models-user_role_rb.html +1 -1
- data/lib/branston/coverage/index.html +13 -13
- data/lib/branston/coverage/lib-client_rb.html +1 -1
- data/lib/branston/coverage/lib-faker_extras_rb.html +1 -1
- data/lib/branston/coverage/lib-story_generator_rb.html +1 -1
- data/lib/branston/db/development.sqlite3 +0 -0
- data/lib/branston/db/migrate/20091223100903_add_status_to_story.rb +11 -0
- data/lib/branston/db/pristine.sqlite3 +0 -0
- data/lib/branston/db/schema.rb +5 -3
- data/lib/branston/db/test.sqlite3 +0 -0
- data/lib/branston/lib/branston.rb +4 -2
- data/lib/branston/log/development.log +4970 -0
- data/lib/branston/log/test.log +88225 -0
- data/lib/branston/test/blueprints.rb +10 -7
- data/lib/branston/test/functional/scenarios_controller_test.rb +22 -15
- data/lib/branston/test/functional/stories_controller_test.rb +51 -30
- data/lib/branston/test/unit/story_test.rb +47 -7
- data/lib/branston/test/unit/user_test.rb +4 -0
- data/lib/branston/tmp/performance/BrowsingTest#test_homepage_process_time_flat.txt +3 -2
- data/lib/branston/tmp/performance/BrowsingTest#test_homepage_process_time_graph.html +2041 -1307
- data/lib/branston/tmp/performance/BrowsingTest#test_homepage_process_time_tree.txt +7922 -7922
- data/lib/branston/vendor/plugins/state_machine/CHANGELOG.rdoc +298 -0
- data/lib/branston/vendor/plugins/state_machine/LICENSE +20 -0
- data/lib/branston/vendor/plugins/state_machine/README.rdoc +466 -0
- data/lib/branston/vendor/plugins/state_machine/Rakefile +98 -0
- 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 +11 -0
- data/lib/branston/vendor/plugins/state_machine/examples/car.rb +19 -0
- data/lib/branston/vendor/plugins/state_machine/examples/merb-rest/controller.rb +51 -0
- data/lib/branston/vendor/plugins/state_machine/examples/merb-rest/model.rb +28 -0
- data/lib/branston/vendor/plugins/state_machine/examples/merb-rest/view_edit.html.erb +24 -0
- data/lib/branston/vendor/plugins/state_machine/examples/merb-rest/view_index.html.erb +23 -0
- data/lib/branston/vendor/plugins/state_machine/examples/merb-rest/view_new.html.erb +13 -0
- data/lib/branston/vendor/plugins/state_machine/examples/merb-rest/view_show.html.erb +17 -0
- data/lib/branston/vendor/plugins/state_machine/examples/rails-rest/controller.rb +43 -0
- data/lib/branston/vendor/plugins/state_machine/examples/rails-rest/migration.rb +11 -0
- data/lib/branston/vendor/plugins/state_machine/examples/rails-rest/model.rb +23 -0
- data/lib/branston/vendor/plugins/state_machine/examples/rails-rest/view_edit.html.erb +25 -0
- data/lib/branston/vendor/plugins/state_machine/examples/rails-rest/view_index.html.erb +23 -0
- data/lib/branston/vendor/plugins/state_machine/examples/rails-rest/view_new.html.erb +14 -0
- data/lib/branston/vendor/plugins/state_machine/examples/rails-rest/view_show.html.erb +17 -0
- data/lib/branston/vendor/plugins/state_machine/examples/traffic_light.rb +7 -0
- data/lib/branston/vendor/plugins/state_machine/examples/vehicle.rb +31 -0
- data/lib/branston/vendor/plugins/state_machine/init.rb +1 -0
- data/lib/branston/vendor/plugins/state_machine/lib/state_machine/assertions.rb +36 -0
- data/lib/branston/vendor/plugins/state_machine/lib/state_machine/callback.rb +189 -0
- data/lib/branston/vendor/plugins/state_machine/lib/state_machine/condition_proxy.rb +94 -0
- data/lib/branston/vendor/plugins/state_machine/lib/state_machine/eval_helpers.rb +67 -0
- data/lib/branston/vendor/plugins/state_machine/lib/state_machine/event.rb +252 -0
- data/lib/branston/vendor/plugins/state_machine/lib/state_machine/event_collection.rb +122 -0
- data/lib/branston/vendor/plugins/state_machine/lib/state_machine/extensions.rb +149 -0
- data/lib/branston/vendor/plugins/state_machine/lib/state_machine/guard.rb +230 -0
- data/lib/branston/vendor/plugins/state_machine/lib/state_machine/integrations/active_record/locale.rb +11 -0
- data/lib/branston/vendor/plugins/state_machine/lib/state_machine/integrations/active_record/observer.rb +41 -0
- data/lib/branston/vendor/plugins/state_machine/lib/state_machine/integrations/active_record.rb +492 -0
- data/lib/branston/vendor/plugins/state_machine/lib/state_machine/integrations/data_mapper/observer.rb +139 -0
- data/lib/branston/vendor/plugins/state_machine/lib/state_machine/integrations/data_mapper.rb +351 -0
- data/lib/branston/vendor/plugins/state_machine/lib/state_machine/integrations/sequel.rb +322 -0
- data/lib/branston/vendor/plugins/state_machine/lib/state_machine/integrations.rb +68 -0
- data/lib/branston/vendor/plugins/state_machine/lib/state_machine/machine.rb +1467 -0
- data/lib/branston/vendor/plugins/state_machine/lib/state_machine/machine_collection.rb +155 -0
- data/lib/branston/vendor/plugins/state_machine/lib/state_machine/matcher.rb +123 -0
- data/lib/branston/vendor/plugins/state_machine/lib/state_machine/matcher_helpers.rb +54 -0
- data/lib/branston/vendor/plugins/state_machine/lib/state_machine/node_collection.rb +152 -0
- data/lib/branston/vendor/plugins/state_machine/lib/state_machine/state.rb +249 -0
- data/lib/branston/vendor/plugins/state_machine/lib/state_machine/state_collection.rb +112 -0
- data/lib/branston/vendor/plugins/state_machine/lib/state_machine/transition.rb +394 -0
- data/lib/branston/vendor/plugins/state_machine/lib/state_machine.rb +388 -0
- data/lib/branston/vendor/plugins/state_machine/state_machine.gemspec +30 -0
- data/lib/branston/vendor/plugins/state_machine/tasks/state_machine.rake +1 -0
- data/lib/branston/vendor/plugins/state_machine/tasks/state_machine.rb +30 -0
- data/lib/branston/vendor/plugins/state_machine/test/classes/switch.rb +11 -0
- data/lib/branston/vendor/plugins/state_machine/test/functional/state_machine_test.rb +941 -0
- data/lib/branston/vendor/plugins/state_machine/test/test_helper.rb +4 -0
- data/lib/branston/vendor/plugins/state_machine/test/unit/assertions_test.rb +40 -0
- data/lib/branston/vendor/plugins/state_machine/test/unit/callback_test.rb +455 -0
- data/lib/branston/vendor/plugins/state_machine/test/unit/condition_proxy_test.rb +328 -0
- data/lib/branston/vendor/plugins/state_machine/test/unit/eval_helpers_test.rb +120 -0
- data/lib/branston/vendor/plugins/state_machine/test/unit/event_collection_test.rb +326 -0
- data/lib/branston/vendor/plugins/state_machine/test/unit/event_test.rb +743 -0
- data/lib/branston/vendor/plugins/state_machine/test/unit/guard_test.rb +908 -0
- data/lib/branston/vendor/plugins/state_machine/test/unit/integrations/active_record_test.rb +1367 -0
- data/lib/branston/vendor/plugins/state_machine/test/unit/integrations/data_mapper_test.rb +962 -0
- data/lib/branston/vendor/plugins/state_machine/test/unit/integrations/sequel_test.rb +859 -0
- data/lib/branston/vendor/plugins/state_machine/test/unit/integrations_test.rb +42 -0
- data/lib/branston/vendor/plugins/state_machine/test/unit/invalid_event_test.rb +7 -0
- data/lib/branston/vendor/plugins/state_machine/test/unit/invalid_transition_test.rb +7 -0
- data/lib/branston/vendor/plugins/state_machine/test/unit/machine_collection_test.rb +938 -0
- data/lib/branston/vendor/plugins/state_machine/test/unit/machine_test.rb +2004 -0
- data/lib/branston/vendor/plugins/state_machine/test/unit/matcher_helpers_test.rb +37 -0
- data/lib/branston/vendor/plugins/state_machine/test/unit/matcher_test.rb +155 -0
- data/lib/branston/vendor/plugins/state_machine/test/unit/node_collection_test.rb +207 -0
- data/lib/branston/vendor/plugins/state_machine/test/unit/state_collection_test.rb +280 -0
- data/lib/branston/vendor/plugins/state_machine/test/unit/state_machine_test.rb +31 -0
- data/lib/branston/vendor/plugins/state_machine/test/unit/state_test.rb +795 -0
- data/lib/branston/vendor/plugins/state_machine/test/unit/transition_test.rb +1212 -0
- metadata +81 -2
@@ -0,0 +1,252 @@
|
|
1
|
+
require 'state_machine/transition'
|
2
|
+
require 'state_machine/guard'
|
3
|
+
require 'state_machine/assertions'
|
4
|
+
require 'state_machine/matcher_helpers'
|
5
|
+
|
6
|
+
module StateMachine
|
7
|
+
# An invalid event was specified
|
8
|
+
class InvalidEvent < StandardError
|
9
|
+
end
|
10
|
+
|
11
|
+
# An event defines an action that transitions an attribute from one state to
|
12
|
+
# another. The state that an attribute is transitioned to depends on the
|
13
|
+
# guards configured for the event.
|
14
|
+
class Event
|
15
|
+
include Assertions
|
16
|
+
include MatcherHelpers
|
17
|
+
|
18
|
+
# The state machine for which this event is defined
|
19
|
+
attr_accessor :machine
|
20
|
+
|
21
|
+
# The name of the event
|
22
|
+
attr_reader :name
|
23
|
+
|
24
|
+
# The fully-qualified name of the event, scoped by the machine's namespace
|
25
|
+
attr_reader :qualified_name
|
26
|
+
|
27
|
+
# The list of guards that determine what state this event transitions
|
28
|
+
# objects to when fired
|
29
|
+
attr_reader :guards
|
30
|
+
|
31
|
+
# A list of all of the states known to this event using the configured
|
32
|
+
# guards/transitions as the source
|
33
|
+
attr_reader :known_states
|
34
|
+
|
35
|
+
# Creates a new event within the context of the given machine
|
36
|
+
def initialize(machine, name) #:nodoc:
|
37
|
+
@machine = machine
|
38
|
+
@name = name
|
39
|
+
@qualified_name = machine.namespace ? :"#{name}_#{machine.namespace}" : name
|
40
|
+
@guards = []
|
41
|
+
@known_states = []
|
42
|
+
|
43
|
+
add_actions
|
44
|
+
end
|
45
|
+
|
46
|
+
# Creates a copy of this event in addition to the list of associated
|
47
|
+
# guards to prevent conflicts across events within a class hierarchy.
|
48
|
+
def initialize_copy(orig) #:nodoc:
|
49
|
+
super
|
50
|
+
@guards = @guards.dup
|
51
|
+
@known_states = @known_states.dup
|
52
|
+
end
|
53
|
+
|
54
|
+
# Creates a new transition that determines what to change the current state
|
55
|
+
# to when this event fires.
|
56
|
+
#
|
57
|
+
# == Defining transitions
|
58
|
+
#
|
59
|
+
# The options for a new transition uses the Hash syntax to map beginning
|
60
|
+
# states to ending states. For example,
|
61
|
+
#
|
62
|
+
# transition :parked => :idling, :idling => :first_gear
|
63
|
+
#
|
64
|
+
# In this case, when the event is fired, this transition will cause the
|
65
|
+
# state to be +idling+ if it's current state is +parked+ or +first_gear+
|
66
|
+
# if it's current state is +idling+.
|
67
|
+
#
|
68
|
+
# To help define these implicit transitions, a set of helpers are available
|
69
|
+
# for slightly more complex matching:
|
70
|
+
# * <tt>all</tt> - Matches every state in the machine
|
71
|
+
# * <tt>all - [:parked, :idling, ...]</tt> - Matches every state except those specified
|
72
|
+
# * <tt>any</tt> - An alias for +all+ (matches every state in the machine)
|
73
|
+
# * <tt>same</tt> - Matches the same state being transitioned from
|
74
|
+
#
|
75
|
+
# See StateMachine::MatcherHelpers for more information.
|
76
|
+
#
|
77
|
+
# Examples:
|
78
|
+
#
|
79
|
+
# transition all => nil # Transitions to nil regardless of the current state
|
80
|
+
# transition all => :idling # Transitions to :idling regardless of the current state
|
81
|
+
# transition all - [:idling, :first_gear] => :idling # Transitions every state but :idling and :first_gear to :idling
|
82
|
+
# transition nil => :idling # Transitions to :idling from the nil state
|
83
|
+
# transition :parked => :idling # Transitions to :idling if :parked
|
84
|
+
# transition [:parked, :stalled] => :idling # Transitions to :idling if :parked or :stalled
|
85
|
+
#
|
86
|
+
# transition :parked => same # Loops :parked back to :parked
|
87
|
+
# transition [:parked, :stalled] => same # Loops either :parked or :stalled back to the same state
|
88
|
+
# transition all - :parked => same # Loops every state but :parked back to the same state
|
89
|
+
#
|
90
|
+
# == Verbose transitions
|
91
|
+
#
|
92
|
+
# Transitions can also be defined use an explicit set of deprecated
|
93
|
+
# configuration options:
|
94
|
+
# * <tt>:from</tt> - A state or array of states that can be transitioned from.
|
95
|
+
# If not specified, then the transition can occur for *any* state.
|
96
|
+
# * <tt>:to</tt> - The state that's being transitioned to. If not specified,
|
97
|
+
# then the transition will simply loop back (i.e. the state will not change).
|
98
|
+
# * <tt>:except_from</tt> - A state or array of states that *cannot* be
|
99
|
+
# transitioned from.
|
100
|
+
#
|
101
|
+
# Examples:
|
102
|
+
#
|
103
|
+
# transition :to => nil
|
104
|
+
# transition :to => :idling
|
105
|
+
# transition :except_from => [:idling, :first_gear], :to => :idling
|
106
|
+
# transition :from => nil, :to => :idling
|
107
|
+
# transition :from => [:parked, :stalled], :to => :idling
|
108
|
+
#
|
109
|
+
# transition :from => :parked
|
110
|
+
# transition :from => [:parked, :stalled]
|
111
|
+
# transition :except_from => :parked
|
112
|
+
#
|
113
|
+
# Notice that the above examples are the verbose equivalent of the examples
|
114
|
+
# described initially.
|
115
|
+
#
|
116
|
+
# == Conditions
|
117
|
+
#
|
118
|
+
# In addition to the state requirements for each transition, a condition
|
119
|
+
# can also be defined to help determine whether that transition is
|
120
|
+
# available. These options will work on both the normal and verbose syntax.
|
121
|
+
#
|
122
|
+
# Configuration options:
|
123
|
+
# * <tt>:if</tt> - A method, proc or string to call to determine if the
|
124
|
+
# transition should occur (e.g. :if => :moving?, or :if => lambda {|vehicle| vehicle.speed > 60}).
|
125
|
+
# The condition should return or evaluate to true or false.
|
126
|
+
# * <tt>:unless</tt> - A method, proc or string to call to determine if the
|
127
|
+
# transition should not occur (e.g. :unless => :stopped?, or :unless => lambda {|vehicle| vehicle.speed <= 60}).
|
128
|
+
# The condition should return or evaluate to true or false.
|
129
|
+
#
|
130
|
+
# Examples:
|
131
|
+
#
|
132
|
+
# transition :parked => :idling, :if => :moving?
|
133
|
+
# transition :parked => :idling, :unless => :stopped?
|
134
|
+
#
|
135
|
+
# transition :from => :parked, :to => :idling, :if => :moving?
|
136
|
+
# transition :from => :parked, :to => :idling, :unless => :stopped?
|
137
|
+
#
|
138
|
+
# == Order of operations
|
139
|
+
#
|
140
|
+
# Transitions are evaluated in the order in which they're defined. As a
|
141
|
+
# result, if more than one transition applies to a given object, then the
|
142
|
+
# first transition that matches will be performed.
|
143
|
+
def transition(options)
|
144
|
+
raise ArgumentError, 'Must specify as least one transition requirement' if options.empty?
|
145
|
+
|
146
|
+
# Only a certain subset of explicit options are allowed for transition
|
147
|
+
# requirements
|
148
|
+
assert_valid_keys(options, :from, :to, :except_from, :if, :unless) if (options.keys - [:from, :to, :on, :except_from, :except_to, :except_on, :if, :unless]).empty?
|
149
|
+
|
150
|
+
guards << guard = Guard.new(options.merge(:on => name))
|
151
|
+
@known_states |= guard.known_states
|
152
|
+
guard
|
153
|
+
end
|
154
|
+
|
155
|
+
# Determines whether any transitions can be performed for this event based
|
156
|
+
# on the current state of the given object.
|
157
|
+
#
|
158
|
+
# If the event can't be fired, then this will return false, otherwise true.
|
159
|
+
def can_fire?(object)
|
160
|
+
!transition_for(object).nil?
|
161
|
+
end
|
162
|
+
|
163
|
+
# Finds and builds the next transition that can be performed on the given
|
164
|
+
# object. If no transitions can be made, then this will return nil.
|
165
|
+
def transition_for(object, requirements = {})
|
166
|
+
requirements[:from] = machine.states.match!(object).name unless custom_from_state = requirements.include?(:from)
|
167
|
+
|
168
|
+
guards.each do |guard|
|
169
|
+
if match = guard.match(object, requirements)
|
170
|
+
# Guard allows for the transition to occur
|
171
|
+
from = requirements[:from]
|
172
|
+
to = match[:to].values.empty? ? from : match[:to].values.first
|
173
|
+
|
174
|
+
return Transition.new(object, machine, name, from, to, !custom_from_state)
|
175
|
+
end
|
176
|
+
end
|
177
|
+
|
178
|
+
# No transition matched
|
179
|
+
nil
|
180
|
+
end
|
181
|
+
|
182
|
+
# Attempts to perform the next available transition on the given object.
|
183
|
+
# If no transitions can be made, then this will return false, otherwise
|
184
|
+
# true.
|
185
|
+
#
|
186
|
+
# Any additional arguments are passed to the StateMachine::Transition#perform
|
187
|
+
# instance method.
|
188
|
+
def fire(object, *args)
|
189
|
+
machine.reset(object)
|
190
|
+
|
191
|
+
if transition = transition_for(object)
|
192
|
+
transition.perform(*args)
|
193
|
+
else
|
194
|
+
machine.invalidate(object, :state, :invalid_transition, [[:event, name]])
|
195
|
+
false
|
196
|
+
end
|
197
|
+
end
|
198
|
+
|
199
|
+
# Draws a representation of this event on the given graph. This will
|
200
|
+
# create 1 or more edges on the graph for each guard (i.e. transition)
|
201
|
+
# configured.
|
202
|
+
#
|
203
|
+
# A collection of the generated edges will be returned.
|
204
|
+
def draw(graph)
|
205
|
+
valid_states = machine.states.by_priority.map {|state| state.name}
|
206
|
+
guards.collect {|guard| guard.draw(graph, name, valid_states)}.flatten
|
207
|
+
end
|
208
|
+
|
209
|
+
# Generates a nicely formatted description of this event's contents.
|
210
|
+
#
|
211
|
+
# For example,
|
212
|
+
#
|
213
|
+
# event = StateMachine::Event.new(machine, :park)
|
214
|
+
# event.transition all - :idling => :parked, :idling => same
|
215
|
+
# event # => #<StateMachine::Event name=:park transitions=[all - :idling => :parked, :idling => same]>
|
216
|
+
def inspect
|
217
|
+
transitions = guards.map do |guard|
|
218
|
+
guard.state_requirements.map do |state_requirement|
|
219
|
+
"#{state_requirement[:from].description} => #{state_requirement[:to].description}"
|
220
|
+
end * ', '
|
221
|
+
end
|
222
|
+
|
223
|
+
"#<#{self.class} name=#{name.inspect} transitions=[#{transitions * ', '}]>"
|
224
|
+
end
|
225
|
+
|
226
|
+
protected
|
227
|
+
# Add the various instance methods that can transition the object using
|
228
|
+
# the current event
|
229
|
+
def add_actions
|
230
|
+
# Checks whether the event can be fired on the current object
|
231
|
+
machine.define_instance_method("can_#{qualified_name}?") do |machine, object|
|
232
|
+
machine.event(name).can_fire?(object)
|
233
|
+
end
|
234
|
+
|
235
|
+
# Gets the next transition that would be performed if the event were
|
236
|
+
# fired now
|
237
|
+
machine.define_instance_method("#{qualified_name}_transition") do |machine, object|
|
238
|
+
machine.event(name).transition_for(object)
|
239
|
+
end
|
240
|
+
|
241
|
+
# Fires the event
|
242
|
+
machine.define_instance_method(qualified_name) do |machine, object, *args|
|
243
|
+
machine.event(name).fire(object, *args)
|
244
|
+
end
|
245
|
+
|
246
|
+
# Fires the event, raising an exception if it fails
|
247
|
+
machine.define_instance_method("#{qualified_name}!") do |machine, object, *args|
|
248
|
+
object.send(qualified_name, *args) || raise(StateMachine::InvalidTransition, "Cannot transition #{machine.name} via :#{name} from #{machine.states.match!(object).name.inspect}")
|
249
|
+
end
|
250
|
+
end
|
251
|
+
end
|
252
|
+
end
|
@@ -0,0 +1,122 @@
|
|
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
|
@@ -0,0 +1,149 @@
|
|
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
|