nxt_registry 0.3.6 → 0.3.8

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f951682f16517cd63a1424d95c52f0db75271bebc61c7fe2cde5a72821f3381e
4
- data.tar.gz: 1fd0496e6736a35e2a489c8791b9dfbf932fb5ba08c99d9213b908748b2a67e3
3
+ metadata.gz: 14c7e9fb583ad7e0962ad09933c359dd60d3d13e1dd701cc4d4b971ed408ed08
4
+ data.tar.gz: 879f214a0ce868bcf28fae668d7572ee2c680785749970b74bb2702074a02715
5
5
  SHA512:
6
- metadata.gz: 30d0f4d39292e592b6622d4f670a004ba5a801730b2e21f03bad0214615076ac5bba6d40b9849f1413f22c5463336e371f79bb60f74df40d19bdcdded743b8d5
7
- data.tar.gz: 29ba8e6fd7cae939e5e3ac171b5810153867c0a2c3da15f709d63ca536c3f90beff8c1e62cb5d29da7560152d55c73316b0e6921afe0b96ec7a06630163947ad
6
+ metadata.gz: ed961d75f5531d6cdf38ce49c72b7d06aaa836b32114b3e5df9508ea4250d099d0e559a78fae9bdd90093d312e00060d0c418994dd8ec2184557743e740ab0f9
7
+ data.tar.gz: 31e1e1ad67e6d59f1f95bd7b31a12f357333ea9542c51eaf5d4ff7bfabcc7542edf2a61033944f89447d13a8f23667a9a38c97cabab18d493c59e619f764353b
data/Gemfile.lock CHANGED
@@ -1,25 +1,25 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- nxt_registry (0.3.6)
4
+ nxt_registry (0.3.8)
5
5
  activesupport
6
6
 
7
7
  GEM
8
8
  remote: https://rubygems.org/
9
9
  specs:
10
- activesupport (6.1.0)
10
+ activesupport (6.1.1)
11
11
  concurrent-ruby (~> 1.0, >= 1.0.2)
12
12
  i18n (>= 1.6, < 2)
13
13
  minitest (>= 5.1)
14
14
  tzinfo (~> 2.0)
15
15
  zeitwerk (~> 2.3)
16
16
  coderay (1.1.3)
17
- concurrent-ruby (1.1.7)
17
+ concurrent-ruby (1.1.8)
18
18
  diff-lcs (1.4.4)
19
- i18n (1.8.5)
19
+ i18n (1.8.8)
20
20
  concurrent-ruby (~> 1.0)
21
21
  method_source (1.0.0)
22
- minitest (5.14.2)
22
+ minitest (5.14.3)
23
23
  pry (0.13.1)
24
24
  coderay (~> 1.1)
25
25
  method_source (~> 1.0)
@@ -28,15 +28,15 @@ GEM
28
28
  rspec-core (~> 3.10.0)
29
29
  rspec-expectations (~> 3.10.0)
30
30
  rspec-mocks (~> 3.10.0)
31
- rspec-core (3.10.0)
31
+ rspec-core (3.10.1)
32
32
  rspec-support (~> 3.10.0)
33
- rspec-expectations (3.10.0)
33
+ rspec-expectations (3.10.1)
34
34
  diff-lcs (>= 1.2.0, < 2.0)
35
35
  rspec-support (~> 3.10.0)
36
- rspec-mocks (3.10.0)
36
+ rspec-mocks (3.10.1)
37
37
  diff-lcs (>= 1.2.0, < 2.0)
38
38
  rspec-support (~> 3.10.0)
39
- rspec-support (3.10.0)
39
+ rspec-support (3.10.1)
40
40
  rspec_junit_formatter (0.4.1)
41
41
  rspec-core (>= 2, < 4, != 2.12.0)
42
42
  tzinfo (2.0.4)
@@ -56,4 +56,4 @@ DEPENDENCIES
56
56
  rspec_junit_formatter
57
57
 
58
58
  BUNDLED WITH
59
- 2.2.0
59
+ 2.2.4
data/README.md CHANGED
@@ -205,12 +205,21 @@ Layer.registry(:path).from(:munich).to(:amsterdam).via(:train) # => 'train'
205
205
 
206
206
  *Note that this feature is also available for registries with a single level only.*
207
207
 
208
- ### Restrict attributes to a certain set
208
+ ### Restrict keys to a certain set
209
209
 
210
- Use `attrs` to restrict which attributes can be registered on a specific level.
210
+ Use `allowed_keys` to restrict which keys can be registered on a specific level.
211
211
 
212
212
  ```ruby
213
- registry :example, attrs: %w[one two three]
213
+ registry :example, allowed_keys: %w[one two three]
214
+ ```
215
+
216
+ ### Require a certain set of keys to be registered
217
+
218
+ Use `required_keys` to enforce a certain set of keys to be registered on a specific level. This is especially helpful
219
+ if you use registries in multiple places and you want to ensure they all register the same set of keys.
220
+
221
+ ```ruby
222
+ registry :example, required_keys: %w[one two three]
214
223
  ```
215
224
 
216
225
  ### Default values
@@ -2,5 +2,7 @@ module NxtRegistry
2
2
  module Errors
3
3
  KeyAlreadyRegisteredError = Class.new(KeyError)
4
4
  KeyNotRegisteredError = Class.new(KeyError)
5
+ RequiredKeyMissing = Class.new(KeyError)
6
+ KeyNotAllowed = Class.new(KeyError)
5
7
  end
6
8
  end
@@ -8,11 +8,10 @@ module NxtRegistry
8
8
  @namespace = build_namespace
9
9
  @config = config
10
10
  @store = {}
11
- @attrs = nil
12
11
  @configured = false
13
12
  @patterns = []
13
+ @config = config
14
14
 
15
- setup_defaults(options)
16
15
  configure(&config)
17
16
  end
18
17
 
@@ -48,18 +47,24 @@ module NxtRegistry
48
47
  register!(name, Registry.new(name, **opts, &config))
49
48
  end
50
49
 
51
- def attr(name)
52
- key = transformed_key(name)
53
- raise KeyError, "Attribute #{key} already registered in #{namespace}" if attrs[key]
50
+ def required_keys(*keys)
51
+ @required_keys ||= []
52
+ return @required_keys if keys.empty?
53
+
54
+ @required_keys += keys.map { |key| transformed_key(key) }
55
+ end
56
+
57
+ def allowed_keys(*keys)
58
+ @allowed_keys ||= []
59
+ return @allowed_keys if keys.empty?
54
60
 
55
- attrs[key] = Attribute.new(key, self)
61
+ @allowed_keys += keys.map { |key| transformed_key(key) }
56
62
  end
57
63
 
58
- def attrs(*args)
59
- @attrs ||= {}
60
- return @attrs unless args.any?
64
+ alias attrs allowed_keys # @deprecated
61
65
 
62
- args.each { |name| attr(name) }
66
+ def attr(key)
67
+ allowed_keys(key) # @deprecated
63
68
  end
64
69
 
65
70
  def register(key = Blank.new, value = Blank.new, **options, &block)
@@ -134,9 +139,11 @@ module NxtRegistry
134
139
  delegate :size, :values, :each, :freeze, to: :store
135
140
 
136
141
  def configure(&block)
142
+ setup_defaults(options)
137
143
  define_accessors
138
144
  define_interface
139
- attrs(*Array(options.fetch(:attrs, [])))
145
+ allowed_keys(*Array(options.fetch(:allowed_keys, [])))
146
+ required_keys(*Array(options.fetch(:required_keys, [])))
140
147
 
141
148
  if block.present?
142
149
  if block.arity == 1
@@ -146,6 +153,7 @@ module NxtRegistry
146
153
  end
147
154
  end
148
155
 
156
+ validate_required_keys_given
149
157
  self.configured = true
150
158
  end
151
159
 
@@ -153,7 +161,7 @@ module NxtRegistry
153
161
  "Registry[#{name}] -> #{store.to_s}"
154
162
  end
155
163
 
156
- alias_method :inspect, :to_s
164
+ alias inspect to_s
157
165
 
158
166
  private
159
167
 
@@ -165,6 +173,14 @@ module NxtRegistry
165
173
  base.merge(opts).merge(parent: self)
166
174
  end
167
175
 
176
+ def validate_required_keys_given
177
+ required_keys.each do |key|
178
+ next if store.key?(key)
179
+
180
+ raise Errors::RequiredKeyMissing, "Required key '#{key}' missing in #{self}"
181
+ end
182
+ end
183
+
168
184
  def is_leaf?
169
185
  @is_leaf
170
186
  end
@@ -178,7 +194,7 @@ module NxtRegistry
178
194
  end
179
195
 
180
196
  raise ArgumentError, "Not allowed to register values in a registry that contains nested registries" unless is_leaf
181
- raise KeyError, "Keys are restricted to #{attrs.keys}" if attribute_not_allowed?(key)
197
+ raise KeyError, "Keys are restricted to #{allowed_keys}" if key_not_allowed?(key)
182
198
 
183
199
  on_key_already_registered && on_key_already_registered.call(key) if store[key] && raise_on_key_already_registered
184
200
 
@@ -303,10 +319,10 @@ module NxtRegistry
303
319
  end
304
320
  end
305
321
 
306
- def attribute_not_allowed?(key)
307
- return if attrs.empty?
322
+ def key_not_allowed?(key)
323
+ return if allowed_keys.empty?
308
324
 
309
- attrs.keys.exclude?(transformed_key(key))
325
+ allowed_keys.exclude?(transformed_key(key))
310
326
  end
311
327
 
312
328
  def resolve_default(key)
@@ -336,13 +352,6 @@ module NxtRegistry
336
352
  end
337
353
  end
338
354
 
339
- def initialize_copy(original)
340
- super
341
- @store = original.send(:store).deep_dup
342
- @options = original.send(:options).deep_dup
343
- @patterns = original.send(:patterns).dup
344
- end
345
-
346
355
  def build_namespace
347
356
  parent ? name.to_s.prepend("#{parent.send(:namespace)}.") : name.to_s
348
357
  end
@@ -358,5 +367,15 @@ module NxtRegistry
358
367
  def raise_invalid_accessor_name(name)
359
368
  raise ArgumentError, "#{self} already implements a method named: #{name}. Please choose a different accessor name"
360
369
  end
370
+
371
+ def initialize_copy(original)
372
+ super
373
+
374
+ containers = %i[store options]
375
+ variables = %i[patterns required_keys allowed_keys namespace on_key_already_registered on_key_not_registered]
376
+
377
+ containers.each { |c| instance_variable_set("@#{c}", original.send(c).deep_dup) }
378
+ variables.each { |v| instance_variable_set("@#{v}", original.send(v).dup) }
379
+ end
361
380
  end
362
381
  end
@@ -1,3 +1,3 @@
1
1
  module NxtRegistry
2
- VERSION = "0.3.6"
2
+ VERSION = '0.3.8'
3
3
  end
data/lib/nxt_registry.rb CHANGED
@@ -2,7 +2,6 @@ require 'active_support'
2
2
  require 'active_support/core_ext'
3
3
  require 'nxt_registry/version'
4
4
  require 'nxt_registry/blank'
5
- require 'nxt_registry/attribute'
6
5
  require 'nxt_registry/errors'
7
6
  require 'nxt_registry/registry_builder'
8
7
  require 'nxt_registry/registry'
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.6
4
+ version: 0.3.8
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-12-23 00:00:00.000000000 Z
14
+ date: 2021-02-03 00:00:00.000000000 Z
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
17
17
  name: activesupport
@@ -106,7 +106,6 @@ files:
106
106
  - bin/console
107
107
  - bin/setup
108
108
  - lib/nxt_registry.rb
109
- - lib/nxt_registry/attribute.rb
110
109
  - lib/nxt_registry/blank.rb
111
110
  - lib/nxt_registry/errors.rb
112
111
  - lib/nxt_registry/recursive_registry.rb
@@ -1,17 +0,0 @@
1
- module NxtRegistry
2
- class Attribute
3
- def initialize(name, registry, **options)
4
- @name = name
5
- @registry = registry
6
- @namespace = [name, registry.send(:namespace)].join('.')
7
- end
8
-
9
- def eql?(other)
10
- { name => registry.object_id } == { other.send(:name) => other.send(:registry).object_id }
11
- end
12
-
13
- private
14
-
15
- attr_reader :name, :registry
16
- end
17
- end