Class: Igniter::Contracts::Assembly::HookSpec

Inherits:
Object
  • Object
show all
Defined in:
lib/igniter/contracts/assembly/hook_spec.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(registry:, method_name:, required_keywords:, role:, return_policy: :opaque, result_validator: nil) ⇒ HookSpec

Returns a new instance of HookSpec.



9
10
11
12
13
14
15
16
17
18
# File 'lib/igniter/contracts/assembly/hook_spec.rb', line 9

def initialize(registry:, method_name:, required_keywords:, role:, return_policy: :opaque,
               result_validator: nil)
  @registry = registry.to_sym
  @method_name = method_name.to_sym
  @required_keywords = required_keywords.map(&:to_sym).freeze
  @role = role.to_sym
  @return_policy = return_policy.to_sym
  @result_validator = result_validator
  freeze
end

Instance Attribute Details

#method_nameObject (readonly)

Returns the value of attribute method_name.



7
8
9
# File 'lib/igniter/contracts/assembly/hook_spec.rb', line 7

def method_name
  @method_name
end

#registryObject (readonly)

Returns the value of attribute registry.



7
8
9
# File 'lib/igniter/contracts/assembly/hook_spec.rb', line 7

def registry
  @registry
end

#required_keywordsObject (readonly)

Returns the value of attribute required_keywords.



7
8
9
# File 'lib/igniter/contracts/assembly/hook_spec.rb', line 7

def required_keywords
  @required_keywords
end

#return_policyObject (readonly)

Returns the value of attribute return_policy.



7
8
9
# File 'lib/igniter/contracts/assembly/hook_spec.rb', line 7

def return_policy
  @return_policy
end

#roleObject (readonly)

Returns the value of attribute role.



7
8
9
# File 'lib/igniter/contracts/assembly/hook_spec.rb', line 7

def role
  @role
end

Instance Method Details

#validate!(key, implementation) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/igniter/contracts/assembly/hook_spec.rb', line 20

def validate!(key, implementation)
  unless implementation.respond_to?(method_name)
    raise InvalidHookImplementationError,
          "#{registry} entry #{key} must respond to ##{method_name}"
  end

  parameters = parameters_for(implementation)
  return if accepts_required_keywords?(parameters)

  missing = missing_keywords(parameters)
  expected_keywords = required_keywords.map { |name| "#{name}:" }.join(", ")
  missing_labels = missing.map { |name| "#{name}:" }.join(", ")

  raise InvalidHookImplementationError,
        "#{registry} entry #{key} must accept keywords #{expected_keywords}; missing #{missing_labels}"
end

#validate_result!(key, result) ⇒ Object



37
38
39
40
41
42
43
44
45
# File 'lib/igniter/contracts/assembly/hook_spec.rb', line 37

def validate_result!(key, result)
  return result unless @result_validator

  message = @result_validator.call(result)
  return result unless message

  raise InvalidHookResultError,
        "#{registry} entry #{key} (#{role}) #{message}"
end