Class: NxtRegistry::Registry
- Inherits:
-
Object
- Object
- NxtRegistry::Registry
- Defined in:
- lib/nxt_registry/registry.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#configured ⇒ Object
Returns the value of attribute configured.
-
#mutex ⇒ Object
readonly
Returns the value of attribute mutex.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Instance Method Summary collapse
- #[](key) ⇒ Object
- #[]=(key, value) ⇒ Object
- #allowed_keys(*keys) ⇒ Object (also: #attrs)
- #attr(key) ⇒ Object
- #configure(&block) ⇒ Object
- #exclude?(key) ⇒ Boolean
- #fetch(key, *args, &block) ⇒ Object
- #include?(key) ⇒ Boolean
-
#initialize(name = object_id.to_s, **options, &config) ⇒ Registry
constructor
A new instance of Registry.
- #key?(key) ⇒ Boolean
- #keys ⇒ Object
- #level(name, **options, &config) ⇒ Object
- #register(key = Blank.new, value = Blank.new, **options, &block) ⇒ Object
- #register!(key = Blank.new, value = Blank.new, **options, &block) ⇒ Object
- #registry(name, **options, &config) ⇒ Object
- #registry!(name, **options, &config) ⇒ Object
- #required_keys(*keys) ⇒ Object
- #resolve(*keys) ⇒ Object
- #resolve!(*keys) ⇒ Object
- #to_h ⇒ Object
- #to_s ⇒ Object (also: #inspect)
Constructor Details
#initialize(name = object_id.to_s, **options, &config) ⇒ Registry
Returns a new instance of Registry.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
# File 'lib/nxt_registry/registry.rb', line 3 def initialize(name = object_id.to_s, **, &config) @options = @name = name @parent = [:parent] @is_leaf = true @namespace = build_namespace @config = config @store = {} @configured = false @patterns = [] @config = config @mutex = Mutex.new configure(&config) end |
Instance Attribute Details
#configured ⇒ Object
Returns the value of attribute configured.
20 21 22 |
# File 'lib/nxt_registry/registry.rb', line 20 def configured @configured end |
#mutex ⇒ Object (readonly)
Returns the value of attribute mutex.
19 20 21 |
# File 'lib/nxt_registry/registry.rb', line 19 def mutex @mutex end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
19 20 21 |
# File 'lib/nxt_registry/registry.rb', line 19 def name @name end |
Instance Method Details
#[](key) ⇒ Object
111 112 113 |
# File 'lib/nxt_registry/registry.rb', line 111 def [](key) resolve!(key) end |
#[]=(key, value) ⇒ Object
115 116 117 |
# File 'lib/nxt_registry/registry.rb', line 115 def []=(key, value) register(key, value) end |
#allowed_keys(*keys) ⇒ Object Also known as: attrs
58 59 60 61 62 63 |
# File 'lib/nxt_registry/registry.rb', line 58 def allowed_keys(*keys) @allowed_keys ||= [] return @allowed_keys if keys.empty? @allowed_keys += keys.map { |key| transformed_key(key) } end |
#attr(key) ⇒ Object
67 68 69 |
# File 'lib/nxt_registry/registry.rb', line 67 def attr(key) allowed_keys(key) # @deprecated end |
#configure(&block) ⇒ Object
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/nxt_registry/registry.rb', line 142 def configure(&block) setup_defaults() define_accessors define_interface allowed_keys(*Array(.fetch(:allowed_keys, []))) required_keys(*Array(.fetch(:required_keys, []))) if block.present? if block.arity == 1 instance_exec(self, &block) else instance_exec(&block) end end validate_required_keys_given self.configured = true end |
#exclude?(key) ⇒ Boolean
131 132 133 |
# File 'lib/nxt_registry/registry.rb', line 131 def exclude?(key) store.exclude?(transformed_key(key)) end |
#fetch(key, *args, &block) ⇒ Object
135 136 137 138 |
# File 'lib/nxt_registry/registry.rb', line 135 def fetch(key, *args, &block) key = matching_key(key) store.fetch(key, *args, &block) end |
#include?(key) ⇒ Boolean
127 128 129 |
# File 'lib/nxt_registry/registry.rb', line 127 def include?(key) store.include?(transformed_key(key)) end |
#key?(key) ⇒ Boolean
123 124 125 |
# File 'lib/nxt_registry/registry.rb', line 123 def key?(key) store.key?(transformed_key(key)) end |
#keys ⇒ Object
119 120 121 |
# File 'lib/nxt_registry/registry.rb', line 119 def keys store.keys.map(&method(:transformed_key)) end |
#level(name, **options, &config) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/nxt_registry/registry.rb', line 22 def level(name, **, &config) = .merge(parent: self) if is_a_blank?(default) self.is_leaf = false self.default = RegistryBuilder.new do Registry.new(name, **, &config) end # Call the builder once to guarantee we do not create a registry with a broken setup default.call elsif default.is_a?(RegistryBuilder) raise ArgumentError, 'Multiple nestings on the same level' else raise ArgumentError, 'Default values cannot be defined on registries that nest others' end end |
#register(key = Blank.new, value = Blank.new, **options, &block) ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/nxt_registry/registry.rb', line 71 def register(key = Blank.new, value = Blank.new, **, &block) if block_given? if is_a_blank?(value) registry(key, **, &block) else raise_register_argument_error end else __register(key, value, raise_on_key_already_registered: true) end end |
#register!(key = Blank.new, value = Blank.new, **options, &block) ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/nxt_registry/registry.rb', line 83 def register!(key = Blank.new, value = Blank.new, **, &block) if block_given? if is_a_blank?(value) registry!(key, **, &block) else raise_register_argument_error end else __register(key, value, raise_on_key_already_registered: false) end end |
#registry(name, **options, &config) ⇒ Object
41 42 43 44 |
# File 'lib/nxt_registry/registry.rb', line 41 def registry(name, **, &config) opts = () register(name, Registry.new(name, **opts, &config)) end |
#registry!(name, **options, &config) ⇒ Object
46 47 48 49 |
# File 'lib/nxt_registry/registry.rb', line 46 def registry!(name, **, &config) opts = () register!(name, Registry.new(name, **opts, &config)) end |
#required_keys(*keys) ⇒ Object
51 52 53 54 55 56 |
# File 'lib/nxt_registry/registry.rb', line 51 def required_keys(*keys) @required_keys ||= [] return @required_keys if keys.empty? @required_keys += keys.map { |key| transformed_key(key) } end |
#resolve(*keys) ⇒ Object
101 102 103 104 105 |
# File 'lib/nxt_registry/registry.rb', line 101 def resolve(*keys) keys.inject(self) do |current_registry, key| current_registry.send(:__resolve, key, raise_on_key_not_registered: false) || break end end |
#resolve!(*keys) ⇒ Object
95 96 97 98 99 |
# File 'lib/nxt_registry/registry.rb', line 95 def resolve!(*keys) keys.inject(self) do |current_registry, key| current_registry.send(:__resolve, key, raise_on_key_not_registered: true) end end |
#to_h ⇒ Object
107 108 109 |
# File 'lib/nxt_registry/registry.rb', line 107 def to_h store end |
#to_s ⇒ Object Also known as: inspect
161 162 163 |
# File 'lib/nxt_registry/registry.rb', line 161 def to_s "Registry[#{name}] -> #{store.to_s}" end |