Class: Clickwrap::Registry

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/clickwrap/registry.rb

Overview

A small thread-safe registry for compiled documents, policies, and retention classes.

Definitions are declared once at boot and read on every request, so writes take a lock and reads do not. A reload clears the complete registry before loading the declaration files again. Seeing the same key twice inside one load is therefore always ambiguous and is refused instead of letting file order silently decide which policy governs a production action.

A registry may carry a seed: built-in entries that are its floor rather than its contents. Clearing re-runs the seed, so a reload returns to the built-ins, never to nothing — which is what lets a gem-shipped default (the indefinite retention class) survive every to_prepare.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(kind, &seed) ⇒ Registry

Returns a new instance of Registry.



18
19
20
21
22
23
24
# File 'lib/clickwrap/registry.rb', line 18

def initialize(kind, &seed)
  @kind = kind
  @entries = {}
  @mutex = Mutex.new
  @seed = seed
  @seed&.call(self)
end

Instance Attribute Details

#kindObject (readonly)

Returns the value of attribute kind.



26
27
28
# File 'lib/clickwrap/registry.rb', line 26

def kind
  @kind
end

Instance Method Details

#[](key) ⇒ Object



50
# File 'lib/clickwrap/registry.rb', line 50

def [](key) = @entries[key]

#clearObject



58
59
60
61
62
# File 'lib/clickwrap/registry.rb', line 58

def clear
  @mutex.synchronize { @entries.clear }
  @seed&.call(self)
  self
end

#eachObject



56
# File 'lib/clickwrap/registry.rb', line 56

def each(&) = @entries.each_value(&)

#empty?Boolean

Returns:

  • (Boolean)


55
# File 'lib/clickwrap/registry.rb', line 55

def empty? = @entries.empty?

#fetch(key, &fallback) ⇒ Object

Raises:



42
43
44
45
46
47
48
# File 'lib/clickwrap/registry.rb', line 42

def fetch(key, &fallback)
  entry = @entries[key]
  return entry if entry
  return fallback.call if fallback

  raise NotDefinedError, "No #{kind} registered for #{key.inspect}"
end

#key?(key) ⇒ Boolean

Returns:

  • (Boolean)


51
# File 'lib/clickwrap/registry.rb', line 51

def key?(key) = @entries.key?(key)

#keysObject



52
# File 'lib/clickwrap/registry.rb', line 52

def keys = @entries.keys

#register(key, definition) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/clickwrap/registry.rb', line 28

def register(key, definition)
  @mutex.synchronize do
    if @entries.key?(key)
      raise DefinitionError,
            "The #{kind} key #{key.inspect} is declared more than once. Give every " \
            "#{kind} one stable key; Clickwrap will not let load order silently replace " \
            "a server-owned definition."
    end

    @entries[key] = definition
  end
  definition
end

#sizeObject



54
# File 'lib/clickwrap/registry.rb', line 54

def size = @entries.size

#valuesObject



53
# File 'lib/clickwrap/registry.rb', line 53

def values = @entries.values