Class: Railsmith::Hooks::HookEntry

Inherits:
Object
  • Object
show all
Defined in:
lib/railsmith/hooks/hook_entry.rb

Overview

Immutable value object describing a single declared hook.

A hook applies to one or more action symbols, has a type (:before, :after, or :around), an optional :if/:unless condition, an optional :name for later skip_hook targeting, and an optional list of domain keys used by global hooks to scope execution to matching service domains.

Constant Summary collapse

VALID_TYPES =
%i[before after around].freeze
VALID_POLARITIES =
%i[if unless].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type:, actions:, block:, condition: nil, polarity: :if, name: nil, only_domains: nil) ⇒ HookEntry

Returns a new instance of HookEntry.



18
19
20
21
22
23
# File 'lib/railsmith/hooks/hook_entry.rb', line 18

def initialize(type:, actions:, block:, condition: nil, polarity: :if, name: nil, only_domains: nil)
  validate_initialize_args!(type, polarity, block, actions)
  assign_attributes({ type: type, actions: actions, block: block, condition: condition,
                      polarity: polarity, name: name, only_domains: only_domains })
  freeze
end

Instance Attribute Details

#actionsObject (readonly)

Returns the value of attribute actions.



16
17
18
# File 'lib/railsmith/hooks/hook_entry.rb', line 16

def actions
  @actions
end

#blockObject (readonly)

Returns the value of attribute block.



16
17
18
# File 'lib/railsmith/hooks/hook_entry.rb', line 16

def block
  @block
end

#conditionObject (readonly)

Returns the value of attribute condition.



16
17
18
# File 'lib/railsmith/hooks/hook_entry.rb', line 16

def condition
  @condition
end

#nameObject (readonly)

Returns the value of attribute name.



16
17
18
# File 'lib/railsmith/hooks/hook_entry.rb', line 16

def name
  @name
end

#only_domainsObject (readonly)

Returns the value of attribute only_domains.



16
17
18
# File 'lib/railsmith/hooks/hook_entry.rb', line 16

def only_domains
  @only_domains
end

#polarityObject (readonly)

Returns the value of attribute polarity.



16
17
18
# File 'lib/railsmith/hooks/hook_entry.rb', line 16

def polarity
  @polarity
end

#typeObject (readonly)

Returns the value of attribute type.



16
17
18
# File 'lib/railsmith/hooks/hook_entry.rb', line 16

def type
  @type
end

Instance Method Details

#applicable?(instance) ⇒ Boolean

Evaluate the if/unless condition (if any) against the given service instance. Returns true when the hook should fire for this call.

Returns:

  • (Boolean)


40
41
42
43
44
45
# File 'lib/railsmith/hooks/hook_entry.rb', line 40

def applicable?(instance)
  return true if condition.nil?

  raw = evaluate_condition(instance)
  polarity == :if ? !!raw : !raw
end

#applies_to?(action) ⇒ Boolean

True when this hook targets the given action symbol.

Returns:

  • (Boolean)


26
27
28
# File 'lib/railsmith/hooks/hook_entry.rb', line 26

def applies_to?(action)
  actions.include?(action.to_sym)
end

#matches_domain?(domain_key) ⇒ Boolean

True when the global hook’s domain filter matches (or is absent). Class-level hooks are not domain-filtered; this always returns true for them.

Returns:

  • (Boolean)


32
33
34
35
36
# File 'lib/railsmith/hooks/hook_entry.rb', line 32

def matches_domain?(domain_key)
  return true if only_domains.nil?

  only_domains.include?(Context.normalize_current_domain(domain_key))
end