Browse files

[Feature] Adding a hook to group with bind()'s :group option.

  • Loading branch information...
1 parent 7546386 commit df50e3e33a2b3568f629b2b7deb0e6f32fc96d02 Kaoru Kobo committed Jan 1, 2011
Showing with 30 additions and 3 deletions.
  1. +20 −3 lib/rhook.rb
  2. +10 −0 spec/rhook_spec.rb
View
23 lib/rhook.rb
@@ -44,7 +44,8 @@ def bound_object
# Add hook to the {#bound_object}. If it is a kind of Class, the hook is affected to all instance & subclasses of the class.
#
# @param [Symbol] name hook-point's name (commonly equals to method name)
- # @option opt [true] :disable Create hook but make disabled. (by default, automatically enabled.)
+ # @option opt [true] :disable Creates hook but make disabled. (by default, automatically enabled.)
+ # @option opt [RHook::HookGroup] :group Adds itself into the specified hook-group.
# @yield [inv] The hook block.
# @yieldparam [Invocation] inv
# @yieldreturn The result value. (Returned value of called method.)
@@ -55,7 +56,11 @@ def bind(name, opt = {}, &block)
(@hooks_map[name.to_sym] ||= []).unshift( hook )
RHook.registry.class_cached_flag_map.delete(name)
opt[:disable] or hook.enable()
- HookGroup.add_to_current_groups(hook)
+ if opt[:group]
+ opt[:group].add(hook)
+ else
+ HookGroup.add_to_current_groups(hook)
+ end
hook
end
@@ -407,6 +412,12 @@ def disable
self
end
+ # Tests the given hook is registered in this group.
+ # @return [Boolean]
+ def include?(hook)
+ @hooks.include?(hook)
+ end
+
# @private
def self.add_to_current_groups(hook)
(Thread.current["rhook_group"] || []).each do |group|
@@ -424,8 +435,14 @@ def self.add_to_current_groups(hook)
# }
# XXX_feature_for_library.disable
#
+ # @example
+ # grp = RHook.group
+ # Target._rhook.bind(..., :group => grp) { ... }
+ #
# @return [HookGroup]
def self.group(&block)
- HookGroup.new.wrap(&block)
+ group = HookGroup.new
+ block and group.wrap(&block)
+ group
end
end
View
10 spec/rhook_spec.rb
@@ -213,6 +213,16 @@ def group_2
t.group_1.should == "hack1"
t.group_2.should == "hack2"
end
+
+ example "Adding a hook to group with bind()'s :group option." do
+ # Instantiate a group with RHook.group() without block.
+ group = RHook.group
+ t = Target.new
+ hook = t._rhook.hack(:group_1, :group => group) do |inv|
+ inv.call
+ end
+ group.include?(hook).should be_true
+ end
end
# ================================================================

0 comments on commit df50e3e

Please sign in to comment.