Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

[bug] super class's hook was not affected.

  • Loading branch information...
commit 0d6a5b920001d459f5ff46875e104f88a474b76c 1 parent a1c15d2
Kaoru Kobo authored
Showing with 69 additions and 41 deletions.
  1. +6 −6 lib/rhook.rb
  2. +63 −35 spec/rhook_spec.rb
View
12 lib/rhook.rb
@@ -135,26 +135,26 @@ def concat_class_hooks(dest, name)
end
hooks = []
-
+
# collect hooks including ancestor classes
begin
concat_hooks_internal(hooks, name)
# skips class without RHookService
klass = @obj
- while klass
+ while true
+ klass = klass.superclass
+ klass or break
if klass._has_rhook?
- klass._rhook.conact_class_hooks(hooks, name)
+ klass._rhook.concat_class_hooks(hooks, name)
break
end
- klass = klass.superclass
end
end
# Store to cache.
@class_cached_hooks_map[name] = hooks
RHook.registry.class_cached_flag_map[name] = true
-
dest.concat(hooks)
end
@@ -218,7 +218,7 @@ def _rhook
end
def _has_rhook?
- @rhook ? true : false
+ @_rhook ? true : false
end
end
View
98 spec/rhook_spec.rb
@@ -126,40 +126,7 @@ def self.hack_class_method
# ================================================================
- describe "multiple (chained) hooks" do
- class Target
- def chained
- ["original"]
- end
- end
-
- example "multiple hooks" do
- t = Target.new
- t._rhook.hack(:chained) do |inv|
- inv.call + ["1st_hook"]
- end
- t._rhook.hack(:chained) do |inv|
- inv.call + ["2nd_hook"]
- end
-
- t.chained.should == ["original", "1st_hook", "2nd_hook"]
- end
-
- example "Hook to class is executed later, than one to object" do
- t = Target.new
- t._rhook.hack(:chained) do |inv|
- inv.call + ["object_hook"]
- end
- Target._rhook.hack(:chained) do |inv|
- inv.call + ["class_hook"]
- end
-
- t.chained.should == ["original", "class_hook", "object_hook"]
- end
- end
-
- # ================================================================
- describe "enable/disable" do
+ describe "Hook object (enable/disable)" do
class Target
def enable_disable()
"disabled"
@@ -187,7 +154,7 @@ def enable_disable()
# ================================================================
- describe "Group" do
+ describe "Hook group" do
class Target
def group_1
"g1"
@@ -250,3 +217,64 @@ def group_2
# ================================================================
end
+
+# ================================================================
+describe "rhook (behavior)" do
+ describe "Super class's hook" do
+ class Target
+ end
+
+ class ChildTarget < Target
+ def superclass_test
+ _rhook.does(:superclass_test) { "foo"; }
+ end
+ end
+
+ example "Super class's hook affects children class." do
+ child_t = ChildTarget.new
+ child_t.superclass_test.should == "foo"
+ # Add hook to superclass:
+ Target._rhook.bind(:superclass_test) do |inv|
+ "bar"
+ end
+ # then it affects to children class:
+ child_t.superclass_test.should == "bar"
+ end
+ end
+
+ # ================================================================
+ describe "multiple (chained) hooks" do
+ class Target
+ def chained
+ ["original"]
+ end
+ end
+
+ example "multiple hooks" do
+ t = Target.new
+ t._rhook.hack(:chained) do |inv|
+ inv.call + ["1st_hook"]
+ end
+ t._rhook.hack(:chained) do |inv|
+ inv.call + ["2nd_hook"]
+ end
+
+ t.chained.should == ["original", "1st_hook", "2nd_hook"]
+ end
+
+ example "Hook to class is executed later, than one to object" do
+ t = Target.new
+ t._rhook.hack(:chained) do |inv|
+ inv.call + ["object_hook"]
+ end
+ Target._rhook.hack(:chained) do |inv|
+ inv.call + ["class_hook"]
+ end
+
+ t.chained.should == ["original", "class_hook", "object_hook"]
+ end
+ end
+
+
+
+end

0 comments on commit 0d6a5b9

Please sign in to comment.
Something went wrong with that request. Please try again.