Class: Omnizip::Registry
- Inherits:
-
Object
show all
- Defined in:
- lib/omnizip/registry.rb
Overview
Generic, thread-safe registry base class.
Subclasses override the hooks #not_found_error_class,
#normalize_key, and #label to specialize behavior. They may also
override #register / #get to add domain-specific arguments
(e.g., #register_with_formats on FilterRegistry).
The base class is intentionally minimal: it owns a Hash store and a
Mutex, and exposes the canonical CRUD quintet (+register+, get,
registered?, available, reset!). Backward-compat aliases are
provided so existing call sites keep working.
Class Method Summary
collapse
Class Method Details
.available ⇒ Object
Also known as:
all, strategies
67
68
69
|
# File 'lib/omnizip/registry.rb', line 67
def available
storage.keys
end
|
.each(&block) ⇒ Object
87
88
89
90
91
|
# File 'lib/omnizip/registry.rb', line 87
def each(&block)
return to_enum(:each) unless block
entries.each(&block)
end
|
.entries ⇒ Object
83
84
85
|
# File 'lib/omnizip/registry.rb', line 83
def entries
synchronize { storage.dup }
end
|
.get(key) ⇒ Object
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
|
# File 'lib/omnizip/registry.rb', line 44
def get(key)
normalized = normalize_key(key)
value = storage[normalized]
return value if value
trigger = lazy_triggers[normalized]
if trigger
synchronize { lazy_triggers.delete(normalized) }
trigger.call
value = storage[normalized]
return value if value
end
raise not_found_error_class,
"#{label} not registered: #{key.inspect}. " \
"Available: #{available.join(', ')}"
end
|
.label ⇒ Object
25
26
27
28
29
|
# File 'lib/omnizip/registry.rb', line 25
def label
return "Item" unless name
name.gsub(/^Omnizip::/, "")
end
|
.normalize_key(key) ⇒ Object
21
22
23
|
# File 'lib/omnizip/registry.rb', line 21
def normalize_key(key)
key.to_sym
end
|
.not_found_error_class ⇒ Object
17
18
19
|
# File 'lib/omnizip/registry.rb', line 17
def not_found_error_class
Omnizip::Error
end
|
.register(key, value) ⇒ Object
31
32
33
34
|
# File 'lib/omnizip/registry.rb', line 31
def register(key, value)
synchronize { storage[normalize_key(key)] = value }
value
end
|
.register_lazy(key, &trigger) ⇒ Object
Register a lazy trigger for key. When get(key) is called and
the key isn't yet in storage, the block is invoked. The block is
expected to trigger autoload of the implementation file (e.g., by
referencing a constant), which then self-registers.
40
41
42
|
# File 'lib/omnizip/registry.rb', line 40
def register_lazy(key, &trigger)
lazy_triggers[normalize_key(key)] = trigger
end
|
.registered?(key) ⇒ Boolean
62
63
64
65
|
# File 'lib/omnizip/registry.rb', line 62
def registered?(key)
normalized = normalize_key(key)
storage.key?(normalized) || lazy_triggers.key?(normalized)
end
|
.reset! ⇒ Object
Also known as:
clear, clear!, reset
73
74
75
76
77
78
|
# File 'lib/omnizip/registry.rb', line 73
def reset!
synchronize { storage.clear }
end
|