Skip to content

Commit

Permalink
Added support for private callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
dwbutler committed Aug 27, 2012
1 parent 8496110 commit 71937c0
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
7 changes: 6 additions & 1 deletion lib/workflow.rb
Original file line number Diff line number Diff line change
Expand Up @@ -266,9 +266,14 @@ def run_after_transition(from, to, event, *args)
def run_action(action, *args)
instance_exec(*args, &action) if action
end

def has_callback?(action)
self.respond_to?(action) or self.private_methods.include?(action)
end

def run_action_callback(action_name, *args)
self.send action_name.to_sym, *args if self.respond_to?(action_name.to_sym)
action = action_name.to_sym
self.send(action, *args) if has_callback?(action)
end

def run_on_entry(state, prior_state, triggering_event, *args)
Expand Down
15 changes: 13 additions & 2 deletions test/main_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,7 @@ def assert_state(title, expected_state, klass = Order)
test 'implicit transition callback' do
args = mock()
args.expects(:my_tran).once # this is validated at the end
args.expects(:another_tran).once
c = Class.new
c.class_eval do
include Workflow
Expand All @@ -277,10 +278,20 @@ def my_transition(args)
state :one do
event :my_transition, :transitions_to => :two
end
state :two
state :two do
event :another_transition, :transitions_to => :three
end
state :three
end

private
def another_transition(args)
args.another_tran
end
end
c.new.my_transition!(args)
a = c.new
a.my_transition!(args)
a.another_transition!(args)
end

test 'Single table inheritance (STI)' do
Expand Down

0 comments on commit 71937c0

Please sign in to comment.