Module: SecurityBox::Registry

Defined in:
lib/security_box/registry.rb

Overview

Named, reusable profiles: SecurityBox.register(:name, from: :base) { |c| ... } stores an immutable Configuration built through the builder DSL; spawn resolves a profile and derives per-call overrides with #with.

Profiles must be registered before they are referenced by from:, cannot be redefined (immutability avoids accidental shadowing) and live for the process lifetime; tests can reset the registry with clear!.

Constant Summary collapse

MUTEX =
Mutex.new

Class Method Summary collapse

Class Method Details

.clear! ⇒ Object



46
47
48
# File 'lib/security_box/registry.rb', line 46

def clear!
  MUTEX.synchronize { @profiles.clear }
end

.profiles ⇒ Object



42
43
44
# File 'lib/security_box/registry.rb', line 42

def profiles
  MUTEX.synchronize { @profiles.dup.freeze }
end

.register(name, from: nil, &block) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/security_box/registry.rb', line 17

def register(name, from: nil, &block)
  key = normalize(name)
  MUTEX.synchronize do
    raise InvalidConfiguration, "profile #{key.inspect} is already registered" if @profiles.key?(key)

    builder = Configuration::Builder.new
    block&.call(builder)
    base = base_of(from)
    if base[:fuel_ms] && builder.changes.key?(:fuel)
      raise InvalidConfiguration,
            "base profile #{from.inspect} uses fuel_ms; override fuel_ms (or clear it with fuel_ms: nil) instead of fuel"
    end
    @profiles[key] = Configuration.build(**base, **builder.changes)
  end
end

.resolve(name) ⇒ Object



33
34
35
36
37
38
39
40
# File 'lib/security_box/registry.rb', line 33

def resolve(name)
  key = normalize(name)
  MUTEX.synchronize do
    @profiles.fetch(key) do
      raise InvalidConfiguration, "unknown profile #{key.inspect}; register it first with SecurityBox.register"
    end
  end
end