Module: Custodian::Core::ActionRegistry

Defined in:
lib/custodian/core/action_registry.rb

Defined Under Namespace

Classes: AlreadyRegisteredError, InvalidOutcomeError, NotRegisteredError

Constant Summary collapse

VALID_SYMBOL_OUTCOMES =
%i[resolved failed].freeze

Class Method Summary collapse

Class Method Details

.call(action_name, node, custody, remaining) ⇒ Object

Raises:



25
26
27
28
29
30
31
32
33
# File 'lib/custodian/core/action_registry.rb', line 25

def call(action_name, node, custody, remaining)
  name = action_name.to_sym
  block = @mutex.synchronize { @registry[name] }
  raise NotRegisteredError, "action #{name.inspect} is not registered" unless block

  outcome = block.call(node, custody, remaining)
  validate_outcome!(name, outcome, remaining: remaining)
  outcome
end

.clear!Object

Removes all registrations. Intended for test isolation: call this in a before/around hook so each spec starts from a clean registry.



50
51
52
# File 'lib/custodian/core/action_registry.rb', line 50

def clear!
  @mutex.synchronize { @registry.clear }
end

.register(action_name, &block) ⇒ Object



16
17
18
19
20
21
22
23
# File 'lib/custodian/core/action_registry.rb', line 16

def register(action_name, &block)
  name = action_name.to_sym
  @mutex.synchronize do
    raise AlreadyRegisteredError, "action #{name.inspect} is already registered" if @registry.key?(name)

    @registry[name] = block
  end
end

.registered?(action_name) ⇒ Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/custodian/core/action_registry.rb', line 44

def registered?(action_name)
  @mutex.synchronize { @registry.key?(action_name.to_sym) }
end

.unregister(action_name) ⇒ Object



35
36
37
38
39
40
41
42
# File 'lib/custodian/core/action_registry.rb', line 35

def unregister(action_name)
  name = action_name.to_sym
  @mutex.synchronize do
    raise NotRegisteredError, "action #{name.inspect} is not registered" unless @registry.key?(name)

    @registry.delete(name)
  end
end

.validate_outcome!(name, outcome, remaining: nil) ⇒ Object

Public so callers outside the registry (e.g. Adjuster's phase hook) can validate an outcome using the exact same rule that governs actions invoked through #call: :resolved, :failed, or a non-negative Numeric.



58
59
60
61
62
63
64
65
66
67
# File 'lib/custodian/core/action_registry.rb', line 58

def validate_outcome!(name, outcome, remaining: nil)
  return if VALID_SYMBOL_OUTCOMES.include?(outcome)

  validate_numeric_outcome!(name, outcome, remaining) if outcome.is_a?(Numeric)
  return if outcome.is_a?(Numeric)

  raise InvalidOutcomeError,
        "action #{name.inspect} returned an invalid outcome: #{outcome.inspect} " \
        "(expected :resolved, :failed, or a Numeric)"
end