Browse files

Added Hook#within { ... }

  • Loading branch information...
1 parent e952020 commit 999a0265e92a517d318fb3256b6029d62e13fe7a Kaoru Kobo committed Feb 10, 2011
Showing with 23 additions and 1 deletion.
  1. +11 −0 lib/rhook.rb
  2. +12 −1 spec/rhook_spec.rb
View
11 lib/rhook.rb
@@ -379,6 +379,17 @@ def unbind
service.unbind(name, self)
self
end
+
+ # After executing block, do {#unbind}.
+ # @return nil (Should return either self or block's result...?)
+ def within(&block)
+ begin
+ yield
+ ensure
+ unbind
+ end
+ nil
+ end
end #/Hook
#
View
13 spec/rhook_spec.rb
@@ -126,7 +126,7 @@ def self.hack_class_method
# ================================================================
- describe "Hook object (enable/disable/unbind)" do
+ describe "Hook object (enable/disable/unbind/within)" do
class Target
def enable_disable()
"disabled"
@@ -168,6 +168,17 @@ def unbind_test()
t = Target.new
t.unbind_test.should == "unbound"
end
+
+ example "within" do
+ t = Target.new
+
+ Target._rhook.hack(:unbind_test) { |inv|
+ "bound"
+ }.within {
+ t.unbind_test.should == "bound"
+ }
+ t.unbind_test.should == "unbound"
+ end
end

0 comments on commit 999a026

Please sign in to comment.