From ec1d57148bf491eef54b06624aa723addb219754 Mon Sep 17 00:00:00 2001 From: Nathaniel Bibler Date: Wed, 11 Mar 2009 13:44:36 -0400 Subject: [PATCH] Added Callbacks module to contain callback execution logic. Replaced callback methods in Event, State, and StateTransitions. --- lib/aasm.rb | 1 + lib/callbacks.rb | 22 ++++++++++++++++++++++ lib/event.rb | 11 +++-------- lib/state.rb | 10 +++------- lib/state_transition.rb | 9 +++------ spec/unit/aasm_spec.rb | 18 +++++++++++++++++- 6 files changed, 49 insertions(+), 22 deletions(-) create mode 100644 lib/callbacks.rb diff --git a/lib/aasm.rb b/lib/aasm.rb index fdee2ff..a7df698 100644 --- a/lib/aasm.rb +++ b/lib/aasm.rb @@ -1,3 +1,4 @@ +require File.join(File.dirname(__FILE__), 'callbacks') require File.join(File.dirname(__FILE__), 'event') require File.join(File.dirname(__FILE__), 'state') require File.join(File.dirname(__FILE__), 'state_machine') diff --git a/lib/callbacks.rb b/lib/callbacks.rb new file mode 100644 index 0000000..88924da --- /dev/null +++ b/lib/callbacks.rb @@ -0,0 +1,22 @@ +module AASM + module SupportingClasses + + module Callbacks + + def execute_callbacks(callbacks, object, *args) + callbacks = [ callbacks ] unless callbacks.respond_to?(:each) + + callbacks.each do |callback| + case callback + when Symbol, String + object.send(callback, *args) + when Proc + callback.call(object, *args) + end + end + end + + end + + end +end diff --git a/lib/event.rb b/lib/event.rb index 5589bf9..3966747 100644 --- a/lib/event.rb +++ b/lib/event.rb @@ -3,6 +3,8 @@ require File.join(File.dirname(__FILE__), 'state_transition') module AASM module SupportingClasses class Event + include Callbacks + attr_reader :name, :success def initialize(name, options = {}, &block) @@ -33,14 +35,7 @@ module AASM end def execute_success_callback(obj) - case success - when String, Symbol - obj.send(success) - when Array - success.each { |meth| obj.send(meth) } - when Proc - success.call(obj) - end + execute_callbacks(success, obj) end private diff --git a/lib/state.rb b/lib/state.rb index af74d74..4d2301e 100644 --- a/lib/state.rb +++ b/lib/state.rb @@ -1,6 +1,8 @@ module AASM module SupportingClasses class State + include Callbacks + attr_reader :name, :options def initialize(name, options={}) @@ -16,13 +18,7 @@ module AASM end def call_action(action, record) - action = @options[action] - case action - when Symbol, String - record.send(action) - when Proc - action.call(record) - end + execute_callbacks(@options[action], record) end def for_select diff --git a/lib/state_transition.rb b/lib/state_transition.rb index f82f5d4..9813d0c 100644 --- a/lib/state_transition.rb +++ b/lib/state_transition.rb @@ -1,6 +1,8 @@ module AASM module SupportingClasses class StateTransition + include Callbacks + attr_reader :from, :to, :opts def initialize(opts) @@ -20,12 +22,7 @@ module AASM end def execute(obj, *args) - case @on_transition - when Symbol, String - obj.send(@on_transition, *args) - when Proc - @on_transition.call(obj, *args) - end + execute_callbacks(@on_transition, obj, *args) end def ==(obj) diff --git a/spec/unit/aasm_spec.rb b/spec/unit/aasm_spec.rb index 30cfe87..861683a 100644 --- a/spec/unit/aasm_spec.rb +++ b/spec/unit/aasm_spec.rb @@ -33,9 +33,16 @@ class Bar aasm_state :read aasm_state :ended - aasm_event :foo do + aasm_event :foo, :success => [:first_success_callback, :second_success_callback] do transitions :to => :ended, :from => [:read] end + + def first_success_callback + end + + def second_success_callback + end + end class Baz < Bar @@ -154,6 +161,15 @@ describe AASM, '- event firing with persistence' do foo.close! end + + it 'should call multiple success callbacks when provided' do + bar = Bar.new + + bar.should_receive(:first_success_callback) + bar.should_receive(:second_success_callback) + + bar.foo! + end it 'should attempt to persist if aasm_write_state is defined' do foo = Foo.new -- 1.6.0.4