Permalink
Browse files

No longer fails when we attach hooks to methods ending in punctuation.

  • Loading branch information...
1 parent d901026 commit 79877a35f1e07203b4c1de4d73288ad7b980ba10 @david david committed Apr 11, 2008
Showing with 71 additions and 4 deletions.
  1. +9 −4 lib/data_mapper/hook.rb
  2. +62 −0 spec/unit/hook_spec.rb
View
@@ -11,7 +11,7 @@ def before(target_method, method_sym = nil, &block)
def install_hook(type, name, method_sym = nil, &block)
(hooks[name][type] ||= []) << if block
- new_meth_name = "__hooks_#{type}_#{name}_#{hooks[name][type].length}".to_sym
+ new_meth_name = "__hooks_#{type}_#{quote_method(name)}_#{hooks[name][type].length}".to_sym
define_method new_meth_name, block
new_meth_name
else
@@ -38,7 +38,7 @@ def inline_call(name, args)
" super(#{args})\n"
else
<<-EOF
- (@__hooks_#{name}_old_method || @__hooks_#{name}_old_method =
+ (@__hooks_#{quote_method(name)}_old_method || @__hooks_#{quote_method(name)}_old_method =
self.class.hooks[:#{name}][:old_method].bind(self)).call(#{args})
EOF
end
@@ -53,8 +53,9 @@ def inline_hooks(name, type, args)
when Symbol
method_def << " #{e}(#{args})\n"
else
- method_def << "(@__hooks_#{name}_#{i} || "
- method_def << " @__hooks_#{name}_#{i} = self.class.hooks[:#{name}][:#{type}][#{i}])"
+ # TODO: Test this. Testing order should be before, after and after, before
+ method_def << "(@__hooks_#{quote_method(name)}_#{type}_#{i} || "
+ method_def << " @__hooks_#{quote_method(name)}_#{type}_#{i} = self.class.hooks[:#{name}][:#{type}][#{i}])"
method_def << ".call #{args}\n"
end
end
@@ -78,6 +79,10 @@ def hooks
@hooks ||= Hash.new { |h, k| h[k] = {} }
end
+ def quote_method(name)
+ name.to_s.gsub(/\?$/, '_q_').gsub(/!$/, '_b_')
+ end
+
def after(target_method, method_sym = nil, &block)
install_hook :after, target_method, method_sym, &block
end
View
@@ -209,4 +209,66 @@ def a_hook
@class.new.a_method
end
+
+ it "should allow advising methods ending in ? or !" do
+ tester = mock("tester")
+ tester.should_receive(:before).ordered.once
+ tester.should_receive(:method!).ordered.once
+ tester.should_receive(:method?).ordered.once
+ tester.should_receive(:after).ordered.once
+
+ @class.class_eval do
+ define_method :a_method! do
+ tester.method!
+ end
+
+ define_method :a_method? do
+ tester.method?
+ end
+
+ before :a_method! do
+ tester.before
+ end
+
+ after :a_method? do
+ tester.after
+ end
+ end
+
+ @class.new.a_method!
+ @class.new.a_method?
+ end
+
+ it "should allow advising methods ending in ? or ! when passing methods as advices" do
+ tester = mock("tester")
+ tester.should_receive(:before).ordered.once
+ tester.should_receive(:method!).ordered.once
+ tester.should_receive(:method?).ordered.once
+ tester.should_receive(:after).ordered.once
+
+ @class.class_eval do
+ define_method :a_method! do
+ tester.method!
+ end
+
+ define_method :a_method? do
+ tester.method?
+ end
+
+ define_method :before_a_method_bang do
+ tester.before
+ end
+
+ before :a_method!, :before_a_method_bang
+
+ define_method :after_a_method_question do
+ tester.after
+ end
+
+ after :a_method?, :after_a_method_question
+ end
+
+ @class.new.a_method!
+ @class.new.a_method?
+ end
end

0 comments on commit 79877a3

Please sign in to comment.