Class: Fmt::Registry
- Inherits:
-
Object
- Object
- Fmt::Registry
- Extended by:
- Forwardable
- Defined in:
- lib/fmt/registries/registry.rb
Overview
Registry for storing and retrieving String formatters i.e. Procs
Direct Known Subclasses
Instance Method Summary collapse
-
#add(key, overwrite: false, &block) ⇒ Object
Adds a keypair to the registry.
-
#any?(method_name) ⇒ Boolean
Indicates if a method name is registered for any Class.
-
#delete(key) ⇒ Object
Deletes a keypair from the registry.
-
#fetch(key, callable: nil, &block) ⇒ Object
Fetches a Proc from the registry.
-
#initialize ⇒ Registry
constructor
Constructor.
-
#key_for(callable) ⇒ Object
Retrieves the registered key for a Proc.
-
#merge!(other) ⇒ Object
Merges another registry into this one.
-
#none?(method_name) ⇒ Boolean
Indicates if a method name is unregistered.
-
#with_overrides(overrides, &block) ⇒ Object
Executes a block with registry overrides.
Constructor Details
#initialize ⇒ Registry
Constructor
14 15 16 |
# File 'lib/fmt/registries/registry.rb', line 14 def initialize @store = LRUCache.new(capacity: -1) end |
Instance Method Details
#add(key, overwrite: false, &block) ⇒ Object
Adds a keypair to the registry
41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/fmt/registries/registry.rb', line 41 def add(key, overwrite: false, &block) raise Error, "key must be an Array[Class | Module, Symbol]" unless key in [Class | Module, Symbol] return store[key] if store.key?(key) && !overwrite store.lock do store[key] = block block.instance_variable_set INSTANCE_VAR, key end block end |
#any?(method_name) ⇒ Boolean
Indicates if a method name is registered for any Class
25 26 27 |
# File 'lib/fmt/registries/registry.rb', line 25 def any?(method_name) !!method_names[method_name] end |
#delete(key) ⇒ Object
Deletes a keypair from the registry
57 58 59 60 61 62 |
# File 'lib/fmt/registries/registry.rb', line 57 def delete(key) store.lock do callable = store.delete(key) callable&.remove_instance_variable INSTANCE_VAR end end |
#fetch(key, callable: nil, &block) ⇒ Object
Fetches a Proc from the registry
69 70 71 72 |
# File 'lib/fmt/registries/registry.rb', line 69 def fetch(key, callable: nil, &block) callable ||= block store[key] || add(key, &callable) end |
#key_for(callable) ⇒ Object
Retrieves the registered key for a Proc
77 78 79 |
# File 'lib/fmt/registries/registry.rb', line 77 def key_for(callable) callable&.instance_variable_get INSTANCE_VAR end |
#merge!(other) ⇒ Object
Merges another registry into this one
84 85 86 87 88 |
# File 'lib/fmt/registries/registry.rb', line 84 def merge!(other) raise Error, "other must be a registry" unless other in Registry other.to_h.each { add(_1, &_2) } self end |
#none?(method_name) ⇒ Boolean
Indicates if a method name is unregistered
32 33 34 |
# File 'lib/fmt/registries/registry.rb', line 32 def none?(method_name) !any?(method_name) end |
#with_overrides(overrides, &block) ⇒ Object
Overrides will temporarily be added to the registry and will overwrite existing entries for the duration of the block Non overriden entries remain unchanged
Executes a block with registry overrides
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/fmt/registries/registry.rb', line 99 def with_overrides(overrides, &block) return yield unless overrides in Hash return yield unless overrides&.any? overrides.select! { [_1, _2] in [[Class | Module, Symbol], Proc] } originals = store.slice(*(store.keys & overrides.keys)) store.lock do overrides.each { add(_1, overwrite: true, &_2) } yield end ensure store.lock do overrides&.each { delete _1 } originals&.each { add(_1, overwrite: true, &_2) } end end |