Skip to content

Commit

Permalink
fix previous broken tests and back to bug reproduced
Browse files Browse the repository at this point in the history
  • Loading branch information
gtrias committed Oct 21, 2024
1 parent f732ecb commit 14652d8
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 17 deletions.
10 changes: 1 addition & 9 deletions lib/captain_hook.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ def self.inherited(subclass)
# need to be executed in a stack-like way.
def run_around_hooks(method, *args, **kwargs, &block)
self.class.get_hooks(:around).to_a.reverse.inject(block) do |chain, hook_configuration|
# binding.pry if hook_configuration.hook.is_a?(ManyParametersHook)

next chain if hook_configuration.skip?(method, args, kwargs)

instance = self
Expand All @@ -46,13 +44,7 @@ def run_around_hooks(method, *args, **kwargs, &block)
# Runs non-around hooks for a given method.
def run_hooks(method, hooks, *args, **kwargs)
hooks.each do |hook_configuration|
next if hook_configuration.skip?(method, args, kwargs)

body = run_hook(method, hook_configuration, -> {}, self, *args, **kwargs) if hook_configuration.methods.empty?

return body if hook_error?(body)
# FIXME: review this condition
next unless hook_configuration.methods.include?(method)
next if hook_configuration.skip?(method, *args, **kwargs)

body = run_hook(method, hook_configuration, -> {}, self, *args, **kwargs)

Expand Down
7 changes: 4 additions & 3 deletions lib/hook_configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,16 @@ def initialize(

attr_reader :hook, :methods, :inject, :exclude, :skip_when, :param_builder

# This determines if this specific hook should be skipped
# This determines if this specific hook should be skipped
# depending on the method or arguments.
def skip?(method, *args, **kwargs)
# binding.pry if hook.class.name == "PrepareHook"

return true if skip_when&.call(args, kwargs)
return true if exclude.include?(method)

return false if methods.empty?

return true if skip_when&.call(args, kwargs)

!methods.include?(method)
end
end
12 changes: 7 additions & 5 deletions spec/captain_hook_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def call(_klass, _method, _one, _two, _three, &_block)
end

class PrepareHook
def call(klass, method); end
def call(klass, method, dto:); end
end

class ServeHook
Expand Down Expand Up @@ -47,7 +47,9 @@ class ResourceWithHooks
hook :before,
hook: BeforeAllHook.new,
exclude: [:serve],
skip_when: ->(_args, kwargs) { !kwargs[:dto] }
skip_when: lambda { |_args, kwargs|
!kwargs[:dto]
}

hook :after, methods: %i[prepare invented_one], hook: PrepareHook.new

Expand Down Expand Up @@ -114,7 +116,7 @@ def prepare
context "when the method is not defined in the hook" do
it "calls all hooks with not methods defined" do
expect_any_instance_of(CookHook).not_to receive(:call).once.and_call_original
expect_any_instance_of(PrepareHook).to receive(:call).once
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
Expand Down Expand Up @@ -153,8 +155,8 @@ def prepare

subject.foo(dto: "fooing")

# expect_any_instance_of(BeforeAllHook).to receive(:call).once
# subject.prepare
expect_any_instance_of(BeforeAllHook).to receive(:call).once
subject.prepare
end
end
end

0 comments on commit 14652d8

Please sign in to comment.