state_machines 0.201.0 → 0.300.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.
@@ -16,6 +16,14 @@ module StateMachines
16
16
  # The condition that must *not* be met on an object
17
17
  attr_reader :unless_condition
18
18
 
19
+ # Conditions on the state of other machines
20
+ attr_reader :if_state_condition, :unless_state_condition,
21
+ :if_all_states_condition, :unless_all_states_condition,
22
+ :if_any_state_condition, :unless_any_state_condition
23
+
24
+ # The state guards that are set, keyed by option
25
+ attr_reader :state_guards
26
+
19
27
  # The requirement for verifying the event being matched
20
28
  attr_reader :event_requirement
21
29
 
@@ -34,12 +42,20 @@ module StateMachines
34
42
  # Build conditionals
35
43
  @if_condition = options.delete(:if)
36
44
  @unless_condition = options.delete(:unless)
37
- @if_state_condition = options.delete(:if_state)
38
- @unless_state_condition = options.delete(:unless_state)
39
- @if_all_states_condition = options.delete(:if_all_states)
40
- @unless_all_states_condition = options.delete(:unless_all_states)
41
- @if_any_state_condition = options.delete(:if_any_state)
42
- @unless_any_state_condition = options.delete(:unless_any_state)
45
+ @if_state_condition = build_state_guard(options, :if_state)
46
+ @unless_state_condition = build_state_guard(options, :unless_state)
47
+ @if_all_states_condition = build_state_guard(options, :if_all_states)
48
+ @unless_all_states_condition = build_state_guard(options, :unless_all_states)
49
+ @if_any_state_condition = build_state_guard(options, :if_any_state)
50
+ @unless_any_state_condition = build_state_guard(options, :unless_any_state)
51
+ @state_guards = {
52
+ if_state: @if_state_condition,
53
+ unless_state: @unless_state_condition,
54
+ if_all_states: @if_all_states_condition,
55
+ unless_all_states: @unless_all_states_condition,
56
+ if_any_state: @if_any_state_condition,
57
+ unless_any_state: @unless_any_state_condition
58
+ }.compact.freeze
43
59
 
44
60
  # Build event requirement
45
61
  @event_requirement = build_matcher(options, :on, :except_on)
@@ -94,7 +110,7 @@ module StateMachines
94
110
  def matches?(object, query = {})
95
111
  !match(object, query).nil?
96
112
  end
97
-
113
+
98
114
  # Alias for Minitest's assert_match
99
115
  alias =~ matches?
100
116
 
@@ -134,7 +150,7 @@ module StateMachines
134
150
  end
135
151
 
136
152
  def draw(graph, event, valid_states, io = $stdout)
137
- machine.renderer.draw_branch(self, graph, event, valid_states, io)
153
+ event.machine.renderer.draw_branch(self, graph, event, valid_states, io)
138
154
  end
139
155
 
140
156
  protected
@@ -158,6 +174,15 @@ module StateMachines
158
174
  end
159
175
  end
160
176
 
177
+ # Frozen copy, since branches are shared with subclasses
178
+ def build_state_guard(options, option)
179
+ conditions = options.delete(option)
180
+ return if conditions.nil?
181
+ raise ArgumentError, ":#{option} must be a non-empty Hash of state machine name => state name, got #{conditions.inspect}" unless conditions.is_a?(Hash) && !conditions.empty?
182
+
183
+ conditions.dup.freeze
184
+ end
185
+
161
186
  # Verifies that all configured requirements (event and state) match the
162
187
  # given query. If a match is found, then a hash containing the
163
188
  # event/state requirements that passed will be returned; otherwise, nil.
@@ -199,16 +224,6 @@ module StateMachines
199
224
 
200
225
  return false unless if_passes && unless_passes
201
226
 
202
- # Consolidate all state guards
203
- state_guards = {
204
- if_state: @if_state_condition,
205
- unless_state: @unless_state_condition,
206
- if_all_states: @if_all_states_condition,
207
- unless_all_states: @unless_all_states_condition,
208
- if_any_state: @if_any_state_condition,
209
- unless_any_state: @unless_any_state_condition
210
- }.compact
211
-
212
227
  return true if state_guards.empty?
213
228
 
214
229
  validate_and_check_state_guards(object, state_guards)
@@ -221,14 +236,12 @@ module StateMachines
221
236
  case guard_type
222
237
  when :if_state, :if_all_states
223
238
  conditions.all? { |machine, state| check_state(object, machine, state) }
224
- when :unless_state
239
+ when :unless_state, :unless_any_state
225
240
  conditions.none? { |machine, state| check_state(object, machine, state) }
226
241
  when :if_any_state
227
242
  conditions.any? { |machine, state| check_state(object, machine, state) }
228
243
  when :unless_all_states
229
244
  !conditions.all? { |machine, state| check_state(object, machine, state) }
230
- when :unless_any_state
231
- conditions.none? { |machine, state| check_state(object, machine, state) }
232
245
  end
233
246
  end
234
247
  end
@@ -240,7 +253,7 @@ module StateMachines
240
253
  state = machine.states[state_name]
241
254
  raise ArgumentError, "State '#{state_name}' is not defined in state machine '#{machine_name}'" unless state
242
255
 
243
- state.matches?(object.send(machine_name))
256
+ state.matches?(machine.read(object, :state))
244
257
  end
245
258
  end
246
259
  end
@@ -176,15 +176,15 @@ module StateMachines
176
176
  # For all other types of callbacks, this will evaluate each method in
177
177
  # order. The callback will only halt if the resulting value from the
178
178
  # method passes the terminator.
179
- def run_methods(object, context = {}, index = 0, *args, &block)
179
+ def run_methods(object, context = {}, index = 0, *, &)
180
180
  case type
181
181
  when :around
182
182
  current_method = @methods[index]
183
183
  if current_method
184
184
  yielded = false
185
- evaluate_method(object, current_method, *args) do
185
+ evaluate_method(object, current_method, *) do
186
186
  yielded = true
187
- run_methods(object, context, index + 1, *args, &block)
187
+ run_methods(object, context, index + 1, *, &)
188
188
  end
189
189
 
190
190
  throw :halt unless yielded
@@ -193,7 +193,7 @@ module StateMachines
193
193
  end
194
194
  else
195
195
  @methods.each do |method|
196
- result = evaluate_method(object, method, *args)
196
+ result = evaluate_method(object, method, *)
197
197
  throw :halt if @terminator&.call(result)
198
198
  end
199
199
  end
@@ -19,6 +19,7 @@ require 'state_machines/matcher'
19
19
  require 'state_machines/matcher_helpers'
20
20
 
21
21
  require 'state_machines/transition'
22
+ require 'state_machines/transition_claim'
22
23
  require 'state_machines/transition_collection'
23
24
 
24
25
  require 'state_machines/branch'
@@ -64,11 +64,19 @@ module StateMachines
64
64
  # The current state value for the machine
65
65
  attr_reader :from
66
66
 
67
+ # Why the transition was refused: the reason recorded for the event on the
68
+ # object when the error was built.
69
+ #
70
+ # @return [:no_transition, :guard, :halted, :action_failed, :contended, nil]
71
+ # @see StateMachines::Event#refusal
72
+ attr_reader :reason
73
+
67
74
  def initialize(object, machine, event) # :nodoc:
68
75
  @machine = machine
69
76
  @from_state = machine.states.match!(object)
70
77
  @from = machine.read(object, :state)
71
78
  @event = machine.events.fetch(event)
79
+ @reason = @event.refusal(object)
72
80
  errors = machine.errors_for(object)
73
81
 
74
82
  message = "Cannot transition #{machine.name} via :#{self.event} from #{from_name.inspect}"
@@ -102,11 +110,40 @@ module StateMachines
102
110
  # The set of events that failed the transition(s)
103
111
  attr_reader :events
104
112
 
113
+ # The refusal reason of each event that caused the failure, keyed by the
114
+ # event name as given. Events refused only because another one was are
115
+ # left out.
116
+ #
117
+ # @return [Hash{Symbol => Symbol}]
118
+ # @see StateMachines::Event#refusal
119
+ attr_reader :reasons
120
+
105
121
  def initialize(object, events) # :nodoc:
106
122
  @events = events
123
+ @reasons = refusal_reasons(object, events)
107
124
 
108
125
  super(object, "Cannot run events in parallel: #{events * ', '}")
109
126
  end
127
+
128
+ private
129
+
130
+ # Reads the recorded refusal reason of each given event on the object,
131
+ # finding each event across the object's machines by its qualified name.
132
+ #
133
+ # @param object [Object] the object the events were fired on
134
+ # @param events [Array<Symbol>] the qualified event names that were fired
135
+ # @return [Hash{Symbol => Symbol}] the reason per event name, leaving out
136
+ # events without one (and every event when the object has no machines)
137
+ def refusal_reasons(object, events)
138
+ return {} unless object.class.respond_to?(:state_machines)
139
+
140
+ machines = object.class.state_machines.values
141
+ events.each_with_object({}) do |event_name, reasons|
142
+ event = machines.lazy.filter_map { |machine| machine.events[event_name, :qualified_name] }.first
143
+ reason = event&.refusal(object)
144
+ reasons[event_name] = reason if reason
145
+ end
146
+ end
110
147
  end
111
148
 
112
149
  # A method was called in an invalid state context
@@ -9,6 +9,28 @@ module StateMachines
9
9
  class Event
10
10
  include MatcherHelpers
11
11
 
12
+ # The object instance variable holding the most recent refusal reason per
13
+ # machine and event
14
+ REFUSALS_VARIABLE = :@_state_machine_refusals
15
+
16
+ # Serialises the creation and updates of an object's refusal reasons, so
17
+ # that concurrent attempts on one object do not lose each other's reason
18
+ REFUSALS_LOCK = Thread::Mutex.new
19
+
20
+ class << self
21
+ # Forgets every refusal reason recorded on the given object, e.g. when
22
+ # it is copied or reloaded.
23
+ #
24
+ # @api private
25
+ # @param object [Object] the object whose reasons are forgotten
26
+ # @return [void]
27
+ def clear_refusals(object)
28
+ return if object.frozen? || !object.instance_variable_defined?(REFUSALS_VARIABLE)
29
+
30
+ object.remove_instance_variable(REFUSALS_VARIABLE)
31
+ end
32
+ end
33
+
12
34
  # The state machine for which this event is defined
13
35
  attr_accessor :machine
14
36
 
@@ -161,21 +183,116 @@ module StateMachines
161
183
 
162
184
  # Attempts to perform the next available transition on the given object.
163
185
  # If no transitions can be made, then this will return false, otherwise
164
- # true.
186
+ # the performed {StateMachines::Transition}.
165
187
  #
166
188
  # Any additional arguments are passed to the StateMachines::Transition#perform
167
189
  # instance method.
190
+ #
191
+ # The reason for a refusal is recorded on the object and can be read back
192
+ # with {#refusal} (or the generated `*_refusal` helper).
193
+ #
194
+ # @return [StateMachines::Transition, false] the performed transition, or
195
+ # false if the event was refused
196
+ # @see #refusal
168
197
  def fire(object, *event_args)
169
198
  machine.reset(object)
170
199
 
171
- if (transition = transition_for(object, {}, *event_args))
200
+ transition = begin
201
+ transition_for(object, {}, *event_args)
202
+ rescue StandardError
203
+ # A guard raised before this attempt had an outcome
204
+ record_refusal(object, nil)
205
+ raise
206
+ end
207
+
208
+ if transition
172
209
  transition.perform(*event_args)
173
210
  else
211
+ record_refusal(object, refusal_without_transition(object))
174
212
  on_failure(object, *event_args)
175
213
  false
176
214
  end
177
215
  end
178
216
 
217
+ # The reason the most recent attempt to fire this event on the given object
218
+ # was refused.
219
+ #
220
+ # The reasons are:
221
+ # - :no_transition - no transition is defined for the event from the
222
+ # object's current state
223
+ # - :guard - a transition is defined from the current state, but its :if /
224
+ # :unless / state guards rejected it
225
+ # - :halted - a before_transition or around_transition callback halted the
226
+ # transition
227
+ # - :action_failed - the machine's action (e.g. save) did not succeed
228
+ # - :contended - in async mode, a concurrent transition held or moved the
229
+ # attribute first
230
+ #
231
+ # When the event was fired in parallel with others (see
232
+ # {StateMachines::InstanceMethods#fire_events}), only the events that caused
233
+ # the refusal carry a reason; the others read nil. When the action failed
234
+ # because an event attribute it performed was refused (see
235
+ # {StateMachines::TransitionCollection#perform}), the event carries that
236
+ # inner reason.
237
+ #
238
+ # The reason describes the most recent attempt *on the object*, whichever
239
+ # caller made it: it is not tied to a caller. When several threads or
240
+ # fibers fire events on the same object concurrently, each should rely on
241
+ # its own result - the bang method's {StateMachines::InvalidTransition#reason}
242
+ # (or {StateMachines::Transition#refusal} of a transition it performed) -
243
+ # rather than on this reader, which another caller may have overwritten.
244
+ #
245
+ # A frozen object records no reason, so this reads nil for it. A copy
246
+ # (+dup+ / +clone+) starts with no reasons, as it has made no attempt.
247
+ #
248
+ # @param object [Object] the object the event was fired on
249
+ # @return [:no_transition, :guard, :halted, :action_failed, :contended, nil]
250
+ # the refusal reason, or nil if the most recent attempt succeeded, raised
251
+ # before an outcome, or none was made
252
+ # @example
253
+ # vehicle.state # => "idling"
254
+ # vehicle.ignite # => false
255
+ # vehicle.class.state_machine.events[:ignite].refusal(vehicle) # => :no_transition
256
+ # vehicle.ignite_refusal # => :no_transition
257
+ def refusal(object)
258
+ return if object.frozen?
259
+
260
+ refusals = object.instance_variable_get(REFUSALS_VARIABLE)
261
+ refusals && refusals[[machine.name, name]]
262
+ end
263
+
264
+ # Records the reason the event was refused on the given object, or clears
265
+ # it when reason is nil. Nothing is recorded on a frozen object.
266
+ #
267
+ # @param object [Object] the object the event was fired on
268
+ # @param reason [Symbol, nil] the refusal reason (see {#refusal}), or nil
269
+ # to clear it
270
+ # @return [void]
271
+ def record_refusal(object, reason) # :nodoc:
272
+ return if object.frozen?
273
+
274
+ key = [machine.name, name]
275
+ REFUSALS_LOCK.synchronize do
276
+ refusals = object.instance_variable_get(REFUSALS_VARIABLE)
277
+ if reason
278
+ (refusals || object.instance_variable_set(REFUSALS_VARIABLE, {}))[key] = reason
279
+ else
280
+ refusals&.delete(key)
281
+ end
282
+ end
283
+ end
284
+
285
+ # Why no transition could be built for the object. The conditions are not
286
+ # evaluated again.
287
+ #
288
+ # @param object [Object] the object the event was fired on
289
+ # @return [:guard, :no_transition] :guard when a branch matches the current
290
+ # state but its conditions rejected it, otherwise :no_transition
291
+ def refusal_without_transition(object) # :nodoc:
292
+ query = { from: machine.states.match!(object).name, guard: false }
293
+ branches.any? { |branch| branch.matches?(object, query) } ? :guard : :no_transition
294
+ end
295
+
179
296
  # Marks the object as invalid and runs any failure callbacks associated with
180
297
  # this event. This should get called anytime this event fails to transition.
181
298
  def on_failure(object, *args)
@@ -238,6 +355,12 @@ module StateMachines
238
355
  machine.event(name).fire(object, *args, **kwargs)
239
356
  end
240
357
 
358
+ # Gets the reason the most recent attempt to fire the event was refused,
359
+ # or nil if it succeeded
360
+ machine.define_helper(:instance, "#{qualified_name}_refusal") do |machine, object|
361
+ machine.event(name).refusal(object)
362
+ end
363
+
241
364
  # Fires the event, raising an exception if it fails
242
365
  machine.define_helper(:instance, "#{qualified_name}!") do |machine, object, *args, **kwargs|
243
366
  object.send(qualified_name, *args, **kwargs) || raise(StateMachines::InvalidTransition.new(object, machine, name))
@@ -129,7 +129,12 @@ module StateMachines
129
129
  if event = self[event_name.to_sym, :name]
130
130
  event.transition_for(object) || begin
131
131
  # No valid transition: invalidate
132
- machine.invalidate(object, :event, :invalid_event, [[:state, machine.states.match!(object).human_name(object.class)]]) if invalidate
132
+ if invalidate
133
+ reason = event.refusal_without_transition(object)
134
+ event.record_refusal(object, reason)
135
+ TransitionCollection.report_nested_refusal(object, reason)
136
+ machine.invalidate(object, :event, :invalid_event, [[:state, machine.states.match!(object).human_name(object.class)]])
137
+ end
133
138
  false
134
139
  end
135
140
  else
@@ -141,6 +141,19 @@ module StateMachines
141
141
  fire_events(*(events + [run_action])) || raise(StateMachines::InvalidParallelTransition.new(self, events))
142
142
  end
143
143
 
144
+ # A copy (+dup+ / +clone+) has made no attempt to fire an event, so it
145
+ # starts with no refusal reasons instead of sharing the original's.
146
+ #
147
+ # A class defining its own +initialize_copy+ must call +super+.
148
+ #
149
+ # @param orig [Object] the object being copied
150
+ # @return [void]
151
+ # @see StateMachines::Event#refusal
152
+ def initialize_copy(orig)
153
+ super
154
+ StateMachines::Event.clear_refusals(self)
155
+ end
156
+
144
157
  protected
145
158
 
146
159
  def initialize_state_machines(options = {}, &) # :nodoc:
@@ -39,10 +39,15 @@ module StateMachines
39
39
  # object or nil if no transitions can be performed. Like <tt>can_park?</tt>
40
40
  # this will also *not* run validations or callbacks. It will only
41
41
  # determine if the state machine defines a valid transition for the event.
42
+ # * <tt>park_refusal</tt> - Gets the reason the most recent attempt to fire
43
+ # the "park" event was refused (<tt>:no_transition</tt>, <tt>:guard</tt>,
44
+ # <tt>:halted</tt>, <tt>:action_failed</tt> or <tt>:contended</tt>), or nil
45
+ # if it succeeded. See {StateMachines::Event#refusal}.
42
46
  #
43
47
  # With a namespace of "car", the above names map to the following methods:
44
48
  # * <tt>can_park_car?</tt>
45
49
  # * <tt>park_car_transition</tt>
50
+ # * <tt>park_car_refusal</tt>
46
51
  # * <tt>park_car</tt>
47
52
  # * <tt>park_car!</tt>
48
53
  #
@@ -55,6 +55,33 @@ module StateMachines
55
55
  target.method_defined?(method) || target.private_method_defined?(method)
56
56
  end
57
57
 
58
+ # Generates the warning message for a method conflict, including where
59
+ # the conflicting method was defined (when it has a Ruby source
60
+ # location) and which class the state machine is being defined on
61
+ def method_conflict_message(scope, method, defined_in)
62
+ scope_label = scope == :class ? 'Class' : 'Instance'
63
+ defined_in_name = defined_in.name && !defined_in.name.empty? ? defined_in.name : defined_in.to_s
64
+ location = conflicting_method_location(scope, method, defined_in)
65
+ location_label = location ? " at #{location[0]}:#{location[1]}" : ''
66
+
67
+ "#{scope_label} method \"#{method}\" is already defined in #{defined_in_name}#{location_label}, " \
68
+ 'use generic helper instead or set StateMachines::Machine.ignore_method_conflicts = true. ' \
69
+ "Defining #{name.inspect} state machine on #{owner_class}."
70
+ end
71
+
72
+ # Looks up the source location of the conflicting method. Returns nil
73
+ # for methods without a Ruby source (e.g. C-defined methods like
74
+ # Kernel#fail) and for the machine's own helper modules, where the
75
+ # location would just point inside this gem
76
+ def conflicting_method_location(scope, method, defined_in)
77
+ return if @helper_modules.value?(defined_in)
78
+
79
+ target = scope == :class && defined_in.is_a?(Class) ? defined_in.singleton_class : defined_in
80
+ target.instance_method(method).source_location
81
+ rescue NameError
82
+ nil
83
+ end
84
+
58
85
  # Pluralizes the given word using #pluralize (if available) or simply
59
86
  # adding an "s" to the end of the word
60
87
  def pluralize(word)
@@ -355,7 +355,7 @@ module StateMachines
355
355
  # module that gets included in every Object. As a result, state_machine will
356
356
  # generate the following warning:
357
357
  #
358
- # Instance method "open" is already defined in Object, use generic helper instead or set StateMachines::Machine.ignore_method_conflicts = true.
358
+ # Instance method "open" is already defined in Object, use generic helper instead or set StateMachines::Machine.ignore_method_conflicts = true. Defining :state state machine on Vehicle.
359
359
  #
360
360
  # Even though you may not be using Kernel's implementation of the "open"
361
361
  # instance method, state_machine isn't aware of this and, as a result, stays
@@ -517,8 +517,7 @@ module StateMachines
517
517
 
518
518
  if block_given?
519
519
  if !self.class.ignore_method_conflicts && (conflicting_ancestor = owner_class_ancestor_has_method?(scope, method))
520
- ancestor_name = conflicting_ancestor.name && !conflicting_ancestor.name.empty? ? conflicting_ancestor.name : conflicting_ancestor.to_s
521
- warn "#{scope == :class ? 'Class' : 'Instance'} method \"#{method}\" is already defined in #{ancestor_name}, use generic helper instead or set StateMachines::Machine.ignore_method_conflicts = true."
520
+ warn method_conflict_message(scope, method, conflicting_ancestor)
522
521
  else
523
522
  name = self.name
524
523
  helper_module.class_eval do
@@ -61,15 +61,16 @@ module StateMachines
61
61
  raise(InvalidEvent.new(object, event_name)) unless event
62
62
 
63
63
  # Get the transition that will be performed for the event
64
- unless (transition = event.transition_for(object))
65
- event.on_failure(object)
66
- end
64
+ transition = event.transition_for(object)
65
+ event.record_refusal(object, transition ? nil : event.refusal_without_transition(object))
66
+ event.on_failure(object) unless transition
67
67
  transition
68
- end.compact
68
+ end
69
69
 
70
70
  # Run the events in parallel only if valid transitions were found for
71
- # all of them
72
- if events.length == transitions.length
71
+ # all of them. Otherwise only the events without a transition carry a
72
+ # refusal reason.
73
+ if transitions.all?
73
74
  TransitionCollection.new(transitions, { use_transactions: resolve_use_transactions, actions: run_action }).perform
74
75
  else
75
76
  false
@@ -185,13 +185,13 @@ module StateMachines
185
185
  #
186
186
  # # Getting current event / transition availability
187
187
  # vehicle.state_events # => [:park]
188
- # vehicle.park # => true
188
+ # vehicle.park # => #<StateMachines::Transition attribute=:state event=:park ...>
189
189
  # vehicle.state_events # => [:ignite]
190
190
  # vehicle.state_events(:from => :idling) # => [:park]
191
191
  # vehicle.state_events(:to => :parked) # => []
192
192
  #
193
193
  # vehicle.state_transitions # => [#<StateMachines::Transition attribute=:state event=:ignite from="parked" from_name=:parked to="idling" to_name=:idling>]
194
- # vehicle.ignite # => true
194
+ # vehicle.ignite # => #<StateMachines::Transition attribute=:state event=:ignite ...>
195
195
  # vehicle.state_transitions # => [#<StateMachines::Transition attribute=:state event=:park from="idling" from_name=:idling to="parked" to_name=:parked>]
196
196
  #
197
197
  # vehicle.state_transitions(:on => :ignite) # => []
@@ -207,7 +207,7 @@ module StateMachines
207
207
  # # ]
208
208
  #
209
209
  # # Fire arbitrary events
210
- # vehicle.fire_state_event(:park) # => true
210
+ # vehicle.fire_state_event(:park) # => #<StateMachines::Transition attribute=:state event=:park ...>
211
211
  #
212
212
  # == Attribute initialization
213
213
  #
@@ -414,7 +414,7 @@ module StateMachines
414
414
  #
415
415
  # # Fire +ignite+ event directly
416
416
  # vehicle = Vehicle.create # => #<Vehicle id=1 state="parked" alarm_state="active">
417
- # vehicle.ignite # => true
417
+ # vehicle.ignite # => #<StateMachines::Transition attribute=:state event=:ignite ...>
418
418
  # vehicle.state # => "idling"
419
419
  # vehicle.alarm_state # => "active"
420
420
  #
@@ -289,8 +289,8 @@ module StateMachines
289
289
  def add_predicate
290
290
  predicate_method = "#{qualified_name}?"
291
291
 
292
- if machine.send(:owner_class_ancestor_has_method?, :instance, predicate_method)
293
- warn_about_method_conflict(predicate_method, machine.owner_class.ancestors.first)
292
+ if (conflicting_ancestor = machine.send(:owner_class_ancestor_has_method?, :instance, predicate_method))
293
+ warn_about_method_conflict(predicate_method, conflicting_ancestor)
294
294
  elsif machine.send(:owner_class_has_method?, :instance, predicate_method)
295
295
  warn_about_method_conflict(predicate_method, machine.owner_class)
296
296
  else
@@ -308,7 +308,7 @@ module StateMachines
308
308
  def warn_about_method_conflict(method, defined_in)
309
309
  return if StateMachines::Machine.ignore_method_conflicts
310
310
 
311
- warn "Instance method #{method.inspect} is already defined in #{defined_in.inspect}, use generic helper instead or set StateMachines::Machine.ignore_method_conflicts = true."
311
+ warn machine.send(:method_conflict_message, :instance, method, defined_in)
312
312
  end
313
313
  end
314
314
  end
@@ -23,18 +23,17 @@ module StateMachines
23
23
  end
24
24
  end
25
25
 
26
- module_function def draw_event(event, _graph, options: {}, io: $stdout)
27
- io = io || options[:io] || $stdout
26
+ module_function def draw_event(event, _graph, options = {}, io = $stdout)
27
+ io = options[:io] || io
28
28
  io.puts " Event: #{event.name}"
29
29
  end
30
30
 
31
- module_function def draw_branch(branch, _graph, _event, options: {}, io: $stdout)
32
- io = io || options[:io] || $stdout
31
+ module_function def draw_branch(branch, _graph, _event, _valid_states = [], io = $stdout)
33
32
  io.puts " Branch: #{branch.inspect}"
34
33
  end
35
34
 
36
- module_function def draw_state(state, _graph, options: {}, io: $stdout)
37
- io = io || options[:io] || $stdout
35
+ module_function def draw_state(state, _graph, options = {}, io = $stdout)
36
+ io = options[:io] || io
38
37
  io.puts " State: #{state.name}"
39
38
  end
40
39
 
@@ -51,6 +50,9 @@ module StateMachines
51
50
  out << "#{draw_requirement(requirement[:from])} => #{draw_requirement(requirement[:to])}"
52
51
  out << " IF #{branch.if_condition}" if branch.if_condition
53
52
  out << " UNLESS #{branch.unless_condition}" if branch.unless_condition
53
+ branch.state_guards.each do |kind, conditions|
54
+ out << " #{kind.upcase} #{conditions.map { |name, state| "#{name}: #{state}" }.join(', ')}"
55
+ end
54
56
  io.puts out
55
57
  end
56
58
  end