Class: Omnizip::Registry

Inherits:
Object
  • 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

.availableObject Also known as: all, strategies



66
67
68
# File 'lib/omnizip/registry.rb', line 66

def available
  storage.keys
end

.each(&block) ⇒ Object



86
87
88
89
90
# File 'lib/omnizip/registry.rb', line 86

def each(&block)
  return to_enum(:each) unless block

  entries.each(&block)
end

.entriesObject



82
83
84
# File 'lib/omnizip/registry.rb', line 82

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
# 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
    trigger.call
    value = storage[normalized]
    return value if value
  end

  raise not_found_error_class,
        "#{label} not registered: #{key.inspect}. " \
        "Available: #{available.join(', ')}"
end

.labelObject



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_classObject



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

Returns:

  • (Boolean)


61
62
63
64
# File 'lib/omnizip/registry.rb', line 61

def registered?(key)
  normalized = normalize_key(key)
  storage.key?(normalized) || lazy_triggers.key?(normalized)
end

.reset!Object Also known as: clear, clear!, reset



72
73
74
75
76
77
# File 'lib/omnizip/registry.rb', line 72

def reset!
  # Clear stored entries but keep lazy triggers — the builtin
  # autoload list should survive a reset so subsequent +get+ calls
  # for builtins still trigger the autoload (which re-registers).
  synchronize { storage.clear }
end