Class: Railsmith::Hooks::HookEntry
- Inherits:
-
Object
- Object
- Railsmith::Hooks::HookEntry
- 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
-
#actions ⇒ Object
readonly
Returns the value of attribute actions.
-
#block ⇒ Object
readonly
Returns the value of attribute block.
-
#condition ⇒ Object
readonly
Returns the value of attribute condition.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#only_domains ⇒ Object
readonly
Returns the value of attribute only_domains.
-
#polarity ⇒ Object
readonly
Returns the value of attribute polarity.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Instance Method Summary collapse
-
#applicable?(instance) ⇒ Boolean
Evaluate the
if/unlesscondition (if any) against the given service instance. -
#applies_to?(action) ⇒ Boolean
True when this hook targets the given action symbol.
-
#initialize(type:, actions:, block:, condition: nil, polarity: :if, name: nil, only_domains: nil) ⇒ HookEntry
constructor
A new instance of HookEntry.
-
#matches_domain?(domain_key) ⇒ Boolean
True when the global hook’s domain filter matches (or is absent).
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
#actions ⇒ Object (readonly)
Returns the value of attribute actions.
16 17 18 |
# File 'lib/railsmith/hooks/hook_entry.rb', line 16 def actions @actions end |
#block ⇒ Object (readonly)
Returns the value of attribute block.
16 17 18 |
# File 'lib/railsmith/hooks/hook_entry.rb', line 16 def block @block end |
#condition ⇒ Object (readonly)
Returns the value of attribute condition.
16 17 18 |
# File 'lib/railsmith/hooks/hook_entry.rb', line 16 def condition @condition end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
16 17 18 |
# File 'lib/railsmith/hooks/hook_entry.rb', line 16 def name @name end |
#only_domains ⇒ Object (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 |
#polarity ⇒ Object (readonly)
Returns the value of attribute polarity.
16 17 18 |
# File 'lib/railsmith/hooks/hook_entry.rb', line 16 def polarity @polarity end |
#type ⇒ Object (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.
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.
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.
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 |