diff --git a/lib/captain_hook.rb b/lib/captain_hook.rb index dad1b0a..e1f3461 100644 --- a/lib/captain_hook.rb +++ b/lib/captain_hook.rb @@ -44,17 +44,13 @@ def hook( # Hooks logic part #### def get_hooks(kind) - # Only get hooks from the most specific class that defines them - return hooks[kind].values if hooks[kind].any? + ancestors.each_with_object({}) do |klass, hook_list| + next unless klass.respond_to?(:hooks) - # If no hooks defined in this class, look up the inheritance chain - ancestors[1..].each do |ancestor| - next unless ancestor.respond_to?(:hooks) - return ancestor.hooks[kind].values if ancestor.hooks[kind].any? - end - - # If no hooks found anywhere in the chain, return empty array - [] + klass.hooks[kind]&.each_value do |hook| + hook_list[hook.hook.class] ||= hook + end + end.values end def hooks @@ -70,7 +66,8 @@ def overriden?(method) def method_excluded_by_all_hooks?(method) get_hooks(:before).all? { |hook| hook.exclude.include?(method) } && - get_hooks(:after).all? { |hook| hook.exclude.include?(method) } + get_hooks(:after).all? { |hook| hook.exclude.include?(method) } && + get_hooks(:around).all? { |hook| hook.exclude.include?(method) } end def method_added(method_name) diff --git a/spec/captain_hook_spec.rb b/spec/captain_hook_spec.rb index 67242f8..363f77e 100644 --- a/spec/captain_hook_spec.rb +++ b/spec/captain_hook_spec.rb @@ -64,12 +64,9 @@ class ResourceWithHooks [args, {}] } - hook :around, - include: %i[prepare], - hook: ServeHook.new hook :around, - include: %i[serve foo], + include: %i[serve foo prepare], hook: ServeHook.new, skip_when: SkipWhenProc @@ -97,6 +94,8 @@ def policy_context end class ResourceChildWithHooks < ResourceWithHooks + hook :before, include: [:deliver], hook: ErroringHook.new + def foo(dto:); end def prepare(dto:) @@ -124,7 +123,6 @@ def prepare(dto:) expect_any_instance_of(PrepareHook).to receive(:call).once.and_call_original expect_any_instance_of(BeforeAllHook).to receive(:call).once.and_call_original expect_any_instance_of(ManyParametersHook).to receive(:call).once.and_call_original - expect_any_instance_of(ServeHook).to receive(:call).once.and_call_original expect(subject.prepare(dto: "bar")).to eq("preparing bar") end