Skip to content

Commit c9aa08c

Browse files
committed
Issue #53 Add support for private callback methods again - but only in the immediate class
1 parent 66a0d31 commit c9aa08c

File tree

3 files changed

+32
-11
lines changed

3 files changed

+32
-11
lines changed

README.markdown

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -503,14 +503,14 @@ Changelog
503503

504504
### New in the version 0.9.0
505505

506-
* **Use protected callback methods.**
507-
If you wish to use non-public callback methods, you now have to switch
508-
from private to protected methods. See also issues
509-
[#53](https://github.com/geekq/workflow/pull/53)
506+
* **Support to priavate/protected callback methods.**
507+
See also issues [#53](https://github.com/geekq/workflow/pull/53)
510508
and [#58](https://github.com/geekq/workflow/pull/58). With the new
511509
implementation:
512510

513-
* callback methods can be hidden (non public)
511+
* callback methods can be hidden (non public): both private methods
512+
in the immediate class and protected methods somewhere in the class
513+
hierarchy are supported
514514
* no unintentional calls on `fail!` and other Kernel methods
515515
* inheritance hierarchy with workflow is supported
516516

lib/workflow.rb

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -284,9 +284,14 @@ def run_after_transition(from, to, event, *args)
284284
def run_action(action, *args)
285285
instance_exec(*args, &action) if action
286286
end
287-
287+
288288
def has_callback?(action)
289-
self.respond_to?(action) or self.class.protected_method_defined?(action)
289+
# 1. public callback method or
290+
# 2. protected method somewhere in the class hierarchy or
291+
# 3. private in the immediate class (parent classes ignored)
292+
self.respond_to?(action) or
293+
self.class.protected_method_defined?(action) or
294+
self.private_methods(false).map(&:to_sym).include?(action)
290295
end
291296

292297
def run_action_callback(action_name, *args)

test/main_test.rb

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -291,24 +291,40 @@ def another_transition(args)
291291

292292
test '#53 Support for non public transition callbacks' do
293293
args = mock()
294-
args.expects(:log).once
295-
c = Class.new
294+
args.expects(:log).with('in private callback').once
295+
args.expects(:log).with('in protected callback in the base class').once
296+
297+
b = Class.new # the base class with a protected callback
298+
b.class_eval do
299+
protected
300+
def assign_old(args)
301+
args.log('in protected callback in the base class')
302+
end
303+
end
304+
305+
c = Class.new(b) # inheriting class with an additional protected callback
296306
c.class_eval do
297307
include Workflow
298308
workflow do
299309
state :new do
300310
event :assign, :transitions_to => :assigned
311+
event :assign_old, :transitions_to => :assigned_old
301312
end
302313
state :assigned
314+
state :assigned_old
303315
end
304316

305-
protected
317+
private
306318
def assign(args)
307-
args.log('Assigned')
319+
args.log('in private callback')
308320
end
309321
end
322+
310323
a = c.new
311324
a.assign!(args)
325+
326+
a2 = c.new
327+
a2.assign_old!(args)
312328
end
313329

314330
test '#58 Limited private transition callback lookup' do

0 commit comments

Comments
 (0)