Allow to override parents defined hooks#12
Conversation
@josemoyab can you try if the configuration works with this changes made by @atd ? |
c6ecc23 to
794891a
Compare
I tested the configuration with the changes made by @atd, and everything worked as expected. |
lib/captain_hook.rb
Outdated
| 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] = [] } |
There was a problem hiding this comment.
what is this? why it needs to be initialized in this way?
There was a problem hiding this comment.
This is a hash where we’ll store and organize the hooks defined by a class and its ancestors. The key is the hook class name, and the value is an array containing all the occurrences of that hook.
It can be initialized that way to directly store hook occurrences using the “<<“ operator on the loop
lib/captain_hook.rb
Outdated
| # 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) |
There was a problem hiding this comment.
why &:first? how this hook_list.values looks like?
There was a problem hiding this comment.
On each iteration of the loop, we stored all the occurrences of hooks from a class and its ancestors in the hook_list. Since the array is sorted, selecting the first element means that we retrieve the hook definition closer to the child class.
Considering the following class definitions::
class Child
hook :before, hook: Hook1.new, ...
end
class Parent < Child
hook :before, hook: Hook1.new, ...
hook :before, hook: Hook2.new, ...
end
Calling get_hooks(:before) will set the value of hook_list to this:
{
"Hook1": [Hook1 (from Child), Hook1 (from Parent),
"Hook2": [Hook2 (from Parent)]
}
and hook_list.values will looks like:
[
[Hook1 (from Child), Hook1 (from Parent)],
[Hook2 (from Parent)]
]
So mapping the values using first will produce an array like:
[Hook1 (from Child), Hook2 (from Parent)]
gtrias
left a comment
There was a problem hiding this comment.
I don't fully understand the fix. What essentially changed?
You mean that merging @atd changes this PR is not needed anymore? |
I added a comment with an example, but TL;DR I changed |
|
@josemoyab please be aware that my branch is failing in Factorial |
No worries, I've tested this branch in factorial and everything looks good @gtrias I've simplified the |
| 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| |
Overriding a hook in a child class currently overrides all the hooks defined by its parents. This PR aims to allow overriding only the desired hooks based on the hook class. However, it doesn’t permit adding the same hook twice on the same kind.
This PR also consider
aroundhooks while checking if all hooks excluded a method