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.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(kind) ⇒ Registry

Returns a new instance of Registry.



13
14
15
16
17
# File 'lib/clickwrap/registry.rb', line 13

def initialize(kind)
  @kind = kind
  @entries = {}
  @mutex = Mutex.new
end

Instance Attribute Details

#kindObject (readonly)

Returns the value of attribute kind.



19
20
21
# File 'lib/clickwrap/registry.rb', line 19

def kind
  @kind
end

Instance Method Details

#[](key) ⇒ Object



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

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

#clearObject



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

def clear = @mutex.synchronize { @entries.clear }

#eachObject



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

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

#empty?Boolean

Returns:

  • (Boolean)


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

def empty? = @entries.empty?

#fetch(key, &fallback) ⇒ Object

Raises:



35
36
37
38
39
40
41
# File 'lib/clickwrap/registry.rb', line 35

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)


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

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

#keysObject



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

def keys = @entries.keys

#register(key, definition) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/clickwrap/registry.rb', line 21

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



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

def size = @entries.size

#valuesObject



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

def values = @entries.values