state_machines 0.202.0 → 0.301.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
@@ -22,6 +22,10 @@ module StateMachines
22
22
  end
23
23
 
24
24
  module InstanceMethods
25
+ # Per-object concurrency state, created on demand: the lock and the
26
+ # transition claims. Never copied nor marshalled.
27
+ TRANSIENT_VARIABLES = %i[@_state_machine_mutex @_state_machine_transition_claims].freeze
28
+
25
29
  # Runs one or more events in parallel. All events will run through the
26
30
  # following steps:
27
31
  # * Before callbacks
@@ -141,6 +145,24 @@ module StateMachines
141
145
  fire_events(*(events + [run_action])) || raise(StateMachines::InvalidParallelTransition.new(self, events))
142
146
  end
143
147
 
148
+ # A copy (+dup+ / +clone+) has made no attempt to fire an event, so it
149
+ # starts with no refusal reasons instead of sharing the original's. It
150
+ # also gets its own lock and transition claims (created on demand)
151
+ # instead of sharing the original's.
152
+ #
153
+ # A class defining its own +initialize_copy+ must call +super+.
154
+ #
155
+ # @param orig [Object] the object being copied
156
+ # @return [void]
157
+ # @see StateMachines::Event#refusal
158
+ def initialize_copy(orig)
159
+ super
160
+ StateMachines::Event.clear_refusals(self)
161
+ TRANSIENT_VARIABLES.each do |ivar|
162
+ remove_instance_variable(ivar) if instance_variable_defined?(ivar)
163
+ end
164
+ end
165
+
144
166
  protected
145
167
 
146
168
  def initialize_state_machines(options = {}, &) # :nodoc:
@@ -3,6 +3,10 @@
3
3
  module StateMachines
4
4
  class Machine
5
5
  module ClassMethods
6
+ # Options a reopened machine accepts; the others only apply when it is
7
+ # created.
8
+ REOPEN_OPTIONS = %i[initial async].freeze
9
+
6
10
  # Attempts to find or create a state machine for the given class. For
7
11
  # example,
8
12
  #
@@ -14,6 +18,9 @@ module StateMachines
14
18
  # If a machine of the given name already exists in one of the class's
15
19
  # superclasses, then a copy of that machine will be created and stored
16
20
  # in the new owner class (the original will remain unchanged).
21
+ #
22
+ # Reopening an existing machine applies +:initial+ and +:async+;
23
+ # any other option raises ArgumentError instead of being ignored.
17
24
  def find_or_create(owner_class, *args, &)
18
25
  options = args.last.is_a?(Hash) ? args.pop : {}
19
26
  name = args.first || :state
@@ -24,15 +31,20 @@ module StateMachines
24
31
  owner_class.state_machines.values.first))) || nil
25
32
 
26
33
  if machine
34
+ unsupported = options.keys - REOPEN_OPTIONS
35
+ unless unsupported.empty?
36
+ raise ArgumentError, "Cannot change #{unsupported.map(&:inspect).join(', ')} of existing state machine " \
37
+ "#{name.inspect} on #{owner_class}; reopening it accepts only :initial and :async"
38
+ end
39
+
27
40
  # Only create a new copy if changes are being made to the machine in
28
41
  # a subclass
29
42
  if machine.owner_class != owner_class && (options.any? || block_given?)
30
43
  machine = machine.clone
31
- machine.initial_state = options[:initial] if options.include?(:initial)
32
44
  machine.owner_class = owner_class
33
- # Configure async mode if requested in options
34
- machine.configure_async_mode!(options[:async]) if options.include?(:async)
35
45
  end
46
+ machine.initial_state = options[:initial] if options.include?(:initial)
47
+ machine.configure_async_mode!(options[:async]) if options.include?(:async)
36
48
 
37
49
  # Evaluate DSL
38
50
  machine.instance_eval(&) if block_given?
@@ -40,6 +40,7 @@ module StateMachines
40
40
  @initialize_state = options[:initialize]
41
41
  @action_hook_defined = false
42
42
  @async_requested = options[:async]
43
+ @integration_options = @integration ? options.slice(*@integration.integration_options).freeze : {}.freeze
43
44
 
44
45
  self.owner_class = owner_class
45
46
 
@@ -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
  #
@@ -476,6 +476,14 @@ module StateMachines
476
476
  # Whether the machine will use transactions when firing events
477
477
  attr_reader :use_transactions
478
478
 
479
+ # The options declared by the machine's integration (see
480
+ # StateMachines::Integrations::Base::ClassMethods#integration_options)
481
+ # that were passed when creating the machine. Available from
482
+ # +owner_class=+ onward.
483
+ #
484
+ # @return [Hash{Symbol => Object}] frozen option name => value
485
+ attr_reader :integration_options
486
+
479
487
  # Defines a new helper method in an instance or class scope with the given
480
488
  # name. If the method is already defined in the scope, then this will not
481
489
  # override it.
@@ -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
  #
@@ -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