state_machines 0.302.0 → 0.303.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 +2 -1
- 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/machine/action_hooks.rb +13 -10
- 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/test_helper.rb +119 -97
- data/lib/state_machines/version.rb +1 -1
- metadata +1 -2
- data/lib/state_machines/machine/validation.rb +0 -39
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 9a29b27e5695470275a9549dd92e27dce0e37c3bdf00541bcfd1fc5194aa506c
|
|
4
|
+
data.tar.gz: 05f7e2bf30fe31ca8f616d2983a4ea2fa248a1b585f529c79ad7dd5f012c77ab
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: ca1284da8da46118da332893513394e21afdadc7aa7f759327787c6e0d55d8acff3ba52e981e5cac1f2527ee3419a42d3f08443ad6c3d2b66d625c1ad246659d
|
|
7
|
+
data.tar.gz: c571cc39176f7d0f20e4cd75c76734a76b508678811d48faa2d34a41bc4cc72e01c063c91ee8c74861e47e3d25640df8853bdd7fe0bab4afd9481bb449035491
|
data/README.md
CHANGED
|
@@ -211,7 +211,8 @@ vehicle # => #<Vehicle:0xb7cf4eac @state="first_gear", @
|
|
|
211
211
|
vehicle.fire_state_event(:shift_up) # => #<StateMachines::Transition attribute=:state event=:shift_up ...>
|
|
212
212
|
|
|
213
213
|
# Call state-driven behavior that's undefined for the state raises a NoMethodError
|
|
214
|
-
|
|
214
|
+
# (StateMachines::InvalidContext)
|
|
215
|
+
vehicle.speed # => StateMachines::InvalidContext: State :second_gear for :state is not a valid context for calling #speed
|
|
215
216
|
vehicle # => #<Vehicle:0xb7cf4eac @state="second_gear", @seatbelt_on=true>
|
|
216
217
|
|
|
217
218
|
# The bang (!) operator can raise exceptions if the event fails
|
|
@@ -108,6 +108,18 @@ module StateMachines
|
|
|
108
108
|
# See StateMachines::Branch for more information.
|
|
109
109
|
attr_reader :branch
|
|
110
110
|
|
|
111
|
+
# The methods this callback runs, as configured: Symbols / Strings naming
|
|
112
|
+
# methods on the object, and the user's own Procs and Methods, in order
|
|
113
|
+
# (positional arguments, then +:do+, then the block). With
|
|
114
|
+
# +bind_to_object+ the Procs are still the user's own, so their identity,
|
|
115
|
+
# +source_location+ and +inspect+ describe the user's code. Frozen.
|
|
116
|
+
#
|
|
117
|
+
# callback = StateMachines::Callback.new(:before, :log, do: ->(vehicle) {})
|
|
118
|
+
# callback.callables # => [:log, #<Proc:0x...(lambda)>]
|
|
119
|
+
def callables
|
|
120
|
+
@methods
|
|
121
|
+
end
|
|
122
|
+
|
|
111
123
|
# Creates a new callback that can get called based on the configured
|
|
112
124
|
# options.
|
|
113
125
|
#
|
|
@@ -134,11 +146,13 @@ module StateMachines
|
|
|
134
146
|
|
|
135
147
|
options = { bind_to_object: self.class.bind_to_object, terminator: self.class.terminator }.merge(options)
|
|
136
148
|
|
|
137
|
-
#
|
|
149
|
+
# Blocks bound to the object run through a proxy, but @methods keeps the
|
|
150
|
+
# user's own blocks: their identity, source_location and inspect are
|
|
151
|
+
# what introspection and renderers report, not the proxy's
|
|
138
152
|
bind_to_object = options.delete(:bind_to_object)
|
|
139
|
-
@
|
|
140
|
-
|
|
141
|
-
|
|
153
|
+
@bound_methods = {}.compare_by_identity
|
|
154
|
+
@methods.each { |method| @bound_methods[method] ||= bound_method(method) if bind_to_object && method.is_a?(Proc) }
|
|
155
|
+
@methods.freeze
|
|
142
156
|
|
|
143
157
|
@terminator = options.delete(:terminator)
|
|
144
158
|
@branch = Branch.new(options)
|
|
@@ -182,7 +196,7 @@ module StateMachines
|
|
|
182
196
|
current_method = @methods[index]
|
|
183
197
|
if current_method
|
|
184
198
|
yielded = false
|
|
185
|
-
evaluate_method(object, current_method, *) do
|
|
199
|
+
evaluate_method(object, invoker(current_method), *) do
|
|
186
200
|
yielded = true
|
|
187
201
|
run_methods(object, context, index + 1, *, &)
|
|
188
202
|
end
|
|
@@ -193,12 +207,18 @@ module StateMachines
|
|
|
193
207
|
end
|
|
194
208
|
else
|
|
195
209
|
@methods.each do |method|
|
|
196
|
-
result = evaluate_method(object, method, *)
|
|
210
|
+
result = evaluate_method(object, invoker(method), *)
|
|
197
211
|
throw :halt if @terminator&.call(result)
|
|
198
212
|
end
|
|
199
213
|
end
|
|
200
214
|
end
|
|
201
215
|
|
|
216
|
+
# The callable to evaluate for a configured method: its bound proxy when
|
|
217
|
+
# the callback binds blocks to the object, otherwise the method itself
|
|
218
|
+
def invoker(method)
|
|
219
|
+
@bound_methods.fetch(method, method)
|
|
220
|
+
end
|
|
221
|
+
|
|
202
222
|
# Generates a method that can be bound to the object being transitioned
|
|
203
223
|
# when the callback is invoked
|
|
204
224
|
def bound_method(block)
|
data/lib/state_machines/error.rb
CHANGED
|
@@ -146,7 +146,18 @@ module StateMachines
|
|
|
146
146
|
end
|
|
147
147
|
end
|
|
148
148
|
|
|
149
|
-
# A method was called
|
|
150
|
-
|
|
149
|
+
# A state-specific method was called on an object whose current state does
|
|
150
|
+
# not define it. It is a NoMethodError, as documented for state behaviors:
|
|
151
|
+
# the method does not exist for the object in that state. #object is the
|
|
152
|
+
# receiver, as on the other StateMachines errors.
|
|
153
|
+
class InvalidContext < NoMethodError
|
|
154
|
+
def initialize(object, message = nil, name = nil, args = []) # :nodoc:
|
|
155
|
+
super(message, name, args, receiver: object)
|
|
156
|
+
end
|
|
157
|
+
|
|
158
|
+
# The object the method was called on
|
|
159
|
+
def object
|
|
160
|
+
receiver
|
|
161
|
+
end
|
|
151
162
|
end
|
|
152
163
|
end
|
|
@@ -192,26 +192,37 @@ module StateMachines
|
|
|
192
192
|
end
|
|
193
193
|
end
|
|
194
194
|
|
|
195
|
-
|
|
195
|
+
# Patterns refused in String conditions before they are evaluated
|
|
196
|
+
DANGEROUS_PATTERNS = [
|
|
197
|
+
/`.*`/, # Backticks (shell execution)
|
|
198
|
+
/system\s*\(/, # System calls
|
|
199
|
+
/exec\s*\(/, # Exec calls
|
|
200
|
+
/eval\s*\(/, # Nested eval
|
|
201
|
+
/require\s+['"]/, # Require statements
|
|
202
|
+
/load\s+['"]/, # Load statements
|
|
203
|
+
/File\./, # File operations
|
|
204
|
+
/IO\./, # IO operations
|
|
205
|
+
/Dir\./, # Directory operations
|
|
206
|
+
/Kernel\./ # Kernel operations
|
|
207
|
+
].freeze
|
|
208
|
+
private_constant :DANGEROUS_PATTERNS
|
|
196
209
|
|
|
197
|
-
#
|
|
198
|
-
# This is a basic safety check
|
|
210
|
+
# Checks a String condition or callback exactly as core does before
|
|
211
|
+
# evaluating it. This is a basic safety check, not a sandbox.
|
|
212
|
+
#
|
|
213
|
+
# StateMachines::EvalHelpers.validate_eval_string('vehicle.seatbelt_on?') # => nil
|
|
214
|
+
# StateMachines::EvalHelpers.validate_eval_string('`ls`') # => SecurityError
|
|
215
|
+
# StateMachines::EvalHelpers.validate_eval_string('if true') # => ArgumentError
|
|
216
|
+
#
|
|
217
|
+
# Raises SecurityError for a pattern it refuses (shell, eval, require,
|
|
218
|
+
# File / IO / Dir / Kernel access) and ArgumentError for invalid syntax.
|
|
219
|
+
# +yield+ is allowed, as a String evaluated with a block may yield.
|
|
220
|
+
# Returns nil when the string may be evaluated.
|
|
221
|
+
#
|
|
222
|
+
# A module function: public on the module, and private in the classes
|
|
223
|
+
# that include EvalHelpers, where evaluation calls it.
|
|
199
224
|
def validate_eval_string(method_string)
|
|
200
|
-
|
|
201
|
-
dangerous_patterns = [
|
|
202
|
-
/`.*`/, # Backticks (shell execution)
|
|
203
|
-
/system\s*\(/, # System calls
|
|
204
|
-
/exec\s*\(/, # Exec calls
|
|
205
|
-
/eval\s*\(/, # Nested eval
|
|
206
|
-
/require\s+['"]/, # Require statements
|
|
207
|
-
/load\s+['"]/, # Load statements
|
|
208
|
-
/File\./, # File operations
|
|
209
|
-
/IO\./, # IO operations
|
|
210
|
-
/Dir\./, # Directory operations
|
|
211
|
-
/Kernel\./ # Kernel operations
|
|
212
|
-
]
|
|
213
|
-
|
|
214
|
-
dangerous_patterns.each do |pattern|
|
|
225
|
+
DANGEROUS_PATTERNS.each do |pattern|
|
|
215
226
|
raise SecurityError, "Potentially dangerous code detected in eval string: #{method_string.inspect}" if method_string.match?(pattern)
|
|
216
227
|
end
|
|
217
228
|
|
|
@@ -222,6 +233,8 @@ module StateMachines
|
|
|
222
233
|
rescue SyntaxError => e
|
|
223
234
|
raise ArgumentError, "Invalid Ruby syntax in eval string: #{e.message}"
|
|
224
235
|
end
|
|
236
|
+
nil
|
|
225
237
|
end
|
|
238
|
+
module_function :validate_eval_string
|
|
226
239
|
end
|
|
227
240
|
end
|
|
@@ -27,16 +27,19 @@ module StateMachines
|
|
|
27
27
|
def define_action_hook
|
|
28
28
|
action_hook = self.action_hook
|
|
29
29
|
action = self.action
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
30
|
+
# The hook replaces the action, so it keeps its exact visibility
|
|
31
|
+
visibility = if owner_class.private_method_defined?(action_hook) then :private
|
|
32
|
+
elsif owner_class.protected_method_defined?(action_hook) then :protected
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
# define_method, not a def in evaluated source: the action is a method
|
|
36
|
+
# the owner already has, and Ruby allows names (:"save it") that no def
|
|
37
|
+
# can spell
|
|
38
|
+
helper = @helper_modules[:instance]
|
|
39
|
+
helper.send(:define_method, action_hook) do |*args, **kwargs, &block|
|
|
40
|
+
self.class.state_machines.transitions(self, action).perform { super(*args, **kwargs, &block) }
|
|
41
|
+
end
|
|
42
|
+
helper.send(visibility, action_hook) if visibility
|
|
40
43
|
end
|
|
41
44
|
|
|
42
45
|
# The method to hook into for triggering transitions when invoked. By
|
|
@@ -33,6 +33,9 @@ module StateMachines
|
|
|
33
33
|
|
|
34
34
|
# Set machine configuration
|
|
35
35
|
@name = args.first || :state
|
|
36
|
+
# The name builds the default attribute and the @<name>_event /
|
|
37
|
+
# @<name>_event_transition ivars, so it must be an ivar-safe name
|
|
38
|
+
assert_ivar_name!(@name, 'state machine name')
|
|
36
39
|
@attribute = options[:attribute] || @name
|
|
37
40
|
@events = EventCollection.new(self)
|
|
38
41
|
@states = StateCollection.new(self)
|
|
@@ -30,22 +30,35 @@ module StateMachines
|
|
|
30
30
|
# Adds reader/writer methods for accessing the state attribute
|
|
31
31
|
def define_state_accessor
|
|
32
32
|
attribute = self.attribute
|
|
33
|
+
reader = !owner_class_ancestor_has_method?(:instance, attribute)
|
|
34
|
+
writer = !owner_class_ancestor_has_method?(:instance, "#{attribute}=")
|
|
35
|
+
unless ivar_name?(attribute)
|
|
36
|
+
# attr_reader / attr_writer store in @<attribute>, so core cannot
|
|
37
|
+
# define them; fine only if the owner class defines both itself
|
|
38
|
+
return if owner_class_has_method?(:instance, attribute) && owner_class_has_method?(:instance, "#{attribute}=")
|
|
39
|
+
|
|
40
|
+
raise_invalid_ivar_name(attribute, 'state machine attribute')
|
|
41
|
+
end
|
|
33
42
|
|
|
34
|
-
@helper_modules[:instance].
|
|
35
|
-
@helper_modules[:instance].
|
|
43
|
+
@helper_modules[:instance].send(:attr_reader, attribute) if reader
|
|
44
|
+
@helper_modules[:instance].send(:attr_writer, attribute) if writer
|
|
36
45
|
end
|
|
37
46
|
|
|
38
47
|
# Adds predicate method to the owner class for determining the name of the
|
|
39
48
|
# current state
|
|
40
49
|
def define_state_predicate
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
50
|
+
machine_name = name
|
|
51
|
+
call_super = owner_class_ancestor_has_method?(:instance, "#{name}?")
|
|
52
|
+
# Defined directly rather than through define_helper, which would skip
|
|
53
|
+
# it with a conflict warning: this predicate deliberately wraps an
|
|
54
|
+
# inherited one, deferring to it when called without arguments
|
|
55
|
+
@helper_modules[:instance].send(:define_method, "#{name}?") do |*args|
|
|
56
|
+
if args.empty? && (call_super || defined?(super))
|
|
57
|
+
super()
|
|
58
|
+
else
|
|
59
|
+
self.class.state_machine(machine_name).states.matches?(self, *args)
|
|
60
|
+
end
|
|
61
|
+
end
|
|
49
62
|
end
|
|
50
63
|
|
|
51
64
|
# Adds helper methods for getting information about this state machine's
|
|
@@ -85,9 +98,9 @@ module StateMachines
|
|
|
85
98
|
end
|
|
86
99
|
|
|
87
100
|
event_transition_attribute = attribute(:event_transition)
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
101
|
+
helper = @helper_modules[:instance]
|
|
102
|
+
helper.send(:attr_accessor, event_transition_attribute)
|
|
103
|
+
helper.send(:protected, event_transition_attribute, :"#{event_transition_attribute}=")
|
|
91
104
|
end
|
|
92
105
|
|
|
93
106
|
# Adds helper methods for getting information about this state machine's
|
|
@@ -271,14 +271,15 @@ module StateMachines
|
|
|
271
271
|
# === Invalid behaviors
|
|
272
272
|
#
|
|
273
273
|
# If a specific behavior has not been defined for a state, then a
|
|
274
|
-
# NoMethodError
|
|
275
|
-
# not normally exist for an object with
|
|
274
|
+
# StateMachines::InvalidContext, which is a NoMethodError, will be raised,
|
|
275
|
+
# indicating that that method would not normally exist for an object with
|
|
276
|
+
# that state.
|
|
276
277
|
#
|
|
277
278
|
# Using the example from before:
|
|
278
279
|
#
|
|
279
280
|
# vehicle = Vehicle.new
|
|
280
281
|
# vehicle.state = 'backing_up'
|
|
281
|
-
# vehicle.speed # =>
|
|
282
|
+
# vehicle.speed # => StateMachines::InvalidContext: State :backing_up for :state is not a valid context for calling #speed
|
|
282
283
|
#
|
|
283
284
|
# === Using matchers
|
|
284
285
|
#
|
|
@@ -3,8 +3,32 @@
|
|
|
3
3
|
module StateMachines
|
|
4
4
|
class Machine
|
|
5
5
|
module Utilities
|
|
6
|
+
# Probed (never written) to ask Ruby whether a name is a legal instance
|
|
7
|
+
# variable name, which also makes it a legal attr_accessor name
|
|
8
|
+
IVAR_PROBE = Object.new.freeze
|
|
9
|
+
private_constant :IVAR_PROBE
|
|
10
|
+
|
|
6
11
|
protected
|
|
7
12
|
|
|
13
|
+
# Whether +name+ makes a valid instance variable name (@name)
|
|
14
|
+
def ivar_name?(name)
|
|
15
|
+
IVAR_PROBE.instance_variable_defined?(:"@#{name}")
|
|
16
|
+
true
|
|
17
|
+
rescue NameError
|
|
18
|
+
false
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
# Raises an ArgumentError unless +name+ makes a valid instance variable
|
|
22
|
+
# name, so a bad name fails where it is given rather than later from
|
|
23
|
+
# attr_accessor or instance_variable_get
|
|
24
|
+
def assert_ivar_name!(name, role)
|
|
25
|
+
raise_invalid_ivar_name(name, role) unless ivar_name?(name)
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def raise_invalid_ivar_name(name, role)
|
|
29
|
+
raise ArgumentError, "#{name.inspect} is not a valid #{role}: it must be usable as an instance variable name (@#{name})"
|
|
30
|
+
end
|
|
31
|
+
|
|
8
32
|
# Looks up other machines that have been defined in the owner class and
|
|
9
33
|
# are targeting the same attribute as this machine. When accessing
|
|
10
34
|
# sibling machines, they will be automatically copied for the current
|
|
@@ -4,7 +4,6 @@ require_relative 'options_validator'
|
|
|
4
4
|
require_relative 'machine/class_methods'
|
|
5
5
|
require_relative 'machine/utilities'
|
|
6
6
|
require_relative 'machine/parsing'
|
|
7
|
-
require_relative 'machine/validation'
|
|
8
7
|
require_relative 'machine/helper_generators'
|
|
9
8
|
require_relative 'machine/action_hooks'
|
|
10
9
|
require_relative 'machine/scoping'
|
|
@@ -421,7 +420,6 @@ module StateMachines
|
|
|
421
420
|
include MatcherHelpers
|
|
422
421
|
include Utilities
|
|
423
422
|
include Parsing
|
|
424
|
-
include Validation
|
|
425
423
|
include HelperGenerators
|
|
426
424
|
include ActionHooks
|
|
427
425
|
include Scoping
|
|
@@ -4,8 +4,11 @@ require_relative 'options_validator'
|
|
|
4
4
|
|
|
5
5
|
module StateMachines
|
|
6
6
|
# Represents a collection of nodes in a state machine, be it events or states.
|
|
7
|
-
#
|
|
8
|
-
#
|
|
7
|
+
# Lookups in a string-keyed index (by default, every index) treat a key and
|
|
8
|
+
# its string form as the same key (:parked, 'parked', and 10 / '10' / :'10'
|
|
9
|
+
# for numeric keys). The one exception is nil: it is only ever found by nil,
|
|
10
|
+
# since nil.to_s is '' and would otherwise collide with a node keyed by :"" or
|
|
11
|
+
# ''. Other indices only find a node by an eql? key.
|
|
9
12
|
class NodeCollection
|
|
10
13
|
include Enumerable
|
|
11
14
|
|
|
@@ -19,17 +22,22 @@ module StateMachines
|
|
|
19
22
|
# * <tt>:index</tt> - One or more attributes to automatically generate
|
|
20
23
|
# hashed indices for in order to perform quick lookups. Default is to
|
|
21
24
|
# index by the :name attribute
|
|
25
|
+
# * <tt>:string_keys</tt> - The indices that also find a node by the string
|
|
26
|
+
# form of its key. Default is every index
|
|
22
27
|
def initialize(machine, options = {})
|
|
23
|
-
StateMachines::OptionsValidator.assert_valid_keys!(options, :index)
|
|
28
|
+
StateMachines::OptionsValidator.assert_valid_keys!(options, :index, :string_keys)
|
|
24
29
|
options = { index: :name }.merge(options)
|
|
25
30
|
|
|
26
31
|
@machine = machine
|
|
27
32
|
@nodes = []
|
|
28
33
|
@index_names = Array(options[:index])
|
|
34
|
+
@string_keys = Array(options.fetch(:string_keys, @index_names))
|
|
35
|
+
unknown = @string_keys - @index_names
|
|
36
|
+
raise ArgumentError, "Invalid string_keys: #{unknown.inspect} not in the indices" if unknown.any?
|
|
37
|
+
|
|
29
38
|
@indices = @index_names.each_with_object({}) do |name, indices|
|
|
30
39
|
indices[name] = {}
|
|
31
|
-
indices[:"#{name}_to_s"] = {}
|
|
32
|
-
indices[:"#{name}_to_sym"] = {}
|
|
40
|
+
indices[:"#{name}_to_s"] = {} if @string_keys.include?(name)
|
|
33
41
|
end
|
|
34
42
|
@default_index = Array(options[:index]).first
|
|
35
43
|
@contexts = []
|
|
@@ -146,9 +154,7 @@ module StateMachines
|
|
|
146
154
|
# If the key cannot be found, then nil will be returned.
|
|
147
155
|
def [](key, index_name = @default_index)
|
|
148
156
|
index(index_name)[key] ||
|
|
149
|
-
index(:"#{index_name}_to_s")[key.to_s]
|
|
150
|
-
(to_sym?(key) && index(:"#{index_name}_to_sym")[:"#{key}"]) ||
|
|
151
|
-
nil
|
|
157
|
+
(string_key?(index_name, key) ? index(:"#{index_name}_to_s")[key.to_s] : nil)
|
|
152
158
|
end
|
|
153
159
|
|
|
154
160
|
# Gets the node indexed by the given key. By default, this will look up the
|
|
@@ -182,24 +188,26 @@ module StateMachines
|
|
|
182
188
|
node.send(attribute)
|
|
183
189
|
end
|
|
184
190
|
|
|
185
|
-
#
|
|
186
|
-
#
|
|
191
|
+
# Whether the key has a string version in the given index. A nil key has
|
|
192
|
+
# none: nil.to_s is '', which would make nil and :"" the same key.
|
|
193
|
+
def string_key?(name, key)
|
|
194
|
+
!key.nil? && @string_keys.include?(name)
|
|
195
|
+
end
|
|
196
|
+
|
|
197
|
+
# Adds the given key / node combination to an index, including its string
|
|
198
|
+
# version where the index has one
|
|
187
199
|
def add_to_index(name, key, node)
|
|
188
200
|
index(name)[key] = node
|
|
189
|
-
index(:"#{name}_to_s")[key.to_s] = node
|
|
190
|
-
index(:"#{name}_to_sym")[:"#{key}"] = node if to_sym?(key)
|
|
201
|
+
index(:"#{name}_to_s")[key.to_s] = node if string_key?(name, key)
|
|
191
202
|
end
|
|
192
203
|
|
|
193
|
-
# Removes the given key from an index, including
|
|
194
|
-
# versions of the index
|
|
204
|
+
# Removes the given key from an index, including its string version
|
|
195
205
|
def remove_from_index(name, key)
|
|
196
206
|
index(name).delete(key)
|
|
197
|
-
index(:"#{name}_to_s").delete(key.to_s)
|
|
198
|
-
index(:"#{name}_to_sym").delete(:"#{key}") if to_sym?(key)
|
|
207
|
+
index(:"#{name}_to_s").delete(key.to_s) if string_key?(name, key)
|
|
199
208
|
end
|
|
200
209
|
|
|
201
|
-
# Updates the node for the given index, including
|
|
202
|
-
# versions of the index
|
|
210
|
+
# Updates the node for the given index, including its string version
|
|
203
211
|
def update_index(name, node)
|
|
204
212
|
index = self.index(name)
|
|
205
213
|
old_key = index.key(node)
|
|
@@ -212,11 +220,6 @@ module StateMachines
|
|
|
212
220
|
add_to_index(name, new_key, node)
|
|
213
221
|
end
|
|
214
222
|
|
|
215
|
-
# Determines whether the given value can be converted to a symbol
|
|
216
|
-
def to_sym?(value)
|
|
217
|
-
"#{value}" != ''
|
|
218
|
-
end
|
|
219
|
-
|
|
220
223
|
# Evaluates the given context for a particular node. This will only
|
|
221
224
|
# evaluate the context if the node matches.
|
|
222
225
|
def eval_context(context, node)
|
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
|
|
@@ -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,6 +728,40 @@ 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
|
|
@@ -706,72 +770,30 @@ module StateMachines
|
|
|
706
770
|
|
|
707
771
|
callbacks = machine.callbacks[callback_type] || []
|
|
708
772
|
|
|
709
|
-
# Extract expected conditions
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
773
|
+
# Extract expected conditions. States and events resolve to their
|
|
774
|
+
# canonical names first, since the branch matchers compare those: a raw
|
|
775
|
+
# 'parked' would never equal :parked, and would pass any all - :parked.
|
|
776
|
+
# An unknown name raises IndexError.
|
|
777
|
+
expected_event = machine.events.fetch(options[:on]).name if options.key?(:on)
|
|
778
|
+
states = options.slice(:from, :to).transform_values { |state| machine.states.fetch(state).name }
|
|
779
|
+
expected_from = states[:from]
|
|
780
|
+
expected_to = states[:to]
|
|
713
781
|
expected_method = options[:do]
|
|
714
782
|
expected_if = options[:if]
|
|
715
783
|
expected_unless = options[:unless]
|
|
716
784
|
|
|
717
|
-
# Find matching callback
|
|
785
|
+
# Find matching callback. Requirements are checked with the matchers'
|
|
786
|
+
# own matches?, so all / all - [...] / same behave as they do at runtime,
|
|
787
|
+
# and an expected nil state is an expectation, not an omission.
|
|
718
788
|
matching_callback = callbacks.find do |callback|
|
|
719
789
|
branch = callback.branch
|
|
720
790
|
|
|
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
|
|
791
|
+
next false if options.key?(:on) && !branch.event_requirement.matches?(expected_event)
|
|
792
|
+
next false if states.any? &&
|
|
793
|
+
branch.state_requirements.none? { |requirement| _sm_state_requirement_matches?(requirement, states) }
|
|
794
|
+
next false if options.key?(:do) && !_sm_includes_callables?(callback.callables, expected_method)
|
|
795
|
+
next false if options.key?(:if) && !_sm_includes_callables?(branch.if_condition, expected_if)
|
|
796
|
+
next false if options.key?(:unless) && !_sm_includes_callables?(branch.unless_condition, expected_unless)
|
|
775
797
|
|
|
776
798
|
true
|
|
777
799
|
end
|
|
@@ -779,12 +801,12 @@ module StateMachines
|
|
|
779
801
|
return if matching_callback
|
|
780
802
|
|
|
781
803
|
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
|
|
804
|
+
expected_parts << "on: #{expected_event.inspect}" if options.key?(:on)
|
|
805
|
+
expected_parts << "from: #{expected_from.inspect}" if options.key?(:from)
|
|
806
|
+
expected_parts << "to: #{expected_to.inspect}" if options.key?(:to)
|
|
807
|
+
expected_parts << "do: #{expected_method.inspect}" if options.key?(:do)
|
|
808
|
+
expected_parts << "if: #{expected_if.inspect}" if options.key?(:if)
|
|
809
|
+
expected_parts << "unless: #{expected_unless.inspect}" if options.key?(:unless)
|
|
788
810
|
|
|
789
811
|
default_message = "Expected #{callback_type}_transition callback with #{expected_parts.join(', ')} to be defined, but it was not found"
|
|
790
812
|
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: state_machines
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.303.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Abdelkader Boudih
|
|
@@ -88,7 +88,6 @@ files:
|
|
|
88
88
|
- lib/state_machines/machine/scoping.rb
|
|
89
89
|
- lib/state_machines/machine/state_methods.rb
|
|
90
90
|
- lib/state_machines/machine/utilities.rb
|
|
91
|
-
- lib/state_machines/machine/validation.rb
|
|
92
91
|
- lib/state_machines/machine_collection.rb
|
|
93
92
|
- lib/state_machines/macro_methods.rb
|
|
94
93
|
- lib/state_machines/matcher.rb
|
|
@@ -1,39 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
module StateMachines
|
|
4
|
-
class Machine
|
|
5
|
-
module Validation
|
|
6
|
-
# Frozen constant to avoid repeated array allocations
|
|
7
|
-
DANGEROUS_PATTERNS = [
|
|
8
|
-
/`.*`/, # Backticks (shell execution)
|
|
9
|
-
/system\s*\(/, # System calls
|
|
10
|
-
/exec\s*\(/, # Exec calls
|
|
11
|
-
/eval\s*\(/, # Nested eval
|
|
12
|
-
/require\s+['"]/, # Require statements
|
|
13
|
-
/load\s+['"]/, # Load statements
|
|
14
|
-
/File\./, # File operations
|
|
15
|
-
/IO\./, # IO operations
|
|
16
|
-
/Dir\./, # Directory operations
|
|
17
|
-
/Kernel\./ # Kernel operations
|
|
18
|
-
].freeze
|
|
19
|
-
|
|
20
|
-
private
|
|
21
|
-
|
|
22
|
-
# Validates string input before eval to prevent code injection
|
|
23
|
-
# This is a basic safety check - not foolproof security
|
|
24
|
-
def validate_eval_string(method_string)
|
|
25
|
-
# Check for obviously dangerous patterns
|
|
26
|
-
DANGEROUS_PATTERNS.each do |pattern|
|
|
27
|
-
raise SecurityError, "Potentially dangerous code detected in eval string: #{method_string.inspect}" if method_string.match?(pattern)
|
|
28
|
-
end
|
|
29
|
-
|
|
30
|
-
# Basic syntax validation (cross-platform)
|
|
31
|
-
begin
|
|
32
|
-
SyntaxValidator.validate!(method_string, '(eval)')
|
|
33
|
-
rescue SyntaxError => e
|
|
34
|
-
raise ArgumentError, "Invalid Ruby syntax in eval string: #{e.message}"
|
|
35
|
-
end
|
|
36
|
-
end
|
|
37
|
-
end
|
|
38
|
-
end
|
|
39
|
-
end
|