nxt_registry 0.3.4 → 0.3.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +4 -0
- data/Gemfile.lock +1 -1
- data/README.md +24 -0
- data/lib/nxt_registry/registry.rb +38 -11
- data/lib/nxt_registry/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e3c1e56b8913dea9833ea5fac4b1b837389de5df7aa269f3b2f04977f2ddceb3
|
4
|
+
data.tar.gz: 782bcb6ecce2753f77b488c29806bf1b2127714fe82aeed8ab2564297a43b95b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1e512d832f3d40ae60ffcfad8578589138c1f00479c2d2dab797c7e9faa6a37cec77da419d671715af55629e0efeac09ef5761bf39afcafe077c0d6f6af740a0
|
7
|
+
data.tar.gz: 3f17bfcc73233107e2daa4db54905115aea50e6f926b8e7ed10aea6d098ff418a4f39ced0e688b02a0a207e0562f02274bdfd9f59d568734485454afe7e1f06d
|
data/CHANGELOG.md
CHANGED
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -82,6 +82,30 @@ OtherExample.registry(:errors).resolve(KeyError)
|
|
82
82
|
OtherExample.registry(:country_codes).resolve(:germany)
|
83
83
|
# => :de
|
84
84
|
```
|
85
|
+
|
86
|
+
## Register Patterns
|
87
|
+
|
88
|
+
You can also register values with patterns as keys. Non pattern keys are always evaluated first and then patterns
|
89
|
+
will be tried to match by definition sequence.
|
90
|
+
|
91
|
+
```ruby
|
92
|
+
class Example
|
93
|
+
extend NxtRegistry
|
94
|
+
|
95
|
+
registry :status_codes do
|
96
|
+
register(/\A4\d{2}\z/, 'Client errors')
|
97
|
+
register(/\A5.*\z/, 'Server errors')
|
98
|
+
register('422', 'Unprocessable Entity')
|
99
|
+
register(:'503', 'Internal Server Error')
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
Example.registry(:status_codes).resolve('503') # => "Internal Server Error"
|
104
|
+
Example.registry(:status_codes).resolve(503) # => "Internal Server Error"
|
105
|
+
Example.registry(:status_codes).resolve(422) # => "Unprocessable Entity"
|
106
|
+
Example.registry(:status_codes).resolve(404) # => "Client Errors"
|
107
|
+
```
|
108
|
+
|
85
109
|
### Readers
|
86
110
|
|
87
111
|
Access your defined registries with the `registry(:country_code)` method.
|
@@ -10,6 +10,7 @@ module NxtRegistry
|
|
10
10
|
@store = {}
|
11
11
|
@attrs = nil
|
12
12
|
@configured = false
|
13
|
+
@patterns = []
|
13
14
|
|
14
15
|
setup_defaults(options)
|
15
16
|
configure(&config)
|
@@ -126,7 +127,8 @@ module NxtRegistry
|
|
126
127
|
end
|
127
128
|
|
128
129
|
def fetch(key, *args, &block)
|
129
|
-
|
130
|
+
key = matching_key(key)
|
131
|
+
store.fetch(key, *args, &block)
|
130
132
|
end
|
131
133
|
|
132
134
|
delegate :size, :values, :each, :freeze, to: :store
|
@@ -155,7 +157,7 @@ module NxtRegistry
|
|
155
157
|
|
156
158
|
private
|
157
159
|
|
158
|
-
attr_reader :namespace, :parent, :config, :store, :options, :accessor
|
160
|
+
attr_reader :namespace, :parent, :config, :store, :options, :accessor, :patterns
|
159
161
|
attr_accessor :is_leaf, :interface_defined
|
160
162
|
|
161
163
|
def is_leaf?
|
@@ -163,7 +165,12 @@ module NxtRegistry
|
|
163
165
|
end
|
164
166
|
|
165
167
|
def __register(key, value, raise_on_key_already_registered: true)
|
166
|
-
key =
|
168
|
+
key = if key.is_a?(Regexp)
|
169
|
+
patterns << key
|
170
|
+
key
|
171
|
+
else
|
172
|
+
transformed_key(key)
|
173
|
+
end
|
167
174
|
|
168
175
|
raise ArgumentError, "Not allowed to register values in a registry that contains nested registries" unless is_leaf
|
169
176
|
raise KeyError, "Keys are restricted to #{attrs.keys}" if attribute_not_allowed?(key)
|
@@ -179,6 +186,8 @@ module NxtRegistry
|
|
179
186
|
value = if is_leaf?
|
180
187
|
if store.key?(key)
|
181
188
|
store.fetch(key)
|
189
|
+
elsif (pattern = matching_pattern(key))
|
190
|
+
store.fetch(pattern)
|
182
191
|
else
|
183
192
|
if is_a_blank?(default)
|
184
193
|
return unless raise_on_key_not_registered
|
@@ -195,11 +204,7 @@ module NxtRegistry
|
|
195
204
|
store[key] ||= default.call
|
196
205
|
end
|
197
206
|
|
198
|
-
value =
|
199
|
-
value.call(*[key].take(value.arity))
|
200
|
-
else
|
201
|
-
value
|
202
|
-
end
|
207
|
+
value = call_or_value(value, key)
|
203
208
|
|
204
209
|
if resolver
|
205
210
|
resolver.call(value)
|
@@ -208,14 +213,35 @@ module NxtRegistry
|
|
208
213
|
end
|
209
214
|
end
|
210
215
|
|
216
|
+
def matching_key(key)
|
217
|
+
key = transformed_key(key)
|
218
|
+
# if key is present it always wins over patterns
|
219
|
+
return key if store.key?(key)
|
220
|
+
|
221
|
+
matching_pattern(key) || key
|
222
|
+
end
|
223
|
+
|
224
|
+
def call_or_value(value, key)
|
225
|
+
return value unless call
|
226
|
+
return value if value.is_a?(NxtRegistry::Registry)
|
227
|
+
return value unless value.respond_to?(:call)
|
228
|
+
|
229
|
+
args = [key, value]
|
230
|
+
value.call(*args.take(value.arity))
|
231
|
+
end
|
232
|
+
|
233
|
+
def matching_pattern(key)
|
234
|
+
patterns.find { |pattern| key.match?(pattern) }
|
235
|
+
end
|
236
|
+
|
211
237
|
def define_interface
|
212
238
|
return if interface_defined
|
213
239
|
|
214
|
-
raise_invalid_accessor_name(accessor) if respond_to?(accessor)
|
240
|
+
raise_invalid_accessor_name(accessor) if respond_to?(accessor.to_s)
|
215
241
|
accessor_with_bang = "#{accessor}!"
|
216
242
|
raise_invalid_accessor_name(accessor_with_bang) if respond_to?(accessor_with_bang)
|
217
243
|
|
218
|
-
define_singleton_method accessor do |key = Blank.new, value = Blank.new|
|
244
|
+
define_singleton_method accessor.to_s do |key = Blank.new, value = Blank.new|
|
219
245
|
return self if is_a_blank?(key)
|
220
246
|
|
221
247
|
key = transformed_key(key)
|
@@ -247,7 +273,7 @@ module NxtRegistry
|
|
247
273
|
@memoize = options.fetch(:memoize) { true }
|
248
274
|
@call = options.fetch(:call) { true }
|
249
275
|
@resolver = options.fetch(:resolver, false)
|
250
|
-
@transform_keys = options.fetch(:transform_keys) { ->(key) { key.to_s } }
|
276
|
+
@transform_keys = options.fetch(:transform_keys) { ->(key) { key.is_a?(Regexp) ? key : key.to_s } }
|
251
277
|
@accessor = options.fetch(:accessor) { name }
|
252
278
|
|
253
279
|
@on_key_already_registered = options.fetch(:on_key_already_registered) { ->(key) { raise_key_already_registered_error(key) } }
|
@@ -309,6 +335,7 @@ module NxtRegistry
|
|
309
335
|
super
|
310
336
|
@store = original.send(:store).deep_dup
|
311
337
|
@options = original.send(:options).deep_dup
|
338
|
+
@patterns = original.send(:patterns).dup
|
312
339
|
end
|
313
340
|
|
314
341
|
def build_namespace
|
data/lib/nxt_registry/version.rb
CHANGED