Class: Clickwrap::Registry
- Inherits:
-
Object
- Object
- Clickwrap::Registry
- 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
-
#kind ⇒ Object
readonly
Returns the value of attribute kind.
Instance Method Summary collapse
- #[](key) ⇒ Object
- #clear ⇒ Object
- #each ⇒ Object
- #empty? ⇒ Boolean
- #fetch(key, &fallback) ⇒ Object
-
#initialize(kind) ⇒ Registry
constructor
A new instance of Registry.
- #key?(key) ⇒ Boolean
- #keys ⇒ Object
- #register(key, definition) ⇒ Object
- #size ⇒ Object
- #values ⇒ Object
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
#kind ⇒ Object (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] |
#clear ⇒ Object
50 |
# File 'lib/clickwrap/registry.rb', line 50 def clear = @mutex.synchronize { @entries.clear } |
#each ⇒ Object
49 |
# File 'lib/clickwrap/registry.rb', line 49 def each(&) = @entries.each_value(&) |
#empty? ⇒ Boolean
48 |
# File 'lib/clickwrap/registry.rb', line 48 def empty? = @entries.empty? |
#fetch(key, &fallback) ⇒ Object
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
44 |
# File 'lib/clickwrap/registry.rb', line 44 def key?(key) = @entries.key?(key) |
#keys ⇒ Object
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 |
#size ⇒ Object
47 |
# File 'lib/clickwrap/registry.rb', line 47 def size = @entries.size |
#values ⇒ Object
46 |
# File 'lib/clickwrap/registry.rb', line 46 def values = @entries.values |