Skip to content

Commit

Permalink
Rename HookConfiguration to CaptainHook::Configuration
Browse files Browse the repository at this point in the history
Follow the starndard ruby gem folder structure with a gem_name.rb
and a gem_name folder with the rest of the library

This makes it predictable for external developers and it will prevent
from polluting the global namespace with classes from the gem
  • Loading branch information
atd committed Nov 7, 2024
1 parent 5c19169 commit 6c37b9f
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 37 deletions.
4 changes: 2 additions & 2 deletions lib/captain_hook.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

require_relative "./hook_configuration"
require_relative "./captain_hook/configuration"

# Strong inspiration on https://github.com/collectiveidea/interactor/blob/master/lib/interactor/hooks.rb
#
Expand Down Expand Up @@ -30,7 +30,7 @@ def hook(
skip_when: nil,
param_builder: nil
)
hooks[kind][hook] = HookConfiguration.new(
hooks[kind][hook] = Configuration.new(
hook: hook,
include: include,
inject: inject,
Expand Down
35 changes: 35 additions & 0 deletions lib/captain_hook/configuration.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# frozen_string_literal: true

module CaptainHook
# Hook configuration parameters these determine how a specific hook will run
class Configuration
def initialize(
hook:,
include: [],
inject: [],
exclude: [],
skip_when: nil,
param_builder: nil
)
@hook = hook
@include = include
@inject = inject
@exclude = exclude
@skip_when = skip_when
@param_builder = param_builder
end

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

# This determines if this specific hook should be skipped
# depending on the method or arguments.
def skip?(method, *args, **kwargs)
return true if skip_when&.call(args, kwargs)
return true if exclude.include?(method)

return false if include.empty?

!include.include?(method)
end
end
end
33 changes: 0 additions & 33 deletions lib/hook_configuration.rb

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class DummyHook
def call(klass, method, dto:); end
end

describe HookConfiguration do
describe CaptainHook::Configuration do
let(:hook) { DummyHook.new }
let(:args) { {} }
let(:kwargs) { {} }
Expand All @@ -17,7 +17,7 @@ def call(klass, method, dto:); end
let(:skip_when) { nil }

subject do
HookConfiguration.new(
described_class.new(
hook: hook,
include: include,
exclude: [excluded_method],
Expand Down

0 comments on commit 6c37b9f

Please sign in to comment.