Skip to content

Commit

Permalink
Allow to override parents defined hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
josemoyab committed Nov 26, 2024
1 parent 047cd32 commit fa761c8
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions lib/captain_hook.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,19 @@ 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?
hook_list = Hash.new { |h, k| h[k] = [] }

# 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?
# Collect hooks from the current class and ancestor classes
[self, *ancestors[1..]].each do |klass|
next unless klass.respond_to?(:hooks)

klass.hooks[kind].each_value do |hook|
hook_list[hook.hook.class] << hook
end
end

# If no hooks found anywhere in the chain, return empty array
[]
# Return an array containing the first element of each key in hook_list
hook_list.values.map(&:first)
end

def hooks
Expand Down

0 comments on commit fa761c8

Please sign in to comment.