nxt_registry 0.3.8 → 0.3.9

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: 14c7e9fb583ad7e0962ad09933c359dd60d3d13e1dd701cc4d4b971ed408ed08
4
- data.tar.gz: 879f214a0ce868bcf28fae668d7572ee2c680785749970b74bb2702074a02715
3
+ metadata.gz: 9d8d5b1247dfc045e077ccf9d79cf3ea010c9c56434133f8210f3b77af439d0d
4
+ data.tar.gz: 9bd0fc656681a64856f6f21996914ebdeca063ea7cd5585746c29840c4f9694e
5
5
  SHA512:
6
- metadata.gz: ed961d75f5531d6cdf38ce49c72b7d06aaa836b32114b3e5df9508ea4250d099d0e559a78fae9bdd90093d312e00060d0c418994dd8ec2184557743e740ab0f9
7
- data.tar.gz: 31e1e1ad67e6d59f1f95bd7b31a12f357333ea9542c51eaf5d4ff7bfabcc7542edf2a61033944f89447d13a8f23667a9a38c97cabab18d493c59e619f764353b
6
+ metadata.gz: c136e4279f1fe2907e43ff24f96a28bdec6cf3b095842831c6a1834b468d174e138a370ede137cd318f879c23579c5664a93c5820e7409b6985d4829f7905f30
7
+ data.tar.gz: 2793ac826854219b0e5017e84264ce3908b28c1c9148cddef1dd37dc70b761445ab07d7bd7185f5f2cd58a220cdc1bcb065f6fe49e359a1716922db85fe4532f
data/CHANGELOG.md CHANGED
@@ -1,3 +1,9 @@
1
+ # v0.3.9 2021-03-10
2
+
3
+ - Synchronize access with Mutex
4
+ - Add key_resolver
5
+ - Fix: Inherit options set through accessors
6
+
1
7
  # v0.3.5 2020-12-23
2
8
 
3
9
  - Allow to inherit options in nested registries
data/Gemfile.lock CHANGED
@@ -1,13 +1,13 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- nxt_registry (0.3.8)
4
+ nxt_registry (0.3.9)
5
5
  activesupport
6
6
 
7
7
  GEM
8
8
  remote: https://rubygems.org/
9
9
  specs:
10
- activesupport (6.1.1)
10
+ activesupport (6.1.3)
11
11
  concurrent-ruby (~> 1.0, >= 1.0.2)
12
12
  i18n (>= 1.6, < 2)
13
13
  minitest (>= 5.1)
@@ -16,11 +16,11 @@ GEM
16
16
  coderay (1.1.3)
17
17
  concurrent-ruby (1.1.8)
18
18
  diff-lcs (1.4.4)
19
- i18n (1.8.8)
19
+ i18n (1.8.9)
20
20
  concurrent-ruby (~> 1.0)
21
21
  method_source (1.0.0)
22
- minitest (5.14.3)
23
- pry (0.13.1)
22
+ minitest (5.14.4)
23
+ pry (0.14.0)
24
24
  coderay (~> 1.1)
25
25
  method_source (~> 1.0)
26
26
  rake (12.3.3)
data/README.md CHANGED
@@ -2,8 +2,7 @@
2
2
 
3
3
  # NxtRegistry
4
4
 
5
- `NxtRegistry` is a simple implementation of the container pattern. It allows you to register and resolve values in nested
6
- structures.
5
+ `NxtRegistry` is a simple container that allows you to register and resolve values in nested structures.
7
6
 
8
7
  ## Installation
9
8
 
@@ -258,19 +257,21 @@ registry.resolve(:one)
258
257
  # => 2020-01-02 23:56:18 +0100
259
258
  ```
260
259
 
261
- ### Resolver
260
+ ### Resolve callbacks
262
261
 
263
- You can register a resolver block if you want to lay hands on your values after they have been resolved.
264
- A resolver can be anything that implements `:call` to which the value is passed.
262
+ You can hook into the before and after resolver callbacks in case you need to lay hands on your values
263
+ before and / or after resolving. A callback can be anything that implements `:call` to which the value is passed.
265
264
 
266
265
  ```ruby
267
266
  registry :example do
268
- resolver ->(value) { value * 2 }
269
- register(:one, 1)
267
+ key_resolver ->(key) { key.strip }
268
+ resolver ->(value) { value.upcase }
269
+
270
+ register(:input, 'output')
270
271
  end
271
272
 
272
- registry.resolve(:one)
273
- # => 2
273
+ registry.resolve(' input ')
274
+ # => 'OUTPUT'
274
275
  ```
275
276
 
276
277
  ### Transform keys
@@ -11,11 +11,12 @@ module NxtRegistry
11
11
  @configured = false
12
12
  @patterns = []
13
13
  @config = config
14
+ @mutex = Mutex.new
14
15
 
15
16
  configure(&config)
16
17
  end
17
18
 
18
- attr_reader :name
19
+ attr_reader :name, :mutex
19
20
  attr_accessor :configured
20
21
 
21
22
  def level(name, **options, &config)
@@ -186,51 +187,53 @@ module NxtRegistry
186
187
  end
187
188
 
188
189
  def __register(key, value, raise_on_key_already_registered: true)
189
- key = if key.is_a?(Regexp)
190
- patterns << key
191
- key
192
- else
193
- transformed_key(key)
194
- end
190
+ mutex.synchronize do
191
+ key = if key.is_a?(Regexp)
192
+ patterns << key
193
+ key
194
+ else
195
+ transformed_key(key)
196
+ end
195
197
 
196
- raise ArgumentError, "Not allowed to register values in a registry that contains nested registries" unless is_leaf
197
- raise KeyError, "Keys are restricted to #{allowed_keys}" if key_not_allowed?(key)
198
+ raise ArgumentError, "Not allowed to register values in a registry that contains nested registries" unless is_leaf
199
+ raise KeyError, "Keys are restricted to #{allowed_keys}" if key_not_allowed?(key)
198
200
 
199
- on_key_already_registered && on_key_already_registered.call(key) if store[key] && raise_on_key_already_registered
201
+ on_key_already_registered && on_key_already_registered.call(key) if store[key] && raise_on_key_already_registered
200
202
 
201
- store[key] = value
203
+ store[key] = value
204
+ end
202
205
  end
203
206
 
204
207
  def __resolve(key, raise_on_key_not_registered: true)
205
- key = transformed_key(key)
208
+ mutex.synchronize do
209
+ key = transformed_key(key)
206
210
 
207
- value = if is_leaf?
208
- if store.key?(key)
209
- store.fetch(key)
210
- elsif (pattern = matching_pattern(key))
211
- store.fetch(pattern)
212
- else
213
- if is_a_blank?(default)
214
- return unless raise_on_key_not_registered
211
+ value = if is_leaf?
212
+ resolved_key = key_resolver.call(key)
215
213
 
216
- on_key_not_registered && on_key_not_registered.call(key)
214
+ if store.key?(resolved_key)
215
+ store.fetch(resolved_key)
216
+ elsif (pattern = matching_pattern(resolved_key))
217
+ store.fetch(pattern)
217
218
  else
218
- value = resolve_default(key)
219
- return value unless memoize
219
+ if is_a_blank?(default)
220
+ return unless raise_on_key_not_registered
220
221
 
221
- store[key] ||= value
222
+ on_key_not_registered && on_key_not_registered.call(key)
223
+ else
224
+ value = resolve_default(key)
225
+ return value unless memoize
226
+
227
+ store[key] ||= value
228
+ end
222
229
  end
230
+ else
231
+ store[key] ||= default.call
223
232
  end
224
- else
225
- store[key] ||= default.call
226
- end
227
233
 
228
- value = call_or_value(value, key)
234
+ value = call_or_value(value, key)
229
235
 
230
- if resolver
231
236
  resolver.call(value)
232
- else
233
- value
234
237
  end
235
238
  end
236
239
 
@@ -293,7 +296,8 @@ module NxtRegistry
293
296
  @default = options.fetch(:default) { Blank.new }
294
297
  @memoize = options.fetch(:memoize) { true }
295
298
  @call = options.fetch(:call) { true }
296
- @resolver = options.fetch(:resolver, false)
299
+ @resolver = options.fetch(:resolver, ->(val) { val })
300
+ @key_resolver = options.fetch(:key_resolver, ->(val) { val })
297
301
  @transform_keys = options.fetch(:transform_keys) { ->(key) { key.is_a?(Regexp) ? key : key.to_s } }
298
302
  @accessor = options.fetch(:accessor) { name }
299
303
 
@@ -302,18 +306,20 @@ module NxtRegistry
302
306
  end
303
307
 
304
308
  def define_accessors
305
- %w[default memoize call resolver transform_keys on_key_already_registered on_key_not_registered].each do |attribute|
309
+ %w[default memoize call resolver key_resolver transform_keys on_key_already_registered on_key_not_registered].each do |attribute|
306
310
  define_singleton_method attribute do |value = Blank.new, &block|
307
311
  value = block if block
308
312
 
309
313
  if is_a_blank?(value)
310
314
  instance_variable_get("@#{attribute}")
311
315
  else
316
+ options[attribute.to_sym] ||= value
312
317
  instance_variable_set("@#{attribute}", value)
313
318
  end
314
319
  end
315
320
 
316
321
  define_singleton_method "#{attribute}=" do |value|
322
+ options[attribute.to_sym] ||= value
317
323
  instance_variable_set("@#{attribute}", value)
318
324
  end
319
325
  end
@@ -371,6 +377,7 @@ module NxtRegistry
371
377
  def initialize_copy(original)
372
378
  super
373
379
 
380
+ @mutex = Mutex.new
374
381
  containers = %i[store options]
375
382
  variables = %i[patterns required_keys allowed_keys namespace on_key_already_registered on_key_not_registered]
376
383
 
@@ -1,3 +1,3 @@
1
1
  module NxtRegistry
2
- VERSION = '0.3.8'
2
+ VERSION = '0.3.9'
3
3
  end
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.8
4
+ version: 0.3.9
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: 2021-02-03 00:00:00.000000000 Z
14
+ date: 2021-03-10 00:00:00.000000000 Z
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
17
17
  name: activesupport