state_machines 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +21 -0
- data/.idea/.name +1 -0
- data/.idea/.rakeTasks +7 -0
- data/.idea/cssxfire.xml +9 -0
- data/.idea/encodings.xml +5 -0
- data/.idea/misc.xml +5 -0
- data/.idea/modules.xml +12 -0
- data/.idea/scopes/scope_settings.xml +5 -0
- data/.idea/state_machine2.iml +34 -0
- data/.idea/vcs.xml +9 -0
- data/.idea/workspace.xml +1156 -0
- data/.rspec +3 -0
- data/.travis.yml +8 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +23 -0
- data/README.md +29 -0
- data/Rakefile +1 -0
- data/lib/state_machines/assertions.rb +40 -0
- data/lib/state_machines/branch.rb +187 -0
- data/lib/state_machines/callback.rb +220 -0
- data/lib/state_machines/core.rb +25 -0
- data/lib/state_machines/core_ext/class/state_machine.rb +5 -0
- data/lib/state_machines/core_ext.rb +2 -0
- data/lib/state_machines/error.rb +13 -0
- data/lib/state_machines/eval_helpers.rb +87 -0
- data/lib/state_machines/event.rb +246 -0
- data/lib/state_machines/event_collection.rb +141 -0
- data/lib/state_machines/extensions.rb +148 -0
- data/lib/state_machines/helper_module.rb +17 -0
- data/lib/state_machines/integrations/base.rb +100 -0
- data/lib/state_machines/integrations.rb +113 -0
- data/lib/state_machines/machine.rb +2234 -0
- data/lib/state_machines/machine_collection.rb +84 -0
- data/lib/state_machines/macro_methods.rb +520 -0
- data/lib/state_machines/matcher.rb +123 -0
- data/lib/state_machines/matcher_helpers.rb +54 -0
- data/lib/state_machines/node_collection.rb +221 -0
- data/lib/state_machines/path.rb +120 -0
- data/lib/state_machines/path_collection.rb +90 -0
- data/lib/state_machines/state.rb +276 -0
- data/lib/state_machines/state_collection.rb +112 -0
- data/lib/state_machines/state_context.rb +138 -0
- data/lib/state_machines/transition.rb +470 -0
- data/lib/state_machines/transition_collection.rb +245 -0
- data/lib/state_machines/version.rb +3 -0
- data/lib/state_machines/yard.rb +8 -0
- data/lib/state_machines.rb +3 -0
- data/spec/errors/default_spec.rb +14 -0
- data/spec/errors/with_message_spec.rb +39 -0
- data/spec/helpers/helper_spec.rb +14 -0
- data/spec/internal/app/models/auto_shop.rb +31 -0
- data/spec/internal/app/models/car.rb +19 -0
- data/spec/internal/app/models/model_base.rb +6 -0
- data/spec/internal/app/models/motorcycle.rb +9 -0
- data/spec/internal/app/models/traffic_light.rb +47 -0
- data/spec/internal/app/models/vehicle.rb +123 -0
- data/spec/machine_spec.rb +3167 -0
- data/spec/matcher_helpers_spec.rb +39 -0
- data/spec/matcher_spec.rb +157 -0
- data/spec/models/auto_shop_spec.rb +41 -0
- data/spec/models/car_spec.rb +90 -0
- data/spec/models/motorcycle_spec.rb +44 -0
- data/spec/models/traffic_light_spec.rb +56 -0
- data/spec/models/vehicle_spec.rb +580 -0
- data/spec/node_collection_spec.rb +371 -0
- data/spec/path_collection_spec.rb +271 -0
- data/spec/path_spec.rb +488 -0
- data/spec/spec_helper.rb +6 -0
- data/spec/state_collection_spec.rb +352 -0
- data/spec/state_context_spec.rb +442 -0
- data/spec/state_machine_spec.rb +29 -0
- data/spec/state_spec.rb +970 -0
- data/spec/support/migration_helpers.rb +50 -0
- data/spec/support/models.rb +6 -0
- data/spec/transition_collection_spec.rb +2199 -0
- data/spec/transition_spec.rb +1558 -0
- data/state_machines.gemspec +23 -0
- metadata +194 -0
@@ -0,0 +1,276 @@
|
|
1
|
+
require 'state_machines/assertions'
|
2
|
+
require 'state_machines/state_context'
|
3
|
+
|
4
|
+
module StateMachines
|
5
|
+
# A state defines a value that an attribute can be in after being transitioned
|
6
|
+
# 0 or more times. States can represent a value of any type in Ruby, though
|
7
|
+
# the most common (and default) type is String.
|
8
|
+
#
|
9
|
+
# In addition to defining the machine's value, a state can also define a
|
10
|
+
# behavioral context for an object when that object is in the state. See
|
11
|
+
# StateMachines::Machine#state for more information about how state-driven
|
12
|
+
# behavior can be utilized.
|
13
|
+
class State
|
14
|
+
|
15
|
+
|
16
|
+
# The state machine for which this state is defined
|
17
|
+
attr_accessor :machine
|
18
|
+
|
19
|
+
# The unique identifier for the state used in event and callback definitions
|
20
|
+
attr_reader :name
|
21
|
+
|
22
|
+
# The fully-qualified identifier for the state, scoped by the machine's
|
23
|
+
# namespace
|
24
|
+
attr_reader :qualified_name
|
25
|
+
|
26
|
+
# The human-readable name for the state
|
27
|
+
attr_writer :human_name
|
28
|
+
|
29
|
+
# The value that is written to a machine's attribute when an object
|
30
|
+
# transitions into this state
|
31
|
+
attr_writer :value
|
32
|
+
|
33
|
+
# Whether this state's value should be cached after being evaluated
|
34
|
+
attr_accessor :cache
|
35
|
+
|
36
|
+
# Whether or not this state is the initial state to use for new objects
|
37
|
+
attr_accessor :initial
|
38
|
+
alias_method :initial?, :initial
|
39
|
+
|
40
|
+
# A custom lambda block for determining whether a given value matches this
|
41
|
+
# state
|
42
|
+
attr_accessor :matcher
|
43
|
+
|
44
|
+
# Creates a new state within the context of the given machine.
|
45
|
+
#
|
46
|
+
# Configuration options:
|
47
|
+
# * <tt>:initial</tt> - Whether this state is the beginning state for the
|
48
|
+
# machine. Default is false.
|
49
|
+
# * <tt>:value</tt> - The value to store when an object transitions to this
|
50
|
+
# state. Default is the name (stringified).
|
51
|
+
# * <tt>:cache</tt> - If a dynamic value (via a lambda block) is being used,
|
52
|
+
# then setting this to true will cache the evaluated result
|
53
|
+
# * <tt>:if</tt> - Determines whether a value matches this state
|
54
|
+
# (e.g. :value => lambda {Time.now}, :if => lambda {|state| !state.nil?}).
|
55
|
+
# By default, the configured value is matched.
|
56
|
+
# * <tt>:human_name</tt> - The human-readable version of this state's name
|
57
|
+
def initialize(machine, name, options = {}) #:nodoc:
|
58
|
+
options.assert_valid_keys(:initial, :value, :cache, :if, :human_name)
|
59
|
+
|
60
|
+
@machine = machine
|
61
|
+
@name = name
|
62
|
+
@qualified_name = name && machine.namespace ? :"#{machine.namespace}_#{name}" : name
|
63
|
+
@human_name = options[:human_name] || (@name ? @name.to_s.tr('_', ' ') : 'nil')
|
64
|
+
@value = options.include?(:value) ? options[:value] : name && name.to_s
|
65
|
+
@cache = options[:cache]
|
66
|
+
@matcher = options[:if]
|
67
|
+
@initial = options[:initial] == true
|
68
|
+
@context = StateContext.new(self)
|
69
|
+
|
70
|
+
if name
|
71
|
+
conflicting_machines = machine.owner_class.state_machines.select { |other_name, other_machine| other_machine != machine && other_machine.states[qualified_name, :qualified_name] }
|
72
|
+
|
73
|
+
# Output a warning if another machine has a conflicting qualified name
|
74
|
+
# for a different attribute
|
75
|
+
if conflict = conflicting_machines.detect { |other_name, other_machine| other_machine.attribute != machine.attribute }
|
76
|
+
name, other_machine = conflict
|
77
|
+
warn "State #{qualified_name.inspect} for #{machine.name.inspect} is already defined in #{other_machine.name.inspect}"
|
78
|
+
elsif conflicting_machines.empty?
|
79
|
+
# Only bother adding predicates when another machine for the same
|
80
|
+
# attribute hasn't already done so
|
81
|
+
add_predicate
|
82
|
+
end
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
# Creates a copy of this state, excluding the context to prevent conflicts
|
87
|
+
# across different machines.
|
88
|
+
def initialize_copy(orig) #:nodoc:
|
89
|
+
super
|
90
|
+
@context = StateContext.new(self)
|
91
|
+
end
|
92
|
+
|
93
|
+
# Determines whether there are any states that can be transitioned to from
|
94
|
+
# this state. If there are none, then this state is considered *final*.
|
95
|
+
# Any objects in a final state will remain so forever given the current
|
96
|
+
# machine's definition.
|
97
|
+
def final?
|
98
|
+
!machine.events.any? do |event|
|
99
|
+
event.branches.any? do |branch|
|
100
|
+
branch.state_requirements.any? do |requirement|
|
101
|
+
requirement[:from].matches?(name) && !requirement[:to].matches?(name, :from => name)
|
102
|
+
end
|
103
|
+
end
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
107
|
+
# Transforms the state name into a more human-readable format, such as
|
108
|
+
# "first gear" instead of "first_gear"
|
109
|
+
def human_name(klass = @machine.owner_class)
|
110
|
+
@human_name.is_a?(Proc) ? @human_name.call(self, klass) : @human_name
|
111
|
+
end
|
112
|
+
|
113
|
+
# Generates a human-readable description of this state's name / value:
|
114
|
+
#
|
115
|
+
# For example,
|
116
|
+
#
|
117
|
+
# State.new(machine, :parked).description # => "parked"
|
118
|
+
# State.new(machine, :parked, :value => :parked).description # => "parked"
|
119
|
+
# State.new(machine, :parked, :value => nil).description # => "parked (nil)"
|
120
|
+
# State.new(machine, :parked, :value => 1).description # => "parked (1)"
|
121
|
+
# State.new(machine, :parked, :value => lambda {Time.now}).description # => "parked (*)
|
122
|
+
#
|
123
|
+
# Configuration options:
|
124
|
+
# * <tt>:human_name</tt> - Whether to use this state's human name in the
|
125
|
+
# description or just the internal name
|
126
|
+
def description(options = {})
|
127
|
+
label = options[:human_name] ? human_name : name
|
128
|
+
description = label ? label.to_s : label.inspect
|
129
|
+
description << " (#{@value.is_a?(Proc) ? '*' : @value.inspect})" unless name.to_s == @value.to_s
|
130
|
+
description
|
131
|
+
end
|
132
|
+
|
133
|
+
# The value that represents this state. This will optionally evaluate the
|
134
|
+
# original block if it's a lambda block. Otherwise, the static value is
|
135
|
+
# returned.
|
136
|
+
#
|
137
|
+
# For example,
|
138
|
+
#
|
139
|
+
# State.new(machine, :parked, :value => 1).value # => 1
|
140
|
+
# State.new(machine, :parked, :value => lambda {Time.now}).value # => Tue Jan 01 00:00:00 UTC 2008
|
141
|
+
# State.new(machine, :parked, :value => lambda {Time.now}).value(false) # => <Proc:0xb6ea7ca0@...>
|
142
|
+
def value(eval = true)
|
143
|
+
if @value.is_a?(Proc) && eval
|
144
|
+
if cache_value?
|
145
|
+
@value = @value.call
|
146
|
+
machine.states.update(self)
|
147
|
+
@value
|
148
|
+
else
|
149
|
+
@value.call
|
150
|
+
end
|
151
|
+
else
|
152
|
+
@value
|
153
|
+
end
|
154
|
+
end
|
155
|
+
|
156
|
+
# Determines whether this state matches the given value. If no matcher is
|
157
|
+
# configured, then this will check whether the values are equivalent.
|
158
|
+
# Otherwise, the matcher will determine the result.
|
159
|
+
#
|
160
|
+
# For example,
|
161
|
+
#
|
162
|
+
# # Without a matcher
|
163
|
+
# state = State.new(machine, :parked, :value => 1)
|
164
|
+
# state.matches?(1) # => true
|
165
|
+
# state.matches?(2) # => false
|
166
|
+
#
|
167
|
+
# # With a matcher
|
168
|
+
# state = State.new(machine, :parked, :value => lambda {Time.now}, :if => lambda {|value| !value.nil?})
|
169
|
+
# state.matches?(nil) # => false
|
170
|
+
# state.matches?(Time.now) # => true
|
171
|
+
def matches?(other_value)
|
172
|
+
matcher ? matcher.call(other_value) : other_value == value
|
173
|
+
end
|
174
|
+
|
175
|
+
# Defines a context for the state which will be enabled on instances of
|
176
|
+
# the owner class when the machine is in this state.
|
177
|
+
#
|
178
|
+
# This can be called multiple times. Each time a new context is created,
|
179
|
+
# a new module will be included in the owner class.
|
180
|
+
def context(&block)
|
181
|
+
# Include the context
|
182
|
+
context = @context
|
183
|
+
machine.owner_class.class_eval { include context }
|
184
|
+
|
185
|
+
# Evaluate the method definitions and track which ones were added
|
186
|
+
old_methods = context_methods
|
187
|
+
context.class_eval(&block)
|
188
|
+
new_methods = context_methods.to_a.select { |(name, method)| old_methods[name] != method }
|
189
|
+
|
190
|
+
# Alias new methods so that the only execute when the object is in this state
|
191
|
+
new_methods.each do |(method_name, method)|
|
192
|
+
context_name = context_name_for(method_name)
|
193
|
+
context.class_eval <<-end_eval, __FILE__, __LINE__ + 1
|
194
|
+
alias_method :"#{context_name}", :#{method_name}
|
195
|
+
def #{method_name}(*args, &block)
|
196
|
+
state = self.class.state_machine(#{machine.name.inspect}).states.fetch(#{name.inspect})
|
197
|
+
options = {:method_missing => lambda {super(*args, &block)}, :method_name => #{method_name.inspect}}
|
198
|
+
state.call(self, :"#{context_name}", *(args + [options]), &block)
|
199
|
+
end
|
200
|
+
end_eval
|
201
|
+
end
|
202
|
+
|
203
|
+
true
|
204
|
+
end
|
205
|
+
|
206
|
+
# The list of methods that have been defined in this state's context
|
207
|
+
def context_methods
|
208
|
+
@context.instance_methods.inject({}) do |methods, name|
|
209
|
+
methods.merge(name.to_sym => @context.instance_method(name))
|
210
|
+
end
|
211
|
+
end
|
212
|
+
|
213
|
+
# Calls a method defined in this state's context on the given object. All
|
214
|
+
# arguments and any block will be passed into the method defined.
|
215
|
+
#
|
216
|
+
# If the method has never been defined for this state, then a NoMethodError
|
217
|
+
# will be raised.
|
218
|
+
def call(object, method, *args, &block)
|
219
|
+
options = args.last.is_a?(Hash) ? args.pop : {}
|
220
|
+
options = {:method_name => method}.merge(options)
|
221
|
+
state = machine.states.match!(object)
|
222
|
+
|
223
|
+
if state == self && object.respond_to?(method)
|
224
|
+
object.send(method, *args, &block)
|
225
|
+
elsif method_missing = options[:method_missing]
|
226
|
+
# Dispatch to the superclass since the object either isn't in this state
|
227
|
+
# or this state doesn't handle the method
|
228
|
+
begin
|
229
|
+
method_missing.call
|
230
|
+
rescue NoMethodError => ex
|
231
|
+
if ex.name.to_s == options[:method_name].to_s && ex.args == args
|
232
|
+
# No valid context for this method
|
233
|
+
raise InvalidContext.new(object, "State #{state.name.inspect} for #{machine.name.inspect} is not a valid context for calling ##{options[:method_name]}")
|
234
|
+
else
|
235
|
+
raise
|
236
|
+
end
|
237
|
+
end
|
238
|
+
end
|
239
|
+
end
|
240
|
+
|
241
|
+
def draw(graph, options = {})
|
242
|
+
fail NotImplementedError
|
243
|
+
end
|
244
|
+
|
245
|
+
# Generates a nicely formatted description of this state's contents.
|
246
|
+
#
|
247
|
+
# For example,
|
248
|
+
#
|
249
|
+
# state = StateMachines::State.new(machine, :parked, :value => 1, :initial => true)
|
250
|
+
# state # => #<StateMachines::State name=:parked value=1 initial=true context=[]>
|
251
|
+
def inspect
|
252
|
+
attributes = [[:name, name], [:value, @value], [:initial, initial?]]
|
253
|
+
"#<#{self.class} #{attributes.map { |attr, value| "#{attr}=#{value.inspect}" } * ' '}>"
|
254
|
+
end
|
255
|
+
|
256
|
+
private
|
257
|
+
# Should the value be cached after it's evaluated for the first time?
|
258
|
+
def cache_value?
|
259
|
+
@cache
|
260
|
+
end
|
261
|
+
|
262
|
+
# Adds a predicate method to the owner class so long as a name has
|
263
|
+
# actually been configured for the state
|
264
|
+
def add_predicate
|
265
|
+
# Checks whether the current value matches this state
|
266
|
+
machine.define_helper(:instance, "#{qualified_name}?") do |machine, object|
|
267
|
+
machine.states.matches?(object, name)
|
268
|
+
end
|
269
|
+
end
|
270
|
+
|
271
|
+
# Generates the name of the method containing the actual implementation
|
272
|
+
def context_name_for(method)
|
273
|
+
:"__#{machine.name}_#{name}_#{method}_#{@context.object_id}__"
|
274
|
+
end
|
275
|
+
end
|
276
|
+
end
|
@@ -0,0 +1,112 @@
|
|
1
|
+
require 'state_machines/node_collection'
|
2
|
+
|
3
|
+
module StateMachines
|
4
|
+
# Represents a collection of states in a state machine
|
5
|
+
class StateCollection < NodeCollection
|
6
|
+
def initialize(machine) #:nodoc:
|
7
|
+
super(machine, :index => [:name, :qualified_name, :value])
|
8
|
+
end
|
9
|
+
|
10
|
+
# Determines whether the given object is in a specific state. If the
|
11
|
+
# object's current value doesn't match the state, then this will return
|
12
|
+
# false, otherwise true. If the given state is unknown, then an IndexError
|
13
|
+
# will be raised.
|
14
|
+
#
|
15
|
+
# == Examples
|
16
|
+
#
|
17
|
+
# class Vehicle
|
18
|
+
# state_machine :initial => :parked do
|
19
|
+
# other_states :idling
|
20
|
+
# end
|
21
|
+
# end
|
22
|
+
#
|
23
|
+
# states = Vehicle.state_machine.states
|
24
|
+
# vehicle = Vehicle.new # => #<Vehicle:0xb7c464b0 @state="parked">
|
25
|
+
#
|
26
|
+
# states.matches?(vehicle, :parked) # => true
|
27
|
+
# states.matches?(vehicle, :idling) # => false
|
28
|
+
# states.matches?(vehicle, :invalid) # => IndexError: :invalid is an invalid key for :name index
|
29
|
+
def matches?(object, name)
|
30
|
+
fetch(name).matches?(machine.read(object, :state))
|
31
|
+
end
|
32
|
+
|
33
|
+
# Determines the current state of the given object as configured by this
|
34
|
+
# state machine. This will attempt to find a known state that matches
|
35
|
+
# the value of the attribute on the object.
|
36
|
+
#
|
37
|
+
# == Examples
|
38
|
+
#
|
39
|
+
# class Vehicle
|
40
|
+
# state_machine :initial => :parked do
|
41
|
+
# other_states :idling
|
42
|
+
# end
|
43
|
+
# end
|
44
|
+
#
|
45
|
+
# states = Vehicle.state_machine.states
|
46
|
+
#
|
47
|
+
# vehicle = Vehicle.new # => #<Vehicle:0xb7c464b0 @state="parked">
|
48
|
+
# states.match(vehicle) # => #<StateMachines::State name=:parked value="parked" initial=true>
|
49
|
+
#
|
50
|
+
# vehicle.state = 'idling'
|
51
|
+
# states.match(vehicle) # => #<StateMachines::State name=:idling value="idling" initial=true>
|
52
|
+
#
|
53
|
+
# vehicle.state = 'invalid'
|
54
|
+
# states.match(vehicle) # => nil
|
55
|
+
def match(object)
|
56
|
+
value = machine.read(object, :state)
|
57
|
+
self[value, :value] || detect {|state| state.matches?(value)}
|
58
|
+
end
|
59
|
+
|
60
|
+
# Determines the current state of the given object as configured by this
|
61
|
+
# state machine. If no state is found, then an ArgumentError will be
|
62
|
+
# raised.
|
63
|
+
#
|
64
|
+
# == Examples
|
65
|
+
#
|
66
|
+
# class Vehicle
|
67
|
+
# state_machine :initial => :parked do
|
68
|
+
# other_states :idling
|
69
|
+
# end
|
70
|
+
# end
|
71
|
+
#
|
72
|
+
# states = Vehicle.state_machine.states
|
73
|
+
#
|
74
|
+
# vehicle = Vehicle.new # => #<Vehicle:0xb7c464b0 @state="parked">
|
75
|
+
# states.match!(vehicle) # => #<StateMachines::State name=:parked value="parked" initial=true>
|
76
|
+
#
|
77
|
+
# vehicle.state = 'invalid'
|
78
|
+
# states.match!(vehicle) # => ArgumentError: "invalid" is not a known state value
|
79
|
+
def match!(object)
|
80
|
+
match(object) || raise(ArgumentError, "#{machine.read(object, :state).inspect} is not a known #{machine.name} value")
|
81
|
+
end
|
82
|
+
|
83
|
+
# Gets the order in which states should be displayed based on where they
|
84
|
+
# were first referenced. This will order states in the following priority:
|
85
|
+
#
|
86
|
+
# 1. Initial state
|
87
|
+
# 2. Event transitions (:from, :except_from, :to, :except_to options)
|
88
|
+
# 3. States with behaviors
|
89
|
+
# 4. States referenced via +state+ or +other_states+
|
90
|
+
# 5. States referenced in callbacks
|
91
|
+
#
|
92
|
+
# This order will determine how the GraphViz visualizations are rendered.
|
93
|
+
def by_priority
|
94
|
+
order = select {|state| state.initial}.map {|state| state.name}
|
95
|
+
|
96
|
+
machine.events.each {|event| order += event.known_states}
|
97
|
+
order += select {|state| state.context_methods.any?}.map {|state| state.name}
|
98
|
+
order += keys(:name) - machine.callbacks.values.flatten.map {|callback| callback.known_states}.flatten
|
99
|
+
order += keys(:name)
|
100
|
+
|
101
|
+
order.uniq!
|
102
|
+
order.map! {|name| self[name]}
|
103
|
+
order
|
104
|
+
end
|
105
|
+
|
106
|
+
private
|
107
|
+
# Gets the value for the given attribute on the node
|
108
|
+
def value(node, attribute)
|
109
|
+
attribute == :value ? node.value(false) : super
|
110
|
+
end
|
111
|
+
end
|
112
|
+
end
|
@@ -0,0 +1,138 @@
|
|
1
|
+
require 'state_machines/assertions'
|
2
|
+
require 'state_machines/eval_helpers'
|
3
|
+
|
4
|
+
module StateMachines
|
5
|
+
# A method was called in an invalid state context
|
6
|
+
class InvalidContext < Error
|
7
|
+
end
|
8
|
+
|
9
|
+
# Represents a module which will get evaluated within the context of a state.
|
10
|
+
#
|
11
|
+
# Class-level methods are proxied to the owner class, injecting a custom
|
12
|
+
# <tt>:if</tt> condition along with method. This assumes that the method has
|
13
|
+
# support for a set of configuration options, including <tt>:if</tt>. This
|
14
|
+
# condition will check that the object's state matches this context's state.
|
15
|
+
#
|
16
|
+
# Instance-level methods are used to define state-driven behavior on the
|
17
|
+
# state's owner class.
|
18
|
+
#
|
19
|
+
# == Examples
|
20
|
+
#
|
21
|
+
# class Vehicle
|
22
|
+
# class << self
|
23
|
+
# attr_accessor :validations
|
24
|
+
#
|
25
|
+
# def validate(options, &block)
|
26
|
+
# validations << options
|
27
|
+
# end
|
28
|
+
# end
|
29
|
+
#
|
30
|
+
# self.validations = []
|
31
|
+
# attr_accessor :state, :simulate
|
32
|
+
#
|
33
|
+
# def moving?
|
34
|
+
# self.class.validations.all? {|validation| validation[:if].call(self)}
|
35
|
+
# end
|
36
|
+
# end
|
37
|
+
#
|
38
|
+
# In the above class, a simple set of validation behaviors have been defined.
|
39
|
+
# Each validation consists of a configuration like so:
|
40
|
+
#
|
41
|
+
# Vehicle.validate :unless => :simulate
|
42
|
+
# Vehicle.validate :if => lambda {|vehicle| ...}
|
43
|
+
#
|
44
|
+
# In order to scope validations to a particular state context, the class-level
|
45
|
+
# +validate+ method can be invoked like so:
|
46
|
+
#
|
47
|
+
# machine = StateMachines::Machine.new(Vehicle)
|
48
|
+
# context = StateMachines::StateContext.new(machine.state(:first_gear))
|
49
|
+
# context.validate(:unless => :simulate)
|
50
|
+
#
|
51
|
+
# vehicle = Vehicle.new # => #<Vehicle:0xb7ce491c @simulate=nil, @state=nil>
|
52
|
+
# vehicle.moving? # => false
|
53
|
+
#
|
54
|
+
# vehicle.state = 'first_gear'
|
55
|
+
# vehicle.moving? # => true
|
56
|
+
#
|
57
|
+
# vehicle.simulate = true
|
58
|
+
# vehicle.moving? # => false
|
59
|
+
class StateContext < Module
|
60
|
+
|
61
|
+
include EvalHelpers
|
62
|
+
|
63
|
+
# The state machine for which this context's state is defined
|
64
|
+
attr_reader :machine
|
65
|
+
|
66
|
+
# The state that must be present in an object for this context to be active
|
67
|
+
attr_reader :state
|
68
|
+
|
69
|
+
# Creates a new context for the given state
|
70
|
+
def initialize(state)
|
71
|
+
@state = state
|
72
|
+
@machine = state.machine
|
73
|
+
|
74
|
+
state_name = state.name
|
75
|
+
machine_name = machine.name
|
76
|
+
@condition = lambda {|object| object.class.state_machine(machine_name).states.matches?(object, state_name)}
|
77
|
+
end
|
78
|
+
|
79
|
+
# Creates a new transition that determines what to change the current state
|
80
|
+
# to when an event fires from this state.
|
81
|
+
#
|
82
|
+
# Since this transition is being defined within a state context, you do
|
83
|
+
# *not* need to specify the <tt>:from</tt> option for the transition. For
|
84
|
+
# example:
|
85
|
+
#
|
86
|
+
# state_machine do
|
87
|
+
# state :parked do
|
88
|
+
# transition :to => :idling, :on => [:ignite, :shift_up] # Transitions to :idling
|
89
|
+
# transition :from => [:idling, :parked], :on => :park, :unless => :seatbelt_on? # Transitions to :parked if seatbelt is off
|
90
|
+
# end
|
91
|
+
# end
|
92
|
+
#
|
93
|
+
# See StateMachines::Machine#transition for a description of the possible
|
94
|
+
# configurations for defining transitions.
|
95
|
+
def transition(options)
|
96
|
+
options.assert_valid_keys(:from, :to, :on, :if, :unless)
|
97
|
+
raise ArgumentError, 'Must specify :on event' unless options[:on]
|
98
|
+
raise ArgumentError, 'Must specify either :to or :from state' unless !options[:to] ^ !options[:from]
|
99
|
+
|
100
|
+
machine.transition(options.merge(options[:to] ? {:from => state.name} : {:to => state.name}))
|
101
|
+
end
|
102
|
+
|
103
|
+
# Hooks in condition-merging to methods that don't exist in this module
|
104
|
+
def method_missing(*args, &block)
|
105
|
+
# Get the configuration
|
106
|
+
if args.last.is_a?(Hash)
|
107
|
+
options = args.last
|
108
|
+
else
|
109
|
+
args << options = {}
|
110
|
+
end
|
111
|
+
|
112
|
+
# Get any existing condition that may need to be merged
|
113
|
+
if_condition = options.delete(:if)
|
114
|
+
unless_condition = options.delete(:unless)
|
115
|
+
|
116
|
+
# Provide scope access to configuration in case the block is evaluated
|
117
|
+
# within the object instance
|
118
|
+
proxy = self
|
119
|
+
proxy_condition = @condition
|
120
|
+
|
121
|
+
# Replace the configuration condition with the one configured for this
|
122
|
+
# proxy, merging together any existing conditions
|
123
|
+
options[:if] = lambda do |*condition_args|
|
124
|
+
# Block may be executed within the context of the actual object, so
|
125
|
+
# it'll either be the first argument or the executing context
|
126
|
+
object = condition_args.first || self
|
127
|
+
|
128
|
+
proxy.evaluate_method(object, proxy_condition) &&
|
129
|
+
Array(if_condition).all? {|condition| proxy.evaluate_method(object, condition)} &&
|
130
|
+
!Array(unless_condition).any? {|condition| proxy.evaluate_method(object, condition)}
|
131
|
+
end
|
132
|
+
|
133
|
+
# Evaluate the method on the owner class with the condition proxied
|
134
|
+
# through
|
135
|
+
machine.owner_class.send(*args, &block)
|
136
|
+
end
|
137
|
+
end
|
138
|
+
end
|