joelind-state_machine 0.8.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (78) hide show
  1. data/CHANGELOG.rdoc +297 -0
  2. data/LICENSE +20 -0
  3. data/README.rdoc +466 -0
  4. data/Rakefile +98 -0
  5. data/examples/AutoShop_state.png +0 -0
  6. data/examples/Car_state.png +0 -0
  7. data/examples/TrafficLight_state.png +0 -0
  8. data/examples/Vehicle_state.png +0 -0
  9. data/examples/auto_shop.rb +11 -0
  10. data/examples/car.rb +19 -0
  11. data/examples/merb-rest/controller.rb +51 -0
  12. data/examples/merb-rest/model.rb +28 -0
  13. data/examples/merb-rest/view_edit.html.erb +24 -0
  14. data/examples/merb-rest/view_index.html.erb +23 -0
  15. data/examples/merb-rest/view_new.html.erb +13 -0
  16. data/examples/merb-rest/view_show.html.erb +17 -0
  17. data/examples/rails-rest/controller.rb +43 -0
  18. data/examples/rails-rest/migration.rb +11 -0
  19. data/examples/rails-rest/model.rb +23 -0
  20. data/examples/rails-rest/view_edit.html.erb +25 -0
  21. data/examples/rails-rest/view_index.html.erb +23 -0
  22. data/examples/rails-rest/view_new.html.erb +14 -0
  23. data/examples/rails-rest/view_show.html.erb +17 -0
  24. data/examples/traffic_light.rb +7 -0
  25. data/examples/vehicle.rb +31 -0
  26. data/init.rb +1 -0
  27. data/lib/state_machine.rb +388 -0
  28. data/lib/state_machine/assertions.rb +36 -0
  29. data/lib/state_machine/callback.rb +189 -0
  30. data/lib/state_machine/condition_proxy.rb +94 -0
  31. data/lib/state_machine/eval_helpers.rb +67 -0
  32. data/lib/state_machine/event.rb +252 -0
  33. data/lib/state_machine/event_collection.rb +122 -0
  34. data/lib/state_machine/extensions.rb +149 -0
  35. data/lib/state_machine/guard.rb +230 -0
  36. data/lib/state_machine/integrations.rb +68 -0
  37. data/lib/state_machine/integrations/active_record.rb +492 -0
  38. data/lib/state_machine/integrations/active_record/locale.rb +11 -0
  39. data/lib/state_machine/integrations/active_record/observer.rb +41 -0
  40. data/lib/state_machine/integrations/data_mapper.rb +351 -0
  41. data/lib/state_machine/integrations/data_mapper/observer.rb +139 -0
  42. data/lib/state_machine/integrations/sequel.rb +322 -0
  43. data/lib/state_machine/machine.rb +1467 -0
  44. data/lib/state_machine/machine_collection.rb +155 -0
  45. data/lib/state_machine/matcher.rb +123 -0
  46. data/lib/state_machine/matcher_helpers.rb +54 -0
  47. data/lib/state_machine/node_collection.rb +152 -0
  48. data/lib/state_machine/state.rb +249 -0
  49. data/lib/state_machine/state_collection.rb +112 -0
  50. data/lib/state_machine/transition.rb +394 -0
  51. data/tasks/state_machine.rake +1 -0
  52. data/tasks/state_machine.rb +30 -0
  53. data/test/classes/switch.rb +11 -0
  54. data/test/functional/state_machine_test.rb +941 -0
  55. data/test/test_helper.rb +4 -0
  56. data/test/unit/assertions_test.rb +40 -0
  57. data/test/unit/callback_test.rb +455 -0
  58. data/test/unit/condition_proxy_test.rb +328 -0
  59. data/test/unit/eval_helpers_test.rb +120 -0
  60. data/test/unit/event_collection_test.rb +326 -0
  61. data/test/unit/event_test.rb +743 -0
  62. data/test/unit/guard_test.rb +908 -0
  63. data/test/unit/integrations/active_record_test.rb +1374 -0
  64. data/test/unit/integrations/data_mapper_test.rb +962 -0
  65. data/test/unit/integrations/sequel_test.rb +859 -0
  66. data/test/unit/integrations_test.rb +42 -0
  67. data/test/unit/invalid_event_test.rb +7 -0
  68. data/test/unit/invalid_transition_test.rb +7 -0
  69. data/test/unit/machine_collection_test.rb +938 -0
  70. data/test/unit/machine_test.rb +2004 -0
  71. data/test/unit/matcher_helpers_test.rb +37 -0
  72. data/test/unit/matcher_test.rb +155 -0
  73. data/test/unit/node_collection_test.rb +207 -0
  74. data/test/unit/state_collection_test.rb +280 -0
  75. data/test/unit/state_machine_test.rb +31 -0
  76. data/test/unit/state_test.rb +795 -0
  77. data/test/unit/transition_test.rb +1212 -0
  78. metadata +163 -0
@@ -0,0 +1,68 @@
1
+ # Load each available integration
2
+ Dir["#{File.dirname(__FILE__)}/integrations/*.rb"].sort.each do |path|
3
+ require "state_machine/integrations/#{File.basename(path)}"
4
+ end
5
+
6
+ module StateMachine
7
+ # Integrations allow state machines to take advantage of features within the
8
+ # context of a particular library. This is currently most useful with
9
+ # database libraries. For example, the various database integrations allow
10
+ # state machines to hook into features like:
11
+ # * Saving
12
+ # * Transactions
13
+ # * Observers
14
+ # * Scopes
15
+ # * Callbacks
16
+ # * Validation errors
17
+ #
18
+ # This type of integration allows the user to work with state machines in a
19
+ # fashion similar to other object models in their application.
20
+ #
21
+ # The integration interface is loosely defined by various unimplemented
22
+ # methods in the StateMachine::Machine class. See that class or the various
23
+ # built-in integrations for more information about how to define additional
24
+ # integrations.
25
+ module Integrations
26
+ # Attempts to find an integration that matches the given class. This will
27
+ # look through all of the built-in integrations under the StateMachine::Integrations
28
+ # namespace and find one that successfully matches the class.
29
+ #
30
+ # == Examples
31
+ #
32
+ # class Vehicle
33
+ # end
34
+ #
35
+ # class ARVehicle < ActiveRecord::Base
36
+ # end
37
+ #
38
+ # class DMVehicle
39
+ # include DataMapper::Resource
40
+ # end
41
+ #
42
+ # class SequelVehicle < Sequel::Model
43
+ # end
44
+ #
45
+ # StateMachine::Integrations.match(Vehicle) # => nil
46
+ # StateMachine::Integrations.match(ARVehicle) # => StateMachine::Integrations::ActiveRecord
47
+ # StateMachine::Integrations.match(DMVehicle) # => StateMachine::Integrations::DataMapper
48
+ # StateMachine::Integrations.match(SequelVehicle) # => StateMachine::Integrations::Sequel
49
+ def self.match(klass)
50
+ if integration = constants.find {|name| const_get(name).matches?(klass)}
51
+ find(integration)
52
+ end
53
+ end
54
+
55
+ # Finds an integration with the given name. If the integration cannot be
56
+ # found, then a NameError exception will be raised.
57
+ #
58
+ # == Examples
59
+ #
60
+ # StateMachine::Integrations.find(:active_record) # => StateMachine::Integrations::ActiveRecord
61
+ # StateMachine::Integrations.find(:data_mapper) # => StateMachine::Integrations::DataMapper
62
+ # StateMachine::Integrations.find(:sequel) # => StateMachine::Integrations::Sequel
63
+ # StateMachine::Integrations.find(:invalid) # => NameError: wrong constant name Invalid
64
+ def self.find(name)
65
+ const_get(name.to_s.gsub(/(?:^|_)(.)/) {$1.upcase})
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,492 @@
1
+ module StateMachine
2
+ module Integrations #:nodoc:
3
+ # Adds support for integrating state machines with ActiveRecord models.
4
+ #
5
+ # == Examples
6
+ #
7
+ # Below is an example of a simple state machine defined within an
8
+ # ActiveRecord model:
9
+ #
10
+ # class Vehicle < ActiveRecord::Base
11
+ # state_machine :initial => :parked do
12
+ # event :ignite do
13
+ # transition :parked => :idling
14
+ # end
15
+ # end
16
+ # end
17
+ #
18
+ # The examples in the sections below will use the above class as a
19
+ # reference.
20
+ #
21
+ # == Actions
22
+ #
23
+ # By default, the action that will be invoked when a state is transitioned
24
+ # is the +save+ action. This will cause the record to save the changes
25
+ # made to the state machine's attribute. *Note* that if any other changes
26
+ # were made to the record prior to transition, then those changes will
27
+ # be saved as well.
28
+ #
29
+ # For example,
30
+ #
31
+ # vehicle = Vehicle.create # => #<Vehicle id: 1, name: nil, state: "parked">
32
+ # vehicle.name = 'Ford Explorer'
33
+ # vehicle.ignite # => true
34
+ # vehicle.reload # => #<Vehicle id: 1, name: "Ford Explorer", state: "idling">
35
+ #
36
+ # == Events
37
+ #
38
+ # As described in StateMachine::InstanceMethods#state_machine, event
39
+ # attributes are created for every machine that allow transitions to be
40
+ # performed automatically when the object's action (in this case, :save)
41
+ # is called.
42
+ #
43
+ # In ActiveRecord, these automated events are run in the following order:
44
+ # * before validation - Run before callbacks and persist new states, then validate
45
+ # * before save - If validation was skipped, run before callbacks and persist new states, then save
46
+ # * after save - Run after callbacks
47
+ #
48
+ # For example,
49
+ #
50
+ # vehicle = Vehicle.create # => #<Vehicle id: 1, name: nil, state: "parked">
51
+ # vehicle.state_event # => nil
52
+ # vehicle.state_event = 'invalid'
53
+ # vehicle.valid? # => false
54
+ # vehicle.errors.full_messages # => ["State event is invalid"]
55
+ #
56
+ # vehicle.state_event = 'ignite'
57
+ # vehicle.valid? # => true
58
+ # vehicle.save # => true
59
+ # vehicle.state # => "idling"
60
+ # vehicle.state_event # => nil
61
+ #
62
+ # Note that this can also be done on a mass-assignment basis:
63
+ #
64
+ # vehicle = Vehicle.create(:state_event => 'ignite') # => #<Vehicle id: 1, name: nil, state: "idling">
65
+ # vehicle.state # => "idling"
66
+ #
67
+ # === Security implications
68
+ #
69
+ # Beware that public event attributes mean that events can be fired
70
+ # whenever mass-assignment is being used. If you want to prevent malicious
71
+ # users from tampering with events through URLs / forms, the attribute
72
+ # should be protected like so:
73
+ #
74
+ # class Vehicle < ActiveRecord::Base
75
+ # attr_protected :state_event
76
+ # # attr_accessible ... # Alternative technique
77
+ #
78
+ # state_machine do
79
+ # ...
80
+ # end
81
+ # end
82
+ #
83
+ # If you want to only have *some* events be able to fire via mass-assignment,
84
+ # you can build two state machines (one public and one protected) like so:
85
+ #
86
+ # class Vehicle < ActiveRecord::Base
87
+ # attr_protected :state_event # Prevent access to events in the first machine
88
+ #
89
+ # state_machine do
90
+ # # Define private events here
91
+ # end
92
+ #
93
+ # # Public machine targets the same state as the private machine
94
+ # state_machine :public_state, :attribute => :state do
95
+ # # Define public events here
96
+ # end
97
+ # end
98
+ #
99
+ # == Transactions
100
+ #
101
+ # In order to ensure that any changes made during transition callbacks
102
+ # are rolled back during a failed attempt, every transition is wrapped
103
+ # within a transaction.
104
+ #
105
+ # For example,
106
+ #
107
+ # class Message < ActiveRecord::Base
108
+ # end
109
+ #
110
+ # Vehicle.state_machine do
111
+ # before_transition do |vehicle, transition|
112
+ # Message.create(:content => transition.inspect)
113
+ # false
114
+ # end
115
+ # end
116
+ #
117
+ # vehicle = Vehicle.create # => #<Vehicle id: 1, name: nil, state: "parked">
118
+ # vehicle.ignite # => false
119
+ # Message.count # => 0
120
+ #
121
+ # *Note* that only before callbacks that halt the callback chain and
122
+ # failed attempts to save the record will result in the transaction being
123
+ # rolled back. If an after callback halts the chain, the previous result
124
+ # still applies and the transaction is *not* rolled back.
125
+ #
126
+ # To turn off transactions:
127
+ #
128
+ # class Vehicle < ActiveRecord::Base
129
+ # state_machine :initial => :parked, :use_transactions => false do
130
+ # ...
131
+ # end
132
+ # end
133
+ #
134
+ # == Validation errors
135
+ #
136
+ # If an event fails to successfully fire because there are no matching
137
+ # transitions for the current record, a validation error is added to the
138
+ # record's state attribute to help in determining why it failed and for
139
+ # reporting via the UI.
140
+ #
141
+ # For example,
142
+ #
143
+ # vehicle = Vehicle.create(:state => 'idling') # => #<Vehicle id: 1, name: nil, state: "idling">
144
+ # vehicle.ignite # => false
145
+ # vehicle.errors.full_messages # => ["State cannot transition via \"ignite\""]
146
+ #
147
+ # If an event fails to fire because of a validation error on the record and
148
+ # *not* because a matching transition was not available, no error messages
149
+ # will be added to the state attribute.
150
+ #
151
+ # == Scopes
152
+ #
153
+ # To assist in filtering models with specific states, a series of named
154
+ # scopes are defined on the model for finding records with or without a
155
+ # particular set of states.
156
+ #
157
+ # These named scopes are essentially the functional equivalent of the
158
+ # following definitions:
159
+ #
160
+ # class Vehicle < ActiveRecord::Base
161
+ # named_scope :with_states, lambda {|*states| {:conditions => {:state => states}}}
162
+ # # with_states also aliased to with_state
163
+ #
164
+ # named_scope :without_states, lambda {|*states| {:conditions => ['state NOT IN (?)', states]}}
165
+ # # without_states also aliased to without_state
166
+ # end
167
+ #
168
+ # *Note*, however, that the states are converted to their stored values
169
+ # before being passed into the query.
170
+ #
171
+ # Because of the way named scopes work in ActiveRecord, they can be
172
+ # chained like so:
173
+ #
174
+ # Vehicle.with_state(:parked).all(:order => 'id DESC')
175
+ #
176
+ # == Callbacks
177
+ #
178
+ # All before/after transition callbacks defined for ActiveRecord models
179
+ # behave in the same way that other ActiveRecord callbacks behave. The
180
+ # object involved in the transition is passed in as an argument.
181
+ #
182
+ # For example,
183
+ #
184
+ # class Vehicle < ActiveRecord::Base
185
+ # state_machine :initial => :parked do
186
+ # before_transition any => :idling do |vehicle|
187
+ # vehicle.put_on_seatbelt
188
+ # end
189
+ #
190
+ # before_transition do |vehicle, transition|
191
+ # # log message
192
+ # end
193
+ #
194
+ # event :ignite do
195
+ # transition :parked => :idling
196
+ # end
197
+ # end
198
+ #
199
+ # def put_on_seatbelt
200
+ # ...
201
+ # end
202
+ # end
203
+ #
204
+ # Note, also, that the transition can be accessed by simply defining
205
+ # additional arguments in the callback block.
206
+ #
207
+ # == Observers
208
+ #
209
+ # In addition to support for ActiveRecord-like hooks, there is additional
210
+ # support for ActiveRecord observers. Because of the way ActiveRecord
211
+ # observers are designed, there is less flexibility around the specific
212
+ # transitions that can be hooked in. However, a large number of hooks
213
+ # *are* supported. For example, if a transition for a record's +state+
214
+ # attribute changes the state from +parked+ to +idling+ via the +ignite+
215
+ # event, the following observer methods are supported:
216
+ # * before/after_ignite_from_parked_to_idling
217
+ # * before/after_ignite_from_parked
218
+ # * before/after_ignite_to_idling
219
+ # * before/after_ignite
220
+ # * before/after_transition_state_from_parked_to_idling
221
+ # * before/after_transition_state_from_parked
222
+ # * before/after_transition_state_to_idling
223
+ # * before/after_transition_state
224
+ # * before/after_transition
225
+ #
226
+ # The following class shows an example of some of these hooks:
227
+ #
228
+ # class VehicleObserver < ActiveRecord::Observer
229
+ # def before_save(vehicle)
230
+ # # log message
231
+ # end
232
+ #
233
+ # # Callback for :ignite event *before* the transition is performed
234
+ # def before_ignite(vehicle, transition)
235
+ # # log message
236
+ # end
237
+ #
238
+ # # Callback for :ignite event *after* the transition has been performed
239
+ # def after_ignite(vehicle, transition)
240
+ # # put on seatbelt
241
+ # end
242
+ #
243
+ # # Generic transition callback *before* the transition is performed
244
+ # def after_transition(vehicle, transition)
245
+ # Audit.log(vehicle, transition)
246
+ # end
247
+ # end
248
+ #
249
+ # More flexible transition callbacks can be defined directly within the
250
+ # model as described in StateMachine::Machine#before_transition
251
+ # and StateMachine::Machine#after_transition.
252
+ #
253
+ # To define a single observer for multiple state machines:
254
+ #
255
+ # class StateMachineObserver < ActiveRecord::Observer
256
+ # observe Vehicle, Switch, Project
257
+ #
258
+ # def after_transition(record, transition)
259
+ # Audit.log(record, transition)
260
+ # end
261
+ # end
262
+ module ActiveRecord
263
+ # The default options to use for state machines using this integration
264
+ class << self; attr_reader :defaults; end
265
+ @defaults = {:action => :save}
266
+
267
+ # Should this integration be used for state machines in the given class?
268
+ # Classes that inherit from ActiveRecord::Base will automatically use
269
+ # the ActiveRecord integration.
270
+ def self.matches?(klass)
271
+ defined?(::ActiveRecord::Base) && klass <= ::ActiveRecord::Base
272
+ end
273
+
274
+ # Loads additional files specific to ActiveRecord
275
+ def self.extended(base) #:nodoc:
276
+ require 'state_machine/integrations/active_record/observer'
277
+
278
+ if Object.const_defined?(:I18n)
279
+ locale = "#{File.dirname(__FILE__)}/active_record/locale.rb"
280
+ I18n.load_path << locale unless I18n.load_path.include?(locale)
281
+ end
282
+ end
283
+
284
+ # Forces the change in state to be recognized regardless of whether the
285
+ # state value actually changed
286
+ def write(object, attribute, value)
287
+ result = super
288
+ if attribute == :state && object.respond_to?("#{attribute}_will_change!") && !object.send("#{attribute}_changed?")
289
+ object.send("#{attribute}_will_change!")
290
+ end
291
+ result
292
+ end
293
+
294
+ # Adds a validation error to the given object
295
+ def invalidate(object, attribute, message, values = [])
296
+ attribute = self.attribute(attribute)
297
+
298
+ if Object.const_defined?(:I18n)
299
+ options = values.inject({}) {|options, (key, value)| options[key] = value; options}
300
+ object.errors.add(attribute, message, options.merge(
301
+ :default => @messages[message]
302
+ ))
303
+ else
304
+ object.errors.add(attribute, generate_message(message, values))
305
+ end
306
+ end
307
+
308
+ # Resets any errors previously added when invalidating the given object
309
+ def reset(object)
310
+ object.errors.clear
311
+ end
312
+
313
+ protected
314
+ # Adds the default callbacks for notifying ActiveRecord observers
315
+ # before/after a transition has been performed.
316
+ def after_initialize
317
+ callbacks[:before] << Callback.new {|object, transition| notify(:before, object, transition)}
318
+ callbacks[:after] << Callback.new {|object, transition| notify(:after, object, transition)}
319
+ end
320
+
321
+ # Defines an initialization hook into the owner class for setting the
322
+ # initial state of the machine *before* any attributes are set on the
323
+ # object
324
+ def define_state_initializer
325
+ @instance_helper_module.class_eval <<-end_eval, __FILE__, __LINE__
326
+ # Ensure that the attributes setter gets used to force initialization
327
+ # of the state machines
328
+ def initialize(attributes = nil, *args)
329
+ attributes ||= {}
330
+ super
331
+ end
332
+
333
+ # Hooks in to attribute initialization to set the states *prior*
334
+ # to the attributes being set
335
+ def attributes=(new_attributes, *args)
336
+ if new_record? && !@initialized_state_machines
337
+ @initialized_state_machines = true
338
+
339
+ if new_attributes
340
+ attributes = new_attributes.dup
341
+ attributes.stringify_keys!
342
+ ignore = remove_attributes_protected_from_mass_assignment(attributes).keys
343
+ else
344
+ ignore = []
345
+ end
346
+
347
+ initialize_state_machines(:dynamic => false, :ignore => ignore)
348
+ super
349
+ initialize_state_machines(:dynamic => true, :ignore => ignore)
350
+ else
351
+ super
352
+ end
353
+ end
354
+ end_eval
355
+ end
356
+
357
+ # Skips defining reader/writer methods since this is done automatically
358
+ def define_state_accessor
359
+ name = self.name
360
+
361
+ owner_class.validates_each(attribute) do |record, attr, value|
362
+ machine = record.class.state_machine(name)
363
+ machine.invalidate(record, :state, :invalid) unless machine.states.match(record)
364
+ end
365
+ end
366
+
367
+ # Adds support for defining the attribute predicate, while providing
368
+ # compatibility with the default predicate which determines whether
369
+ # *anything* is set for the attribute's value
370
+ def define_state_predicate
371
+ name = self.name
372
+
373
+ # Still use class_eval here instance of define_instance_method since
374
+ # we need to be able to call +super+
375
+ @instance_helper_module.class_eval do
376
+ define_method("#{name}?") do |*args|
377
+ args.empty? ? super(*args) : self.class.state_machine(name).states.matches?(self, *args)
378
+ end
379
+ end
380
+ end
381
+
382
+ # Adds hooks into validation for automatically firing events
383
+ def define_action_helpers
384
+ if action == :save
385
+ if super(:create_or_update)
386
+ @instance_helper_module.class_eval do
387
+ define_method(:valid?) do |*args|
388
+ self.class.state_machines.fire_event_attributes(self, :save, false) { super(*args) }
389
+ end
390
+ end
391
+ end
392
+ else
393
+ super
394
+ end
395
+ end
396
+
397
+ # Creates a scope for finding records *with* a particular state or
398
+ # states for the attribute
399
+ def create_with_scope(name)
400
+ attribute = self.attribute
401
+ define_scope(name, lambda {|values| {:conditions => {attribute => values}}})
402
+ end
403
+
404
+ # Creates a scope for finding records *without* a particular state or
405
+ # states for the attribute
406
+ def create_without_scope(name)
407
+ attribute = self.attribute
408
+ define_scope(name, lambda {|values| {:conditions => ["#{attribute} NOT IN (?)", values]}})
409
+ end
410
+
411
+ # Runs a new database transaction, rolling back any changes by raising
412
+ # an ActiveRecord::Rollback exception if the yielded block fails
413
+ # (i.e. returns false).
414
+ def transaction(object)
415
+ object.class.transaction {raise ::ActiveRecord::Rollback unless yield}
416
+ end
417
+
418
+ # Creates a new callback in the callback chain, always inserting it
419
+ # before the default Observer callbacks that were created after
420
+ # initialization.
421
+ def add_callback(type, options, &block)
422
+ options[:terminator] = @terminator ||= lambda {|result| result == false}
423
+ @callbacks[type].insert(-2, callback = Callback.new(options, &block))
424
+ add_states(callback.known_states)
425
+
426
+ callback
427
+ end
428
+
429
+ private
430
+ # Defines a new named scope with the given name. Since ActiveRecord
431
+ # does not allow direct access to the model being used within the
432
+ # evaluation of a dynamic named scope, the scope must be generated
433
+ # manually. It's necessary to have access to the model so that the
434
+ # state names can be translated to their associated values and so that
435
+ # inheritance is respected properly.
436
+ def define_scope(name, scope)
437
+ name = name.to_sym
438
+ machine_name = self.name
439
+
440
+ # Create the scope and then override it with state translation
441
+ owner_class.named_scope(name)
442
+ owner_class.scopes[name] = lambda do |klass, *states|
443
+ machine_states = klass.state_machine(machine_name).states
444
+ values = states.flatten.map {|state| machine_states.fetch(state).value}
445
+
446
+ ::ActiveRecord::NamedScope::Scope.new(klass, scope.call(values))
447
+ end
448
+
449
+ # Prevent the Machine class from wrapping the scope
450
+ false
451
+ end
452
+
453
+ # Notifies observers on the given object that a callback occurred
454
+ # involving the given transition. This will attempt to call the
455
+ # following methods on observers:
456
+ # * #{type}_#{qualified_event}_from_#{from}_to_#{to}
457
+ # * #{type}_#{qualified_event}_from_#{from}
458
+ # * #{type}_#{qualified_event}_to_#{to}
459
+ # * #{type}_#{qualified_event}
460
+ # * #{type}_transition_#{machine_name}_from_#{from}_to_#{to}
461
+ # * #{type}_transition_#{machine_name}_from_#{from}
462
+ # * #{type}_transition_#{machine_name}_to_#{to}
463
+ # * #{type}_transition_#{machine_name}
464
+ # * #{type}_transition
465
+ #
466
+ # This will always return true regardless of the results of the
467
+ # callbacks.
468
+ def notify(type, object, transition)
469
+ name = self.name
470
+ event = transition.qualified_event
471
+ from = transition.from_name
472
+ to = transition.to_name
473
+
474
+ # Machine-specific updates
475
+ ["#{type}_#{event}", "#{type}_transition_#{name}"].each do |event_segment|
476
+ ["_from_#{from}", nil].each do |from_segment|
477
+ ["_to_#{to}", nil].each do |to_segment|
478
+ object.class.changed
479
+ object.class.notify_observers([event_segment, from_segment, to_segment].join, object, transition)
480
+ end
481
+ end
482
+ end
483
+
484
+ # Generic updates
485
+ object.class.changed
486
+ object.class.notify_observers("#{type}_transition", object, transition)
487
+
488
+ true
489
+ end
490
+ end
491
+ end
492
+ end