Module: Hecks::Runtime::Registry::Verification

Included in:
Hecks::Runtime::Registry
Defined in:
lib/hecks/runtime/registry/verification.rb

Overview

The wiring gate: every bind names a declared aggregate, every adapter satisfies the verb its port declares, every world setting is a field the adapter admits, and the default adapter is usable at all. Included into Registry — verify! is what a boot calls after loading, and the smaller checks are also called piecemeal by the repository factory.

Instance Method Summary collapse

Instance Method Details

#adapter_class(name) ⇒ Object



98
99
100
101
102
103
# File 'lib/hecks/runtime/registry/verification.rb', line 98

def adapter_class(name)
  Adapters.const_get(name)
rescue NameError
  raise WiringError, "no Ruby adapter implementation for #{name.inspect} " \
                     "(expected Hecks::Adapters::#{name})"
end

#check_settings(bind, settings) ⇒ Object

Raises:



76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/hecks/runtime/registry/verification.rb', line 76

def check_settings(bind, settings)
  adapter = @adapters[bind.adapter]
  return unless adapter

  declared = settings.keys - [:adapter]
  unknown  = declared.reject { |field| adapter.declares?(field) }
  return if unknown.empty?

  raise WiringError,
        "#{bind.adapter} does not declare #{unknown.map(&:inspect).join(', ')}" \
        "it declares #{adapter.all_fields.map(&:inspect).join(', ')}. " \
        "Add the field to the adapter, or remove it from the world."
end

#check_verb(bind) ⇒ Object

Raises:



67
68
69
70
71
72
73
74
# File 'lib/hecks/runtime/registry/verification.rb', line 67

def check_verb(bind)
  port = port_for(bind)
  return if port.verb.to_s == bind.verb.to_s

  raise WiringError,
        "#{bind.adapter} implements the #{port.name} port (verb #{port.verb}) " \
        "and cannot satisfy #{bind.verb}"
end

#port_for(bind) ⇒ Object

Raises:



90
91
92
93
94
95
96
# File 'lib/hecks/runtime/registry/verification.rb', line 90

def port_for(bind)
  adapter = @adapters[bind.adapter]
  raise WiringError, "unknown adapter #{bind.adapter.inspect}" unless adapter

  @ports[adapter.port] ||
    raise(WiringError, "adapter #{bind.adapter} declares unknown port #{adapter.port.inspect}")
end

#verify!Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/hecks/runtime/registry/verification.rb', line 13

def verify!
  verify_default_adapter!

  @hecksagons.each_value do |hexagon|
    refuse_ungoverned_roles!(hexagon)

    hexagon.binds.each do |bind|
      # A domain-level default (§0) — `persisted_by "Heki"` bare,
      # applying to whichever aggregates don't override it — names
      # no aggregate of its own, so there's nothing to look up in
      # the bluebook for THIS row specifically. Still validate its
      # own adapter/verb shape (the same reason
      # `verify_default_adapter!` checks the framework-wide
      # default the same way, aggregate-less). Coverage of real
      # aggregates that only resolve THROUGH this default comes
      # from their own dispatch-time `BindingPolicy.resolve` —
      # deliberately not required to be exhaustive here, the same
      # leniency this method already extended to any aggregate
      # left out of an explicit bind list entirely (real test
      # fixtures bind only the aggregates they exercise).
      if bind.aggregate.nil?
        check_verb(bind)
        next
      end

      aggregate = bluebook(hexagon.domain)&.aggregate(bind.aggregate_name)
      raise WiringError, "#{bind.aggregate} is bound but not declared in the bluebook" unless aggregate

      check_verb(bind)

      repository(hexagon.domain, aggregate)
    end
  end
  self
end

#verify_default_adapter!Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/hecks/runtime/registry/verification.rb', line 49

def verify_default_adapter!
  name = Ports::Persistence::DEFAULT_ADAPTER

  check_verb(
    Bluebook::Bind.new(
      aggregate: "(default)",
      verb:      Ports::Persistence::VERB,
      adapter:   name
    )
  )
  adapter_class(name)
  self
rescue WiringError => error
  raise WiringError,
        "the default persistence adapter (#{name}) is not usable, so an " \
        "aggregate with no bind could not be given one: #{error.message}"
end