Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for private callbacks #53

Merged
merged 4 commits into from
Sep 3, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.class.private_method_defined?(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
30 changes: 29 additions & 1 deletion test/main_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -279,8 +279,36 @@ def my_transition(args)
end
state :two
end

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

test 'private transition callback' do
args = mock()
args.expects(:log).once
c = Class.new
c.class_eval do
include Workflow
workflow do
state :new do
event :assign, :transitions_to => :assigned
end
state :assigned
end

private
def assign(args)
args.log('Assigned')
end
end
c.new.my_transition!(args)
a = c.new
a.assign!(args)
end

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