nxt_registry 0.3.1 → 0.3.6
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 +21 -0
- data/Gemfile.lock +22 -22
- data/README.md +47 -7
- data/lib/nxt_registry.rb +8 -6
- data/lib/nxt_registry/registry.rb +51 -15
- data/lib/nxt_registry/singleton.rb +15 -0
- data/lib/nxt_registry/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f951682f16517cd63a1424d95c52f0db75271bebc61c7fe2cde5a72821f3381e
|
4
|
+
data.tar.gz: 1fd0496e6736a35e2a489c8791b9dfbf932fb5ba08c99d9213b908748b2a67e3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 30d0f4d39292e592b6622d4f670a004ba5a801730b2e21f03bad0214615076ac5bba6d40b9849f1413f22c5463336e371f79bb60f74df40d19bdcdded743b8d5
|
7
|
+
data.tar.gz: 29ba8e6fd7cae939e5e3ac171b5810153867c0a2c3da15f709d63ca536c3f90beff8c1e62cb5d29da7560152d55c73316b0e6921afe0b96ec7a06630163947ad
|
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,24 @@
|
|
1
|
+
# v0.3.5 2020-12-23
|
2
|
+
|
3
|
+
- Allow to inherit options in nested registries
|
4
|
+
|
5
|
+
# v0.3.5 2020-12-04
|
6
|
+
|
7
|
+
- Allow patterns as keys
|
8
|
+
|
9
|
+
# v0.3.4 2020-12-04
|
10
|
+
|
11
|
+
- Bring back Singleton
|
12
|
+
- Fix mistakes in readme
|
13
|
+
|
14
|
+
# v0.3.3 2020-11-24
|
15
|
+
|
16
|
+
- Fix: Pass key to resolver instead of value
|
17
|
+
|
18
|
+
# v0.3.2 2020-09-29
|
19
|
+
|
20
|
+
- Fix interface definition
|
21
|
+
|
1
22
|
# v0.3.1 2020-09-23
|
2
23
|
|
3
24
|
- Allow to define custom accessors for registries
|
data/Gemfile.lock
CHANGED
@@ -1,18 +1,18 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
nxt_registry (0.3.
|
4
|
+
nxt_registry (0.3.6)
|
5
5
|
activesupport
|
6
6
|
|
7
7
|
GEM
|
8
8
|
remote: https://rubygems.org/
|
9
9
|
specs:
|
10
|
-
activesupport (6.0
|
10
|
+
activesupport (6.1.0)
|
11
11
|
concurrent-ruby (~> 1.0, >= 1.0.2)
|
12
|
-
i18n (>=
|
13
|
-
minitest (
|
14
|
-
tzinfo (~>
|
15
|
-
zeitwerk (~> 2.
|
12
|
+
i18n (>= 1.6, < 2)
|
13
|
+
minitest (>= 5.1)
|
14
|
+
tzinfo (~> 2.0)
|
15
|
+
zeitwerk (~> 2.3)
|
16
16
|
coderay (1.1.3)
|
17
17
|
concurrent-ruby (1.1.7)
|
18
18
|
diff-lcs (1.4.4)
|
@@ -24,28 +24,28 @@ GEM
|
|
24
24
|
coderay (~> 1.1)
|
25
25
|
method_source (~> 1.0)
|
26
26
|
rake (12.3.3)
|
27
|
-
rspec (3.
|
28
|
-
rspec-core (~> 3.
|
29
|
-
rspec-expectations (~> 3.
|
30
|
-
rspec-mocks (~> 3.
|
31
|
-
rspec-core (3.
|
32
|
-
rspec-support (~> 3.
|
33
|
-
rspec-expectations (3.
|
27
|
+
rspec (3.10.0)
|
28
|
+
rspec-core (~> 3.10.0)
|
29
|
+
rspec-expectations (~> 3.10.0)
|
30
|
+
rspec-mocks (~> 3.10.0)
|
31
|
+
rspec-core (3.10.0)
|
32
|
+
rspec-support (~> 3.10.0)
|
33
|
+
rspec-expectations (3.10.0)
|
34
34
|
diff-lcs (>= 1.2.0, < 2.0)
|
35
|
-
rspec-support (~> 3.
|
36
|
-
rspec-mocks (3.
|
35
|
+
rspec-support (~> 3.10.0)
|
36
|
+
rspec-mocks (3.10.0)
|
37
37
|
diff-lcs (>= 1.2.0, < 2.0)
|
38
|
-
rspec-support (~> 3.
|
39
|
-
rspec-support (3.
|
38
|
+
rspec-support (~> 3.10.0)
|
39
|
+
rspec-support (3.10.0)
|
40
40
|
rspec_junit_formatter (0.4.1)
|
41
41
|
rspec-core (>= 2, < 4, != 2.12.0)
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
zeitwerk (2.4.0)
|
42
|
+
tzinfo (2.0.4)
|
43
|
+
concurrent-ruby (~> 1.0)
|
44
|
+
zeitwerk (2.4.2)
|
46
45
|
|
47
46
|
PLATFORMS
|
48
47
|
ruby
|
48
|
+
x86_64-darwin-19
|
49
49
|
|
50
50
|
DEPENDENCIES
|
51
51
|
bundler (~> 2.0)
|
@@ -56,4 +56,4 @@ DEPENDENCIES
|
|
56
56
|
rspec_junit_formatter
|
57
57
|
|
58
58
|
BUNDLED WITH
|
59
|
-
2.
|
59
|
+
2.2.0
|
data/README.md
CHANGED
@@ -27,24 +27,23 @@ Or install it yourself as:
|
|
27
27
|
|
28
28
|
## Instance Level
|
29
29
|
|
30
|
-
|
30
|
+
If you simply need a single global instance of a registry include `NxtRegistry::Singleton`:
|
31
31
|
|
32
32
|
```ruby
|
33
33
|
class Example
|
34
|
-
include NxtRegistry
|
34
|
+
include NxtRegistry::Singleton
|
35
35
|
|
36
|
-
registry
|
36
|
+
registry do
|
37
37
|
register(:ruby, 'Stone')
|
38
38
|
register(:python, 'Snake')
|
39
39
|
register(:javascript, 'undefined')
|
40
40
|
end
|
41
41
|
end
|
42
42
|
|
43
|
-
|
44
|
-
example.registry(:languages).resolve(:ruby) # => 'Stone'
|
43
|
+
Example.resolve(:ruby) # => 'Stone'
|
45
44
|
```
|
46
45
|
|
47
|
-
Alternatively you can
|
46
|
+
Alternatively you can simply create instances of `NxtRegistry::Registry`:
|
48
47
|
|
49
48
|
```ruby
|
50
49
|
registry = NxtRegistry::Registry.new do
|
@@ -59,7 +58,7 @@ registry.resolve(:aki) # => 'Aki'
|
|
59
58
|
|
60
59
|
## Class Level
|
61
60
|
|
62
|
-
You can add registries on the class level simply by extending your class with `NxtRegistry`
|
61
|
+
You can also add registries on the class level simply by extending your class with `NxtRegistry`
|
63
62
|
|
64
63
|
```ruby
|
65
64
|
class OtherExample
|
@@ -83,6 +82,30 @@ OtherExample.registry(:errors).resolve(KeyError)
|
|
83
82
|
OtherExample.registry(:country_codes).resolve(:germany)
|
84
83
|
# => :de
|
85
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
|
+
|
86
109
|
### Readers
|
87
110
|
|
88
111
|
Access your defined registries with the `registry(:country_code)` method.
|
@@ -112,6 +135,23 @@ Nested.registry(:developers).resolve(:frontend, :igor)
|
|
112
135
|
# => 'Igor'
|
113
136
|
```
|
114
137
|
|
138
|
+
#### Inherit options in nested registries
|
139
|
+
|
140
|
+
```ruby
|
141
|
+
class Nested
|
142
|
+
extend NxtRegistry
|
143
|
+
|
144
|
+
registry :developers, default: 'options can be inherited' do
|
145
|
+
register(:frontend, inherit_options: true) do
|
146
|
+
register(:igor, 'Igor')
|
147
|
+
register(:ben, 'Ben')
|
148
|
+
end
|
149
|
+
end
|
150
|
+
end
|
151
|
+
|
152
|
+
Nested.registry(:developers).resolve(:frontend, :blank)
|
153
|
+
# => 'options can be inherited'
|
154
|
+
```
|
115
155
|
|
116
156
|
### Defining specific nesting levels of a registry
|
117
157
|
|
data/lib/nxt_registry.rb
CHANGED
@@ -1,3 +1,4 @@
|
|
1
|
+
require 'active_support'
|
1
2
|
require 'active_support/core_ext'
|
2
3
|
require 'nxt_registry/version'
|
3
4
|
require 'nxt_registry/blank'
|
@@ -6,6 +7,7 @@ require 'nxt_registry/errors'
|
|
6
7
|
require 'nxt_registry/registry_builder'
|
7
8
|
require 'nxt_registry/registry'
|
8
9
|
require 'nxt_registry/recursive_registry'
|
10
|
+
require 'nxt_registry/singleton'
|
9
11
|
|
10
12
|
module NxtRegistry
|
11
13
|
def registry(name, **options, &config)
|
@@ -19,17 +21,17 @@ module NxtRegistry
|
|
19
21
|
private
|
20
22
|
|
21
23
|
def build_registry(registry_class, name, **options, &config)
|
22
|
-
|
23
|
-
|
24
|
+
registry = registries.resolve(name)
|
25
|
+
|
26
|
+
if registry.present?
|
24
27
|
if registry.configured
|
25
|
-
registry
|
28
|
+
return registry
|
26
29
|
else
|
27
30
|
raise_unconfigured_registry_accessed(name)
|
28
31
|
end
|
29
32
|
else
|
30
33
|
registry = registry_class.new(name, **options, &config)
|
31
|
-
registries
|
32
|
-
registry
|
34
|
+
registries.register(name, registry)
|
33
35
|
end
|
34
36
|
end
|
35
37
|
|
@@ -38,6 +40,6 @@ module NxtRegistry
|
|
38
40
|
end
|
39
41
|
|
40
42
|
def registries
|
41
|
-
@registries ||=
|
43
|
+
@registries ||= Registry.new(:registries)
|
42
44
|
end
|
43
45
|
end
|
@@ -10,9 +10,10 @@ module NxtRegistry
|
|
10
10
|
@store = {}
|
11
11
|
@attrs = nil
|
12
12
|
@configured = false
|
13
|
+
@patterns = []
|
13
14
|
|
14
15
|
setup_defaults(options)
|
15
|
-
configure(&config)
|
16
|
+
configure(&config)
|
16
17
|
end
|
17
18
|
|
18
19
|
attr_reader :name
|
@@ -38,12 +39,12 @@ module NxtRegistry
|
|
38
39
|
end
|
39
40
|
|
40
41
|
def registry(name, **options, &config)
|
41
|
-
opts = options
|
42
|
+
opts = conditionally_inherit_options(options)
|
42
43
|
register(name, Registry.new(name, **opts, &config))
|
43
44
|
end
|
44
45
|
|
45
46
|
def registry!(name, **options, &config)
|
46
|
-
opts = options
|
47
|
+
opts = conditionally_inherit_options(options)
|
47
48
|
register!(name, Registry.new(name, **opts, &config))
|
48
49
|
end
|
49
50
|
|
@@ -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,15 +157,25 @@ module NxtRegistry
|
|
155
157
|
|
156
158
|
private
|
157
159
|
|
158
|
-
attr_reader :namespace, :parent, :config, :store, :options, :accessor
|
159
|
-
attr_accessor :is_leaf
|
160
|
+
attr_reader :namespace, :parent, :config, :store, :options, :accessor, :patterns
|
161
|
+
attr_accessor :is_leaf, :interface_defined
|
162
|
+
|
163
|
+
def conditionally_inherit_options(opts)
|
164
|
+
base = opts.delete(:inherit_options) ? options : {}
|
165
|
+
base.merge(opts).merge(parent: self)
|
166
|
+
end
|
160
167
|
|
161
168
|
def is_leaf?
|
162
169
|
@is_leaf
|
163
170
|
end
|
164
171
|
|
165
172
|
def __register(key, value, raise_on_key_already_registered: true)
|
166
|
-
key =
|
173
|
+
key = if key.is_a?(Regexp)
|
174
|
+
patterns << key
|
175
|
+
key
|
176
|
+
else
|
177
|
+
transformed_key(key)
|
178
|
+
end
|
167
179
|
|
168
180
|
raise ArgumentError, "Not allowed to register values in a registry that contains nested registries" unless is_leaf
|
169
181
|
raise KeyError, "Keys are restricted to #{attrs.keys}" if attribute_not_allowed?(key)
|
@@ -179,6 +191,8 @@ module NxtRegistry
|
|
179
191
|
value = if is_leaf?
|
180
192
|
if store.key?(key)
|
181
193
|
store.fetch(key)
|
194
|
+
elsif (pattern = matching_pattern(key))
|
195
|
+
store.fetch(pattern)
|
182
196
|
else
|
183
197
|
if is_a_blank?(default)
|
184
198
|
return unless raise_on_key_not_registered
|
@@ -195,11 +209,7 @@ module NxtRegistry
|
|
195
209
|
store[key] ||= default.call
|
196
210
|
end
|
197
211
|
|
198
|
-
value =
|
199
|
-
value.call(*[value].take(value.arity))
|
200
|
-
else
|
201
|
-
value
|
202
|
-
end
|
212
|
+
value = call_or_value(value, key)
|
203
213
|
|
204
214
|
if resolver
|
205
215
|
resolver.call(value)
|
@@ -208,12 +218,35 @@ module NxtRegistry
|
|
208
218
|
end
|
209
219
|
end
|
210
220
|
|
221
|
+
def matching_key(key)
|
222
|
+
key = transformed_key(key)
|
223
|
+
# if key is present it always wins over patterns
|
224
|
+
return key if store.key?(key)
|
225
|
+
|
226
|
+
matching_pattern(key) || key
|
227
|
+
end
|
228
|
+
|
229
|
+
def call_or_value(value, key)
|
230
|
+
return value unless call
|
231
|
+
return value if value.is_a?(NxtRegistry::Registry)
|
232
|
+
return value unless value.respond_to?(:call)
|
233
|
+
|
234
|
+
args = [key, value]
|
235
|
+
value.call(*args.take(value.arity))
|
236
|
+
end
|
237
|
+
|
238
|
+
def matching_pattern(key)
|
239
|
+
patterns.find { |pattern| key.match?(pattern) }
|
240
|
+
end
|
241
|
+
|
211
242
|
def define_interface
|
212
|
-
|
243
|
+
return if interface_defined
|
244
|
+
|
245
|
+
raise_invalid_accessor_name(accessor) if respond_to?(accessor.to_s)
|
213
246
|
accessor_with_bang = "#{accessor}!"
|
214
247
|
raise_invalid_accessor_name(accessor_with_bang) if respond_to?(accessor_with_bang)
|
215
248
|
|
216
|
-
define_singleton_method accessor do |key = Blank.new, value = Blank.new|
|
249
|
+
define_singleton_method accessor.to_s do |key = Blank.new, value = Blank.new|
|
217
250
|
return self if is_a_blank?(key)
|
218
251
|
|
219
252
|
key = transformed_key(key)
|
@@ -236,6 +269,8 @@ module NxtRegistry
|
|
236
269
|
register!(key, value)
|
237
270
|
end
|
238
271
|
end
|
272
|
+
|
273
|
+
self.interface_defined = true
|
239
274
|
end
|
240
275
|
|
241
276
|
def setup_defaults(options)
|
@@ -243,7 +278,7 @@ module NxtRegistry
|
|
243
278
|
@memoize = options.fetch(:memoize) { true }
|
244
279
|
@call = options.fetch(:call) { true }
|
245
280
|
@resolver = options.fetch(:resolver, false)
|
246
|
-
@transform_keys = options.fetch(:transform_keys) { ->(key) { key.to_s } }
|
281
|
+
@transform_keys = options.fetch(:transform_keys) { ->(key) { key.is_a?(Regexp) ? key : key.to_s } }
|
247
282
|
@accessor = options.fetch(:accessor) { name }
|
248
283
|
|
249
284
|
@on_key_already_registered = options.fetch(:on_key_already_registered) { ->(key) { raise_key_already_registered_error(key) } }
|
@@ -305,6 +340,7 @@ module NxtRegistry
|
|
305
340
|
super
|
306
341
|
@store = original.send(:store).deep_dup
|
307
342
|
@options = original.send(:options).deep_dup
|
343
|
+
@patterns = original.send(:patterns).dup
|
308
344
|
end
|
309
345
|
|
310
346
|
def build_namespace
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module NxtRegistry
|
2
|
+
module Singleton
|
3
|
+
include NxtRegistry
|
4
|
+
|
5
|
+
def self.included(base)
|
6
|
+
base.extend(self)
|
7
|
+
end
|
8
|
+
|
9
|
+
def registry(type = Registry, **options, &config)
|
10
|
+
@registry ||= build_registry(type, self.class.name, **options, &config)
|
11
|
+
end
|
12
|
+
|
13
|
+
delegate_missing_to :registry
|
14
|
+
end
|
15
|
+
end
|
data/lib/nxt_registry/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: nxt_registry
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
4
|
+
version: 0.3.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Andreas Robecke
|
@@ -11,7 +11,7 @@ authors:
|
|
11
11
|
autorequire:
|
12
12
|
bindir: exe
|
13
13
|
cert_chain: []
|
14
|
-
date: 2020-
|
14
|
+
date: 2020-12-23 00:00:00.000000000 Z
|
15
15
|
dependencies:
|
16
16
|
- !ruby/object:Gem::Dependency
|
17
17
|
name: activesupport
|
@@ -112,6 +112,7 @@ files:
|
|
112
112
|
- lib/nxt_registry/recursive_registry.rb
|
113
113
|
- lib/nxt_registry/registry.rb
|
114
114
|
- lib/nxt_registry/registry_builder.rb
|
115
|
+
- lib/nxt_registry/singleton.rb
|
115
116
|
- lib/nxt_registry/version.rb
|
116
117
|
- nxt_registry.gemspec
|
117
118
|
homepage: https://github.com/nxt-insurance
|