state_machines 0.302.0 → 0.304.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +61 -1
- data/lib/state_machines/branch.rb +21 -0
- data/lib/state_machines/callback.rb +26 -6
- data/lib/state_machines/error.rb +13 -2
- data/lib/state_machines/eval_helpers.rb +31 -18
- data/lib/state_machines/event.rb +60 -2
- data/lib/state_machines/machine/action_hooks.rb +13 -10
- data/lib/state_machines/machine/callbacks.rb +15 -0
- data/lib/state_machines/machine/configuration.rb +3 -0
- data/lib/state_machines/machine/helper_generators.rb +26 -13
- data/lib/state_machines/machine/state_methods.rb +4 -3
- data/lib/state_machines/machine/utilities.rb +24 -0
- data/lib/state_machines/machine.rb +0 -2
- data/lib/state_machines/node_collection.rb +26 -23
- data/lib/state_machines/state.rb +66 -31
- data/lib/state_machines/state_collection.rb +10 -2
- data/lib/state_machines/state_context.rb +6 -1
- data/lib/state_machines/test_helper.rb +132 -101
- data/lib/state_machines/transition.rb +108 -232
- data/lib/state_machines/transition_collection.rb +6 -14
- data/lib/state_machines/version.rb +1 -1
- metadata +1 -2
- data/lib/state_machines/machine/validation.rb +0 -39
data/lib/state_machines/state.rb
CHANGED
|
@@ -212,17 +212,30 @@ module StateMachines
|
|
|
212
212
|
context.class_eval(&)
|
|
213
213
|
new_methods = context_methods.to_a.reject { |(name, method)| old_methods[name] == method }
|
|
214
214
|
|
|
215
|
-
# Alias new methods so that the only execute when the object is in this
|
|
215
|
+
# Alias new methods so that the only execute when the object is in this
|
|
216
|
+
# state. define_method, not a def in evaluated source: the methods are
|
|
217
|
+
# the user's, under any name Ruby allows (:"do it", :[], :+).
|
|
218
|
+
machine_name = machine.name
|
|
219
|
+
state_name = name
|
|
216
220
|
new_methods.each do |(method_name, _method)|
|
|
217
221
|
context_name = context_name_for(method_name)
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
222
|
+
visibility = if context.private_method_defined?(method_name) then :private
|
|
223
|
+
elsif context.protected_method_defined?(method_name) then :protected
|
|
224
|
+
end
|
|
225
|
+
context.send(:alias_method, context_name, method_name)
|
|
226
|
+
context.send(:define_method, method_name) do |*args, **kwargs, &block|
|
|
227
|
+
state = self.class.state_machine(machine_name).states.fetch(state_name)
|
|
228
|
+
# Out of state: hand over to whatever is defined above this context,
|
|
229
|
+
# decided up front rather than by rescuing a NoMethodError, which
|
|
230
|
+
# could come from deeper in the stack (another receiver)
|
|
231
|
+
fallback = lambda do
|
|
232
|
+
return super(*args, **kwargs, &block) if StateMachines::State.fallback?(self, context, method_name)
|
|
233
|
+
|
|
234
|
+
raise state.invalid_context(self, method_name, args: args, kwargs: kwargs)
|
|
224
235
|
end
|
|
225
|
-
|
|
236
|
+
state.call(self, context_name, args: args, kwargs: kwargs, method_name: method_name, method_missing: fallback, &block)
|
|
237
|
+
end
|
|
238
|
+
context.send(visibility, method_name) if visibility
|
|
226
239
|
end
|
|
227
240
|
|
|
228
241
|
true
|
|
@@ -230,38 +243,60 @@ module StateMachines
|
|
|
230
243
|
|
|
231
244
|
# The list of methods that have been defined in this state's context
|
|
232
245
|
def context_methods
|
|
233
|
-
|
|
234
|
-
|
|
246
|
+
# Private ones too: they are as state-specific as public ones, and an
|
|
247
|
+
# unwrapped private method would run its state body in every state
|
|
248
|
+
(@context.instance_methods + @context.private_instance_methods).to_h do |name|
|
|
249
|
+
[name.to_sym, @context.instance_method(name)]
|
|
235
250
|
end
|
|
236
251
|
end
|
|
237
252
|
|
|
238
253
|
# Calls a method defined in this state's context on the given object. All
|
|
239
254
|
# arguments and any block will be passed into the method defined.
|
|
240
255
|
#
|
|
241
|
-
# If the
|
|
242
|
-
#
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
"State #{state.name.inspect} for #{machine.name.inspect} is not a valid context for calling ##{options[:method_name]}")
|
|
261
|
-
end
|
|
256
|
+
# If the object is not in this state and no other definition handles the
|
|
257
|
+
# call, StateMachines::InvalidContext (a NoMethodError) is raised.
|
|
258
|
+
#
|
|
259
|
+
# The method's own arguments come in +args+ and +kwargs+, kept apart from
|
|
260
|
+
# the dispatch options so that no argument of the method can be mistaken
|
|
261
|
+
# for one:
|
|
262
|
+
# * <tt>:method_missing</tt> - Called instead when the object is not in this
|
|
263
|
+
# state (e.g. to dispatch to the superclass); its errors propagate as
|
|
264
|
+
# they are. Without one, the call raises InvalidContext.
|
|
265
|
+
# * <tt>:method_name</tt> - The name reported when no state handles the call
|
|
266
|
+
def call(object, method, args: [], kwargs: {}, method_missing: nil, method_name: method, &)
|
|
267
|
+
# include_all: the aliased context method keeps a protected original's
|
|
268
|
+
# visibility, and respond_to? alone would not see it
|
|
269
|
+
if machine.states.match!(object) == self && object.respond_to?(method, true)
|
|
270
|
+
object.send(method, *args, **kwargs, &)
|
|
271
|
+
elsif method_missing
|
|
272
|
+
method_missing.call
|
|
273
|
+
else
|
|
274
|
+
raise invalid_context(object, method_name, args: args, kwargs: kwargs)
|
|
262
275
|
end
|
|
263
276
|
end
|
|
264
277
|
|
|
278
|
+
# The InvalidContext for calling +method_name+ on +object+ outside this
|
|
279
|
+
# state. Its args are what NoMethodError#args would hold: the positional
|
|
280
|
+
# arguments, then the keywords as a Hash when any were given.
|
|
281
|
+
def invalid_context(object, method_name, args: [], kwargs: {}) # :nodoc:
|
|
282
|
+
current = machine.states.match!(object)
|
|
283
|
+
InvalidContext.new(object,
|
|
284
|
+
"State #{current.name.inspect} for #{machine.name.inspect} is not a valid context for calling ##{method_name}",
|
|
285
|
+
method_name, kwargs.empty? ? args : [*args, kwargs])
|
|
286
|
+
end
|
|
287
|
+
|
|
288
|
+
# Whether a call to +method_name+ that leaves +context+ (a state context
|
|
289
|
+
# module) has somewhere to go: a definition above that context in the
|
|
290
|
+
# object's lookup chain (another state's context, an ancestor, a
|
|
291
|
+
# singleton extension), or a method_missing that claims it through
|
|
292
|
+
# respond_to_missing?.
|
|
293
|
+
def self.fallback?(object, context, method_name) # :nodoc:
|
|
294
|
+
# Kernel#method, in case the object overrides #method (e.g. a request)
|
|
295
|
+
method = Kernel.instance_method(:method).bind_call(object, method_name)
|
|
296
|
+
method = method.super_method until method.nil? || method.owner == context
|
|
297
|
+
!method&.super_method.nil? || object.send(:respond_to_missing?, method_name, true)
|
|
298
|
+
end
|
|
299
|
+
|
|
265
300
|
def draw(graph, options = {}, io = $stdout)
|
|
266
301
|
machine.renderer.draw_state(self, graph, options, io)
|
|
267
302
|
end
|
|
@@ -3,8 +3,10 @@
|
|
|
3
3
|
module StateMachines
|
|
4
4
|
# Represents a collection of states in a state machine
|
|
5
5
|
class StateCollection < NodeCollection
|
|
6
|
+
# Names are found by their string form too ('parked' / :parked). Values
|
|
7
|
+
# are not: a state matches a value by ==, so true must not find 'true'.
|
|
6
8
|
def initialize(machine) # :nodoc:
|
|
7
|
-
super(machine, index: %i[name qualified_name value])
|
|
9
|
+
super(machine, index: %i[name qualified_name value], string_keys: %i[name qualified_name])
|
|
8
10
|
end
|
|
9
11
|
|
|
10
12
|
# Determines whether the given object is in a specific state. If the
|
|
@@ -54,7 +56,13 @@ module StateMachines
|
|
|
54
56
|
# states.match(vehicle) # => nil
|
|
55
57
|
def match(object)
|
|
56
58
|
value = machine.read(object, :state)
|
|
57
|
-
self[value, :value]
|
|
59
|
+
indexed = self[value, :value]
|
|
60
|
+
# An index hit is eql?, so it matches by == unless the state has a custom
|
|
61
|
+
# :if matcher, which has the last word. Each matcher runs at most once:
|
|
62
|
+
# the scan skips the candidate already asked.
|
|
63
|
+
return indexed if indexed && (!indexed.matcher || indexed.matches?(value))
|
|
64
|
+
|
|
65
|
+
detect { |state| !state.equal?(indexed) && state.matches?(value) }
|
|
58
66
|
end
|
|
59
67
|
|
|
60
68
|
# Determines the current state of the given object as configured by this
|
|
@@ -87,10 +87,15 @@ module StateMachines
|
|
|
87
87
|
# end
|
|
88
88
|
# end
|
|
89
89
|
#
|
|
90
|
+
# Branch callbacks (<tt>:before</tt>, <tt>:around</tt>, <tt>:after</tt>)
|
|
91
|
+
# work as in StateMachines::Event#transition, e.g.
|
|
92
|
+
# <tt>transition to: :idling, on: :ignite, after: :log_ignition</tt>.
|
|
93
|
+
#
|
|
90
94
|
# See StateMachines::Machine#transition for a description of the possible
|
|
91
95
|
# configurations for defining transitions.
|
|
92
96
|
def transition(options)
|
|
93
|
-
|
|
97
|
+
# :do is not valid, but is let through so Event#transition explains it
|
|
98
|
+
StateMachines::OptionsValidator.assert_valid_keys!(options.except(:do), :from, :to, :on, :if, :unless, :before, :around, :after)
|
|
94
99
|
raise ArgumentError, 'Must specify :on event' unless options[:on]
|
|
95
100
|
raise ArgumentError, 'Must specify either :to or :from state' unless !options[:to] ^ !options[:from]
|
|
96
101
|
|
|
@@ -53,9 +53,9 @@ module StateMachines
|
|
|
53
53
|
end
|
|
54
54
|
|
|
55
55
|
actual = object.send(name_method)
|
|
56
|
-
default_message = "Expected #{object.class}##{machine_name} to be #{expected_state}, but was #{actual}"
|
|
56
|
+
default_message = "Expected #{object.class}##{machine_name} to be #{expected_state.inspect}, but was #{actual.inspect}"
|
|
57
57
|
|
|
58
|
-
|
|
58
|
+
_sm_assert(_sm_same_state?(expected_state, actual), message || default_message)
|
|
59
59
|
end
|
|
60
60
|
|
|
61
61
|
# Assert that an object can transition via a specific event
|
|
@@ -119,70 +119,94 @@ module StateMachines
|
|
|
119
119
|
|
|
120
120
|
# === Extended State Machine Assertions ===
|
|
121
121
|
|
|
122
|
+
# Assert the machine's states, in any order. Names resolve through the
|
|
123
|
+
# machine ('parked' is :parked). The nil state is only listed when expected,
|
|
124
|
+
# since a machine without an initial state has one implicitly.
|
|
122
125
|
def assert_sm_states_list(machine, expected_states, message = nil)
|
|
123
|
-
|
|
124
|
-
|
|
126
|
+
expected = expected_states.map { |state| machine.states[state]&.name || state }
|
|
127
|
+
actual = machine.states.map(&:name)
|
|
128
|
+
actual.compact! unless expected_states.include?(nil)
|
|
129
|
+
default_message = "Expected states #{expected.inspect} but got #{actual.inspect}"
|
|
125
130
|
|
|
126
|
-
_sm_assert_equal(
|
|
131
|
+
_sm_assert_equal(expected.sort_by(&:inspect), actual.sort_by(&:inspect), message || default_message)
|
|
127
132
|
end
|
|
128
133
|
|
|
129
134
|
def refute_sm_state_defined(machine, state, message = nil)
|
|
130
|
-
|
|
131
|
-
default_message = "Expected state #{state} to not be defined in machine"
|
|
135
|
+
default_message = "Expected state #{state.inspect} to not be defined in machine"
|
|
132
136
|
|
|
133
|
-
_sm_refute(
|
|
137
|
+
_sm_refute(machine.states[state], message || default_message)
|
|
134
138
|
end
|
|
135
139
|
alias assert_sm_state_not_defined refute_sm_state_defined
|
|
136
140
|
|
|
141
|
+
# @raise [IndexError] if +expected_state+ is not a known state
|
|
137
142
|
def assert_sm_initial_state(machine, expected_state, message = nil)
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
default_message = "Expected state #{expected_state} to be the initial state"
|
|
143
|
+
state = machine.states.fetch(expected_state)
|
|
144
|
+
default_message = "Expected state #{state.name.inspect} to be the initial state"
|
|
141
145
|
|
|
142
|
-
_sm_assert(
|
|
146
|
+
_sm_assert(state.initial?, message || default_message)
|
|
143
147
|
end
|
|
144
148
|
|
|
149
|
+
# @raise [IndexError] if +state+ is not a known state
|
|
145
150
|
def assert_sm_final_state(machine, state, message = nil)
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
default_message = "Expected state #{state} to be final"
|
|
151
|
+
state = machine.states.fetch(state)
|
|
152
|
+
default_message = "Expected state #{state.name.inspect} to be final"
|
|
149
153
|
|
|
150
|
-
_sm_assert(
|
|
154
|
+
_sm_assert(state.final?, message || default_message)
|
|
151
155
|
end
|
|
152
156
|
|
|
157
|
+
# Assert the set of states the machine's events can lead to from a state,
|
|
158
|
+
# ignoring guards. Each branch contributes the target it would pick for
|
|
159
|
+
# +from+ (a loopback contributes +from+ itself); order does not matter.
|
|
160
|
+
#
|
|
161
|
+
# @param machine [StateMachines::Machine] The state machine
|
|
162
|
+
# @param from [Symbol, String, nil] The source state
|
|
163
|
+
# @param expected_to_states [Array] The expected target states
|
|
164
|
+
# @param message [String, nil] Custom failure message
|
|
165
|
+
# @raise [IndexError] if +from+ or an expected state is not a known state
|
|
153
166
|
def assert_sm_possible_transitions(machine, from:, expected_to_states:, message: nil)
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
167
|
+
from = machine.states.fetch(from).name
|
|
168
|
+
names = machine.states.map(&:name)
|
|
169
|
+
expected = expected_to_states.map { |state| machine.states.fetch(state).name }.uniq
|
|
170
|
+
actual = machine.events.flat_map do |event|
|
|
171
|
+
event.branches.flat_map { |branch| _sm_branch_targets(branch, from, names) }
|
|
157
172
|
end.uniq
|
|
158
|
-
|
|
173
|
+
by_definition = ->(name) { names.index(name) }
|
|
174
|
+
default_message = "Expected transitions from #{from.inspect} to #{expected.inspect} but got #{actual.inspect}"
|
|
159
175
|
|
|
160
|
-
_sm_assert_equal(
|
|
176
|
+
_sm_assert_equal(expected.sort_by(&by_definition), actual.sort_by(&by_definition), message || default_message)
|
|
161
177
|
end
|
|
162
178
|
|
|
179
|
+
# Refute that an event has a branch from +from+ to +to+, ignoring guards.
|
|
180
|
+
#
|
|
181
|
+
# @raise [IndexError] if +on+, +from+ or +to+ is unknown
|
|
163
182
|
def refute_sm_transition_allowed(machine, from:, to:, on:, message: nil)
|
|
164
|
-
event = machine.events
|
|
165
|
-
|
|
166
|
-
|
|
183
|
+
event = machine.events.fetch(on)
|
|
184
|
+
from = machine.states.fetch(from).name
|
|
185
|
+
to = machine.states.fetch(to).name
|
|
186
|
+
names = machine.states.map(&:name)
|
|
187
|
+
is_allowed = event.branches.any? { |branch| _sm_branch_targets(branch, from, names) == [to] }
|
|
188
|
+
default_message = "Expected transition from #{from.inspect} to #{to.inspect} on #{event.name.inspect} to not be allowed"
|
|
167
189
|
|
|
168
190
|
_sm_refute(is_allowed, message || default_message)
|
|
169
191
|
end
|
|
170
192
|
alias assert_sm_transition_not_allowed refute_sm_transition_allowed
|
|
171
193
|
|
|
172
194
|
def assert_sm_event_triggers(object, event, machine_name = :state, message = nil)
|
|
173
|
-
|
|
195
|
+
machine = object.class.state_machine(machine_name)
|
|
196
|
+
initial_state = machine.read(object, :state)
|
|
174
197
|
object.send("#{event}!")
|
|
175
|
-
state_changed = initial_state !=
|
|
198
|
+
state_changed = initial_state != machine.read(object, :state)
|
|
176
199
|
default_message = "Expected event #{event} to trigger state change on #{machine_name}"
|
|
177
200
|
|
|
178
201
|
_sm_assert(state_changed, message || default_message)
|
|
179
202
|
end
|
|
180
203
|
|
|
181
204
|
def refute_sm_event_triggers(object, event, machine_name = :state, message = nil)
|
|
182
|
-
|
|
205
|
+
machine = object.class.state_machine(machine_name)
|
|
206
|
+
initial_state = machine.read(object, :state)
|
|
183
207
|
begin
|
|
184
208
|
object.send("#{event}!")
|
|
185
|
-
state_unchanged = initial_state ==
|
|
209
|
+
state_unchanged = initial_state == machine.read(object, :state)
|
|
186
210
|
default_message = "Expected event #{event} to not trigger state change on #{machine_name}"
|
|
187
211
|
|
|
188
212
|
_sm_assert(state_unchanged, message || default_message)
|
|
@@ -271,15 +295,14 @@ module StateMachines
|
|
|
271
295
|
# # Specific machine
|
|
272
296
|
# assert_sm_triggers_event(vehicle, :disable, machine_name: :alarm) { vehicle.turn_off_alarm }
|
|
273
297
|
def assert_sm_triggers_event(object, expected_events, machine_name: :state, message: nil)
|
|
274
|
-
expected_events = Array(expected_events)
|
|
275
298
|
triggered_events = []
|
|
276
299
|
|
|
277
300
|
# Get the state machine
|
|
278
301
|
machine = object.class.state_machines[machine_name]
|
|
279
302
|
raise ArgumentError, "No state machine found for #{machine_name}" unless machine
|
|
280
303
|
|
|
281
|
-
#
|
|
282
|
-
machine.
|
|
304
|
+
# Resolve the names ('crash' is :crash); an unknown event raises IndexError
|
|
305
|
+
expected_events = Array(expected_events).map { |event| machine.events.fetch(event).name }
|
|
283
306
|
|
|
284
307
|
# Add a temporary callback to track triggered events
|
|
285
308
|
temp_callback = machine.before_transition do |_obj, transition|
|
|
@@ -374,7 +397,7 @@ module StateMachines
|
|
|
374
397
|
|
|
375
398
|
# Check that transition succeeded
|
|
376
399
|
state_changed = initial_state != final_state
|
|
377
|
-
correct_final_state = final_state
|
|
400
|
+
correct_final_state = _sm_same_state?(expected_state, final_state)
|
|
378
401
|
|
|
379
402
|
default_message = "Expected sync execution of '#{event}' to change #{machine_name} from '#{initial_state}' to '#{expected_state}', but got '#{final_state}'"
|
|
380
403
|
|
|
@@ -580,6 +603,13 @@ module StateMachines
|
|
|
580
603
|
|
|
581
604
|
private
|
|
582
605
|
|
|
606
|
+
# Compares a state name or value with an expected state, treating a
|
|
607
|
+
# String and its Symbol as equal. nil only equals nil: comparing by to_s
|
|
608
|
+
# alone would make the nil state and a state named :"" indistinguishable.
|
|
609
|
+
def _sm_same_state?(expected, actual)
|
|
610
|
+
expected.nil? || actual.nil? ? expected.nil? && actual.nil? : expected.to_s == actual.to_s
|
|
611
|
+
end
|
|
612
|
+
|
|
583
613
|
# Finds the machine and its event, accepting the plain or the
|
|
584
614
|
# namespace-qualified event name, and raising when either is missing
|
|
585
615
|
def _sm_find_event(object, event, machine_name)
|
|
@@ -698,80 +728,81 @@ module StateMachines
|
|
|
698
728
|
assert false, message
|
|
699
729
|
end
|
|
700
730
|
|
|
731
|
+
# The target a branch would pick from +from+ (as Event#transition_for
|
|
732
|
+
# does, ignoring guards), as [] or [target]. first(1), not first: a
|
|
733
|
+
# transition into the nil state must not read as none.
|
|
734
|
+
def _sm_branch_targets(branch, from, names)
|
|
735
|
+
requirement = branch.state_requirements.detect { |candidate| candidate[:from].matches?(from) }
|
|
736
|
+
return [] unless requirement
|
|
737
|
+
|
|
738
|
+
requirement[:to].is_a?(LoopbackMatcher) ? [from] : requirement[:to].filter([from] | names).first(1)
|
|
739
|
+
end
|
|
740
|
+
|
|
741
|
+
# Whether a callback's from/to requirement admits the expected states. A
|
|
742
|
+
# loopback (same) lands where it left, so without an expected source the
|
|
743
|
+
# source is the target itself.
|
|
744
|
+
def _sm_state_requirement_matches?(requirement, states)
|
|
745
|
+
loopback = requirement[:to].is_a?(LoopbackMatcher)
|
|
746
|
+
from = states.key?(:from) ? states[:from] : states[:to]
|
|
747
|
+
return false if (states.key?(:from) || loopback) && !requirement[:from].matches?(from)
|
|
748
|
+
|
|
749
|
+
!states.key?(:to) || requirement[:to].matches?(states[:to], from: from)
|
|
750
|
+
end
|
|
751
|
+
|
|
752
|
+
# Whether every expected callable (one or an Array) is among the defined
|
|
753
|
+
# ones. Method names compare as String/Symbol alike; anything else, such as
|
|
754
|
+
# a lambda, only matches itself.
|
|
755
|
+
def _sm_includes_callables?(defined, expected)
|
|
756
|
+
defined = Array(defined)
|
|
757
|
+
name = ->(value) { value.is_a?(Symbol) || value.is_a?(String) }
|
|
758
|
+
Array(expected).all? do |wanted|
|
|
759
|
+
defined.any? do |candidate|
|
|
760
|
+
name.call(candidate) && name.call(wanted) ? candidate.to_s == wanted.to_s : candidate.equal?(wanted)
|
|
761
|
+
end
|
|
762
|
+
end
|
|
763
|
+
end
|
|
764
|
+
|
|
701
765
|
# Internal helper for checking transition callbacks
|
|
702
766
|
def _assert_transition_callback(callback_type, machine_or_class, options, message)
|
|
703
767
|
# Get the machine
|
|
704
768
|
machine = machine_or_class.is_a?(StateMachines::Machine) ? machine_or_class : machine_or_class.state_machine
|
|
705
769
|
raise ArgumentError, 'No state machine found' unless machine
|
|
706
770
|
|
|
707
|
-
|
|
771
|
+
# Each candidate is [callback, branch it is matched against]: a
|
|
772
|
+
# machine-wide callback has its own branch; a transition's before: /
|
|
773
|
+
# around: / after: callback is matched against that transition branch
|
|
774
|
+
# (its event, states and guards). Before includes around, as the
|
|
775
|
+
# machine keeps both in one chain.
|
|
776
|
+
branch_types = callback_type == :before ? %i[before around] : [callback_type]
|
|
777
|
+
candidates = (machine.callbacks[callback_type] || []).map { |callback| [callback, callback.branch] }
|
|
778
|
+
machine.events.each do |event|
|
|
779
|
+
event.branches.each do |branch|
|
|
780
|
+
branch.callbacks.each { |callback| candidates << [callback, branch] if branch_types.include?(callback.type) }
|
|
781
|
+
end
|
|
782
|
+
end
|
|
708
783
|
|
|
709
|
-
# Extract expected conditions
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
784
|
+
# Extract expected conditions. States and events resolve to their
|
|
785
|
+
# canonical names first, since the branch matchers compare those: a raw
|
|
786
|
+
# 'parked' would never equal :parked, and would pass any all - :parked.
|
|
787
|
+
# An unknown name raises IndexError.
|
|
788
|
+
expected_event = machine.events.fetch(options[:on]).name if options.key?(:on)
|
|
789
|
+
states = options.slice(:from, :to).transform_values { |state| machine.states.fetch(state).name }
|
|
790
|
+
expected_from = states[:from]
|
|
791
|
+
expected_to = states[:to]
|
|
713
792
|
expected_method = options[:do]
|
|
714
793
|
expected_if = options[:if]
|
|
715
794
|
expected_unless = options[:unless]
|
|
716
795
|
|
|
717
|
-
# Find matching callback
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
if
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
false
|
|
728
|
-
end
|
|
729
|
-
next false unless event_matches
|
|
730
|
-
end
|
|
731
|
-
|
|
732
|
-
# Check state requirements (from/to)
|
|
733
|
-
if expected_from || expected_to
|
|
734
|
-
state_matches = false
|
|
735
|
-
branch.state_requirements.each do |req|
|
|
736
|
-
from_matches = !expected_from || (req[:from] && req[:from].respond_to?(:values) && req[:from].values.include?(expected_from))
|
|
737
|
-
to_matches = !expected_to || (req[:to] && req[:to].respond_to?(:values) && req[:to].values.include?(expected_to))
|
|
738
|
-
|
|
739
|
-
if from_matches && to_matches
|
|
740
|
-
state_matches = true
|
|
741
|
-
break
|
|
742
|
-
end
|
|
743
|
-
end
|
|
744
|
-
next false unless state_matches
|
|
745
|
-
end
|
|
746
|
-
|
|
747
|
-
# Check method requirement
|
|
748
|
-
if expected_method
|
|
749
|
-
methods = callback.instance_variable_get(:@methods) || []
|
|
750
|
-
method_matches = methods.any? do |method|
|
|
751
|
-
(method.is_a?(Symbol) && method == expected_method) ||
|
|
752
|
-
(method.is_a?(String) && method.to_sym == expected_method) ||
|
|
753
|
-
(method.respond_to?(:call) && method.respond_to?(:source_location))
|
|
754
|
-
end
|
|
755
|
-
next false unless method_matches
|
|
756
|
-
end
|
|
757
|
-
|
|
758
|
-
# Check if condition
|
|
759
|
-
if expected_if
|
|
760
|
-
if_condition = branch.if_condition
|
|
761
|
-
if_matches = (if_condition.is_a?(Symbol) && if_condition == expected_if) ||
|
|
762
|
-
(if_condition.is_a?(String) && if_condition.to_sym == expected_if) ||
|
|
763
|
-
if_condition.respond_to?(:call)
|
|
764
|
-
next false unless if_matches
|
|
765
|
-
end
|
|
766
|
-
|
|
767
|
-
# Check unless condition
|
|
768
|
-
if expected_unless
|
|
769
|
-
unless_condition = branch.unless_condition
|
|
770
|
-
unless_matches = (unless_condition.is_a?(Symbol) && unless_condition == expected_unless) ||
|
|
771
|
-
(unless_condition.is_a?(String) && unless_condition.to_sym == expected_unless) ||
|
|
772
|
-
unless_condition.respond_to?(:call)
|
|
773
|
-
next false unless unless_matches
|
|
774
|
-
end
|
|
796
|
+
# Find matching callback. Requirements are checked with the matchers'
|
|
797
|
+
# own matches?, so all / all - [...] / same behave as they do at runtime,
|
|
798
|
+
# and an expected nil state is an expectation, not an omission.
|
|
799
|
+
matching_callback = candidates.find do |(callback, branch)|
|
|
800
|
+
next false if options.key?(:on) && !branch.event_requirement.matches?(expected_event)
|
|
801
|
+
next false if states.any? &&
|
|
802
|
+
branch.state_requirements.none? { |requirement| _sm_state_requirement_matches?(requirement, states) }
|
|
803
|
+
next false if options.key?(:do) && !_sm_includes_callables?(callback.callables, expected_method)
|
|
804
|
+
next false if options.key?(:if) && !_sm_includes_callables?(branch.if_condition, expected_if)
|
|
805
|
+
next false if options.key?(:unless) && !_sm_includes_callables?(branch.unless_condition, expected_unless)
|
|
775
806
|
|
|
776
807
|
true
|
|
777
808
|
end
|
|
@@ -779,12 +810,12 @@ module StateMachines
|
|
|
779
810
|
return if matching_callback
|
|
780
811
|
|
|
781
812
|
expected_parts = []
|
|
782
|
-
expected_parts << "on: #{expected_event.inspect}" if
|
|
783
|
-
expected_parts << "from: #{expected_from.inspect}" if
|
|
784
|
-
expected_parts << "to: #{expected_to.inspect}" if
|
|
785
|
-
expected_parts << "do: #{expected_method.inspect}" if
|
|
786
|
-
expected_parts << "if: #{expected_if.inspect}" if
|
|
787
|
-
expected_parts << "unless: #{expected_unless.inspect}" if
|
|
813
|
+
expected_parts << "on: #{expected_event.inspect}" if options.key?(:on)
|
|
814
|
+
expected_parts << "from: #{expected_from.inspect}" if options.key?(:from)
|
|
815
|
+
expected_parts << "to: #{expected_to.inspect}" if options.key?(:to)
|
|
816
|
+
expected_parts << "do: #{expected_method.inspect}" if options.key?(:do)
|
|
817
|
+
expected_parts << "if: #{expected_if.inspect}" if options.key?(:if)
|
|
818
|
+
expected_parts << "unless: #{expected_unless.inspect}" if options.key?(:unless)
|
|
788
819
|
|
|
789
820
|
default_message = "Expected #{callback_type}_transition callback with #{expected_parts.join(', ')} to be defined, but it was not found"
|
|
790
821
|
|