nxt_registry 0.1.1 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.circleci/config.yml +56 -0
- data/.editorconfig +15 -0
- data/.rakeTasks +1 -1
- data/CHANGELOG.md +10 -0
- data/Gemfile +4 -0
- data/Gemfile.lock +25 -22
- data/README.md +166 -46
- data/lib/nxt_registry.rb +5 -4
- data/lib/nxt_registry/registry.rb +21 -6
- data/lib/nxt_registry/singleton.rb +17 -0
- data/lib/nxt_registry/version.rb +1 -1
- data/nxt_registry.gemspec +1 -1
- metadata +9 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3643055d95f31f6addec18700fd8d7d9157bc4f97848acef8340c11668cfa3df
|
4
|
+
data.tar.gz: 25018d4d1f657991137e0c94bf8afacaca70dcd44008ff6cb96783ea82aa064d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 74aefe510917560c5dbc3686834ce64aeb815e8f67d99718f6abf38dd08d639f9a8bba14f3cd2275f7f790b3b853b0207006888ee9303ed4d4b706e99d6e1d8b
|
7
|
+
data.tar.gz: 2225edb2af2a5493b90642934d415115a8577043f69fa329bfe99c0676997d4ad68c023c1c79fc1552519a2ed50a5478ebe440ad936ee5832956a27ebad1caec
|
@@ -0,0 +1,56 @@
|
|
1
|
+
# Ruby CircleCI 2.0 configuration file
|
2
|
+
#
|
3
|
+
# Check https://circleci.com/docs/2.0/language-ruby/ for more details
|
4
|
+
#
|
5
|
+
version: 2
|
6
|
+
jobs:
|
7
|
+
build:
|
8
|
+
docker:
|
9
|
+
# specify the version you desire here
|
10
|
+
- image: circleci/ruby:2.6.5-node
|
11
|
+
environment:
|
12
|
+
BUNDLER_VERSION: 2.1.4
|
13
|
+
|
14
|
+
working_directory: ~/repo
|
15
|
+
|
16
|
+
steps:
|
17
|
+
- checkout
|
18
|
+
|
19
|
+
# Download and cache dependencies
|
20
|
+
- restore_cache:
|
21
|
+
keys:
|
22
|
+
- v1-dependencies-{{ checksum "Gemfile.lock" }}
|
23
|
+
|
24
|
+
- run: gem install bundler --version $BUNDLER_VERSION
|
25
|
+
|
26
|
+
- run:
|
27
|
+
name: install dependencies
|
28
|
+
command: |
|
29
|
+
bundle install --jobs=4 --retry=3 --path vendor/bundle
|
30
|
+
|
31
|
+
- save_cache:
|
32
|
+
paths:
|
33
|
+
- ./vendor/bundle
|
34
|
+
key: v1-dependencies-{{ checksum "Gemfile.lock" }}
|
35
|
+
|
36
|
+
# run tests!
|
37
|
+
- run:
|
38
|
+
name: run tests
|
39
|
+
command: |
|
40
|
+
mkdir /tmp/test-results
|
41
|
+
TEST_FILES="$(circleci tests glob "spec/**/*_spec.rb" | \
|
42
|
+
circleci tests split --split-by=timings)"
|
43
|
+
|
44
|
+
bundle exec rspec \
|
45
|
+
--format progress \
|
46
|
+
--format RspecJunitFormatter \
|
47
|
+
--out /tmp/test-results/rspec.xml \
|
48
|
+
--format progress \
|
49
|
+
$TEST_FILES
|
50
|
+
|
51
|
+
# collect reports
|
52
|
+
- store_test_results:
|
53
|
+
path: /tmp/test-results
|
54
|
+
- store_artifacts:
|
55
|
+
path: /tmp/test-results
|
56
|
+
destination: test-results
|
data/.editorconfig
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
# EditorConfig is awesome: http://EditorConfig.org
|
2
|
+
|
3
|
+
# top-most EditorConfig file
|
4
|
+
root = true
|
5
|
+
|
6
|
+
# Unix-style newlines with a newline ending every file
|
7
|
+
[*]
|
8
|
+
end_of_line = lf
|
9
|
+
insert_final_newline = true
|
10
|
+
charset = utf-8
|
11
|
+
|
12
|
+
[*.{rb,yml,json,rake}]
|
13
|
+
indent_style = space
|
14
|
+
indent_size = 2
|
15
|
+
trim_trailing_whitespace = true
|
data/.rakeTasks
CHANGED
@@ -4,4 +4,4 @@ You are allowed to:
|
|
4
4
|
1. Remove rake task
|
5
5
|
2. Add existing rake tasks
|
6
6
|
To add existing rake tasks automatically delete this file and reload the project.
|
7
|
-
--><RakeGroup description="" fullCmd="" taksId="rake"><RakeTask description="Build nxt_registry-0.1.
|
7
|
+
--><RakeGroup description="" fullCmd="" taksId="rake"><RakeTask description="Build nxt_registry-0.1.4.gem into the pkg directory" fullCmd="build" taksId="build" /><RakeTask description="Remove any temporary products" fullCmd="clean" taksId="clean" /><RakeTask description="Remove any generated files" fullCmd="clobber" taksId="clobber" /><RakeTask description="Build and install nxt_registry-0.1.4.gem into system gems" fullCmd="install" taksId="install" /><RakeGroup description="" fullCmd="" taksId="install"><RakeTask description="Build and install nxt_registry-0.1.4.gem into system gems without network access" fullCmd="install:local" taksId="local" /></RakeGroup><RakeTask description="Create tag v0.1.4 and build and push nxt_registry-0.1.4.gem to https://rubygems.org" fullCmd="release[remote]" taksId="release[remote]" /><RakeTask description="Run RSpec code examples" fullCmd="spec" taksId="spec" /><RakeTask description="" fullCmd="default" taksId="default" /><RakeTask description="" fullCmd="release" taksId="release" /><RakeGroup description="" fullCmd="" taksId="release"><RakeTask description="" fullCmd="release:guard_clean" taksId="guard_clean" /><RakeTask description="" fullCmd="release:rubygem_push" taksId="rubygem_push" /><RakeTask description="" fullCmd="release:source_control_push" taksId="source_control_push" /></RakeGroup></RakeGroup></Settings>
|
data/CHANGELOG.md
ADDED
@@ -0,0 +1,10 @@
|
|
1
|
+
# v0.2.0 2020-07-24
|
2
|
+
|
3
|
+
### Added
|
4
|
+
|
5
|
+
- [internal] Added NxtRegistry::Singleton
|
6
|
+
- Added NxtRegistry::Singleton interface
|
7
|
+
- Make name optional and have object_id.to_s as default name
|
8
|
+
- Fix readme
|
9
|
+
|
10
|
+
[Compare v0.1.5...v0.2.0](https://github.com/nxt-insurance/nxt_registry/compare/v0.1.5...v0.2.0)
|
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
@@ -1,46 +1,48 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
nxt_registry (0.
|
4
|
+
nxt_registry (0.2.0)
|
5
5
|
activesupport
|
6
6
|
|
7
7
|
GEM
|
8
8
|
remote: https://rubygems.org/
|
9
9
|
specs:
|
10
|
-
activesupport (6.0.2
|
10
|
+
activesupport (6.0.3.2)
|
11
11
|
concurrent-ruby (~> 1.0, >= 1.0.2)
|
12
12
|
i18n (>= 0.7, < 2)
|
13
13
|
minitest (~> 5.1)
|
14
14
|
tzinfo (~> 1.1)
|
15
|
-
zeitwerk (~> 2.2)
|
16
|
-
coderay (1.1.
|
17
|
-
concurrent-ruby (1.1.
|
18
|
-
diff-lcs (1.
|
19
|
-
i18n (1.
|
15
|
+
zeitwerk (~> 2.2, >= 2.2.2)
|
16
|
+
coderay (1.1.3)
|
17
|
+
concurrent-ruby (1.1.6)
|
18
|
+
diff-lcs (1.4.4)
|
19
|
+
i18n (1.8.5)
|
20
20
|
concurrent-ruby (~> 1.0)
|
21
|
-
method_source (0.
|
22
|
-
minitest (5.
|
23
|
-
pry (0.
|
24
|
-
coderay (~> 1.1
|
25
|
-
method_source (~>
|
26
|
-
rake (
|
21
|
+
method_source (1.0.0)
|
22
|
+
minitest (5.14.1)
|
23
|
+
pry (0.13.1)
|
24
|
+
coderay (~> 1.1)
|
25
|
+
method_source (~> 1.0)
|
26
|
+
rake (12.3.3)
|
27
27
|
rspec (3.9.0)
|
28
28
|
rspec-core (~> 3.9.0)
|
29
29
|
rspec-expectations (~> 3.9.0)
|
30
30
|
rspec-mocks (~> 3.9.0)
|
31
|
-
rspec-core (3.9.
|
32
|
-
rspec-support (~> 3.9.
|
33
|
-
rspec-expectations (3.9.
|
31
|
+
rspec-core (3.9.2)
|
32
|
+
rspec-support (~> 3.9.3)
|
33
|
+
rspec-expectations (3.9.2)
|
34
34
|
diff-lcs (>= 1.2.0, < 2.0)
|
35
35
|
rspec-support (~> 3.9.0)
|
36
|
-
rspec-mocks (3.9.
|
36
|
+
rspec-mocks (3.9.1)
|
37
37
|
diff-lcs (>= 1.2.0, < 2.0)
|
38
38
|
rspec-support (~> 3.9.0)
|
39
|
-
rspec-support (3.9.
|
39
|
+
rspec-support (3.9.3)
|
40
|
+
rspec_junit_formatter (0.4.1)
|
41
|
+
rspec-core (>= 2, < 4, != 2.12.0)
|
40
42
|
thread_safe (0.3.6)
|
41
|
-
tzinfo (1.2.
|
43
|
+
tzinfo (1.2.7)
|
42
44
|
thread_safe (~> 0.1)
|
43
|
-
zeitwerk (2.
|
45
|
+
zeitwerk (2.4.0)
|
44
46
|
|
45
47
|
PLATFORMS
|
46
48
|
ruby
|
@@ -49,8 +51,9 @@ DEPENDENCIES
|
|
49
51
|
bundler (~> 2.0)
|
50
52
|
nxt_registry!
|
51
53
|
pry
|
52
|
-
rake (~>
|
54
|
+
rake (~> 12)
|
53
55
|
rspec (~> 3.0)
|
56
|
+
rspec_junit_formatter
|
54
57
|
|
55
58
|
BUNDLED WITH
|
56
|
-
2.
|
59
|
+
2.1.4
|
data/README.md
CHANGED
@@ -1,7 +1,9 @@
|
|
1
|
+
[![CircleCI](https://circleci.com/gh/nxt-insurance/nxt_registry.svg?style=svg)](https://circleci.com/gh/nxt-insurance/nxt_registry)
|
2
|
+
|
1
3
|
# NxtRegistry
|
2
4
|
|
3
|
-
NxtRegistry is a simple implementation of the container pattern. It allows you to register and resolve values
|
4
|
-
|
5
|
+
NxtRegistry is a simple implementation of the container pattern. It allows you to register and resolve values in nested
|
6
|
+
structures by allowing nesting registries into each other. In theory this can be indefinitely deep.
|
5
7
|
|
6
8
|
## Installation
|
7
9
|
|
@@ -21,47 +23,18 @@ Or install it yourself as:
|
|
21
23
|
|
22
24
|
## Usage
|
23
25
|
|
24
|
-
|
25
|
-
|
26
|
-
Use `attrs` to restrict which attributes can be registered on a specific level.
|
27
|
-
|
28
|
-
## Default values
|
29
|
-
|
30
|
-
Use `default` to register a default value that will be resolved in case an attribute was not registered.
|
31
|
-
|
32
|
-
## Block
|
33
|
-
|
34
|
-
When you register a block value that can be called, tt will automatically be called when you resolve the value.
|
35
|
-
If that's not what you want, you can configure your registry (on each level) not to call blocks directly by defining `call false`
|
36
|
-
|
37
|
-
## Memoize
|
38
|
-
|
39
|
-
Values are memoized per default. Switch it off with `default false`
|
40
|
-
|
41
|
-
## Resolver
|
42
|
-
|
43
|
-
You can register a resolver block if you want to lay hands on your values after they have been resolved.
|
44
|
-
|
45
|
-
## Transform keys
|
46
|
-
|
47
|
-
NxtRegistry uses a plain ruby hash to store values internally. Per default all keys used are transformed with `&:to_s`.
|
48
|
-
Thus you can use symbols or strings to register and resolve values. If it's not what you want, switch it off with `transform_keys false`
|
49
|
-
or define your own key transformer by assigning a block `transform_keys ->(key) { key.upcase }`
|
50
|
-
|
26
|
+
### Instance Level
|
51
27
|
|
52
28
|
```ruby
|
53
|
-
class
|
29
|
+
class Example
|
54
30
|
include NxtRegistry
|
55
31
|
|
56
32
|
def passengers
|
57
33
|
@passengers ||= begin
|
58
34
|
registry :from do
|
59
35
|
nested :to do
|
60
|
-
nested :via do
|
61
|
-
attrs
|
62
|
-
default -> { [] }
|
63
|
-
memoize true
|
64
|
-
call true
|
36
|
+
nested :via, memoize: true, call: true, default: -> { [] } do
|
37
|
+
attrs %i[train car plane horse] # restrict the attributes that can be registered
|
65
38
|
resolver ->(value) { value } # do something with your registered value here
|
66
39
|
transform_keys ->(key) { key.upcase } # transform keys
|
67
40
|
end
|
@@ -71,31 +44,178 @@ class MyClass
|
|
71
44
|
end
|
72
45
|
end
|
73
46
|
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
47
|
+
example = Example.new
|
48
|
+
# Based on the naming of the registry and its nesting you are provided with a simple interface
|
49
|
+
# that allows you to resolve and register values by name
|
50
|
+
|
51
|
+
# Register an array with a value by calling the accessor with a key, value pair
|
52
|
+
example.passengers.from(:a).to(:b).via(:train, ['Andy']) # => ['Andy']
|
53
|
+
|
54
|
+
# In case you try to register the same key again you will get an error
|
55
|
+
example.passengers.from(:a).to(:b).via(:train, ['Andy'])
|
56
|
+
# => NxtRegistry::Errors::KeyAlreadyRegisteredError
|
57
|
+
# (Key 'train' already registered in registry 'from.to.via')
|
58
|
+
# NxtRegistry::Errors::KeyAlreadyRegisteredError inherits from KeyError
|
59
|
+
|
60
|
+
# You can force values on the registry by using the bang method
|
61
|
+
example.passengers.from(:a).to(:b).via!(:train, ['Andreas'])
|
62
|
+
|
63
|
+
# Similarly you can try to resolve values softly
|
64
|
+
# (meaning no KeyNotRegisteredError will be raised when nothing was registered)
|
65
|
+
example.passengers.from(:a).to(:b).via!(:train)
|
66
|
+
# Since there is a default defined for this registry, it does not make any sense
|
67
|
+
# since there is always a value. But you get the point...
|
68
|
+
|
69
|
+
# Resolve values by calling the accessor with the key only
|
70
|
+
# In this case the default is returned because nothing was registered yet
|
71
|
+
example.passengers.from(:a).to(:b).via(:hyperloop) # []
|
72
|
+
|
73
|
+
# Appending values to a default array
|
74
|
+
example.passengers.from(:a).to(:b).via(:car) << 'Lütfi' # => ['Lütif']
|
75
|
+
example.passengers.from(:a).to(:b).via(:plane) += %w[Nils Rapha]
|
76
|
+
example.passengers.from(:a).to(:b).via(:plane) # => ['Nils', 'Rapha']
|
77
|
+
|
78
|
+
```
|
79
|
+
|
80
|
+
Alternatively you can create an instance of NxtRegistry::Registry.new('name', **options, &config)
|
81
|
+
|
82
|
+
```ruby
|
83
|
+
registry = NxtRegistry::Registry.new do
|
84
|
+
register(:andy, 'Andy')
|
85
|
+
register(:anthony, 'Anthony')
|
86
|
+
register(:aki, 'Aki')
|
87
|
+
end
|
88
|
+
|
89
|
+
registry.resolve(:aki) # => 'Aki'
|
90
|
+
|
91
|
+
```
|
92
|
+
|
93
|
+
### Class Level
|
81
94
|
|
82
|
-
|
95
|
+
```ruby
|
96
|
+
class SimpleExample
|
97
|
+
# By extending NxtRegistry::Singleton you get a super simple class level interface to an underlying instance of a :registry
|
98
|
+
extend NxtRegistry::Singleton
|
99
|
+
|
100
|
+
registry do
|
101
|
+
# procs are called directly if not defined otherwise
|
102
|
+
register(KeyError, ->(error) { puts 'KeyError handler' } )
|
103
|
+
register(ArgumentError, ->(error) { puts 'ArgumentError handler' } )
|
104
|
+
# Custom key error handlers
|
105
|
+
on_key_already_registered ->(key) { raise "Key was already registered dude: #{key}" }
|
106
|
+
on_key_not_registered ->(key) { raise "Key was never registered dude: #{key}" }
|
107
|
+
end
|
108
|
+
end
|
109
|
+
|
110
|
+
SimpleExample.resolve(KeyError)
|
111
|
+
# Alternatively: SimpleExample.registry.resolve(KeyError)
|
112
|
+
# Or: SimpleExample.instance.resolve(KeyError)
|
113
|
+
# KeyError handler
|
114
|
+
# => nil
|
83
115
|
|
116
|
+
```
|
84
117
|
|
85
|
-
|
118
|
+
```ruby
|
119
|
+
class OtherExample
|
86
120
|
extend NxtRegistry
|
87
121
|
|
88
|
-
|
122
|
+
# By passing a block to :registry you can directly register your values inline
|
123
|
+
REGISTRY = registry(:errors) do
|
124
|
+
# procs are called directly if not defined otherwise
|
89
125
|
register(KeyError, ->(error) { puts 'KeyError handler' } )
|
126
|
+
register(ArgumentError, ->(error) { puts 'ArgumentError handler' } )
|
90
127
|
end
|
91
128
|
end
|
92
129
|
|
93
|
-
|
130
|
+
# Instead of using the name of the registry, you can also always call register and resolve on the
|
131
|
+
# level where you want to register or resolve values. Equivalently to the named interface you can
|
132
|
+
# use register! and resolve! to softly resolve or forcfully register values.
|
133
|
+
OtherExample::REGISTRY.resolve(KeyError)
|
94
134
|
# KeyError handler
|
95
135
|
# => nil
|
96
136
|
|
97
137
|
```
|
98
138
|
|
139
|
+
### Restrict attributes to a certain set
|
140
|
+
|
141
|
+
Use `attrs` to restrict which attributes can be registered on a specific level.
|
142
|
+
|
143
|
+
```ruby
|
144
|
+
registry :example, attrs: %w[one two three]
|
145
|
+
```
|
146
|
+
|
147
|
+
### Default values
|
148
|
+
|
149
|
+
Use `default` to register a default value that will be resolved in case an attribute was not registered.
|
150
|
+
|
151
|
+
```ruby
|
152
|
+
registry :example, default: ->(value) { 'default' }
|
153
|
+
```
|
154
|
+
|
155
|
+
### Blocks
|
156
|
+
|
157
|
+
When you register a block value that can be called, it will automatically be called when you resolve the value.
|
158
|
+
If that's not what you want, you can configure your registry (on each level) not to call blocks directly by defining `call false`
|
159
|
+
|
160
|
+
```ruby
|
161
|
+
registry :example, call: false do
|
162
|
+
register(:one, ->(value) { 'Not called when resolved' } )
|
163
|
+
end
|
164
|
+
```
|
165
|
+
|
166
|
+
### Memoize
|
167
|
+
|
168
|
+
Values are memoized per default. Switch it off with `memoize: false`
|
169
|
+
|
170
|
+
```ruby
|
171
|
+
registry :example, memoize: false do
|
172
|
+
register(:one, -> { Time.current } )
|
173
|
+
end
|
174
|
+
|
175
|
+
registry.resolve(:one)
|
176
|
+
# => 2020-01-02 23:56:15 +0100
|
177
|
+
registry.resolve(:one)
|
178
|
+
# => 2020-01-02 23:56:17 +0100
|
179
|
+
registry.resolve(:one)
|
180
|
+
# => 2020-01-02 23:56:18 +0100
|
181
|
+
```
|
182
|
+
|
183
|
+
### Resolver
|
184
|
+
|
185
|
+
You can register a resolver block if you want to lay hands on your values after they have been resolved.
|
186
|
+
A resolver can be anything that implements `:call` to which the value is passed.
|
187
|
+
|
188
|
+
```ruby
|
189
|
+
registry :example do
|
190
|
+
resolver ->(value) { value * 2 }
|
191
|
+
register(:one, 1)
|
192
|
+
end
|
193
|
+
|
194
|
+
registry.resolve(:one)
|
195
|
+
# => 2
|
196
|
+
```
|
197
|
+
|
198
|
+
### Transform keys
|
199
|
+
|
200
|
+
NxtRegistry uses a plain ruby hash to store values internally. Per default all keys used are transformed with `&:to_s`.
|
201
|
+
Thus you can use symbols or strings to register and resolve values. If it's not what you want, switch it off with `transform_keys false`
|
202
|
+
or define your own key transformer by assigning a block to transform_keys: `transform_keys ->(key) { key.upcase }`
|
203
|
+
|
204
|
+
```ruby
|
205
|
+
registry :example do
|
206
|
+
transform_keys ->(key) { key.to_s.downcase }
|
207
|
+
register(:bombshell, 'hanna')
|
208
|
+
end
|
209
|
+
|
210
|
+
registry.resolve('BOMBSHELL')
|
211
|
+
# => 'hanna'
|
212
|
+
```
|
213
|
+
|
214
|
+
### Customize registry errors
|
215
|
+
|
216
|
+
You can also customize what kind of errors are being raised in case a of a key was not registered or was already registered.
|
217
|
+
by providing blocks or a handler responding to :call for `on_key_already_registered` and `on_key_already_registered`
|
218
|
+
|
99
219
|
## Development
|
100
220
|
|
101
221
|
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
data/lib/nxt_registry.rb
CHANGED
@@ -6,13 +6,14 @@ require "nxt_registry/errors"
|
|
6
6
|
require "nxt_registry/nested_registry_builder"
|
7
7
|
require "nxt_registry/registry"
|
8
8
|
require "nxt_registry/recursive_registry"
|
9
|
+
require "nxt_registry/singleton"
|
9
10
|
|
10
11
|
module NxtRegistry
|
11
|
-
def registry(name, &config)
|
12
|
-
Registry.new(name, &config)
|
12
|
+
def registry(name, **options, &config)
|
13
|
+
Registry.new(name, **options, &config)
|
13
14
|
end
|
14
15
|
|
15
|
-
def recursive_registry(name, &config)
|
16
|
-
RecursiveRegistry.new(name, &config)
|
16
|
+
def recursive_registry(name, **options, &config)
|
17
|
+
RecursiveRegistry.new(name, **options, &config)
|
17
18
|
end
|
18
19
|
end
|
@@ -1,11 +1,12 @@
|
|
1
1
|
module NxtRegistry
|
2
2
|
class Registry
|
3
|
-
def initialize(name, **options, &config)
|
3
|
+
def initialize(name = object_id.to_s, **options, &config)
|
4
4
|
@name = name
|
5
5
|
@parent = options[:parent]
|
6
6
|
@is_leaf = true
|
7
|
-
@namespace =
|
7
|
+
@namespace = parent ? name.to_s.prepend("#{parent.send(:namespace)}.") : name.to_s
|
8
8
|
@config = config
|
9
|
+
@options = options
|
9
10
|
@store = {}
|
10
11
|
@attrs = nil
|
11
12
|
|
@@ -16,6 +17,7 @@ module NxtRegistry
|
|
16
17
|
attr_reader :name
|
17
18
|
|
18
19
|
def nested(name, **options, &config)
|
20
|
+
# TODO: Ensure that nesting is included in defined attrs
|
19
21
|
options = options.merge(parent: self)
|
20
22
|
|
21
23
|
if default.is_a?(Blank)
|
@@ -100,6 +102,7 @@ module NxtRegistry
|
|
100
102
|
def configure(&block)
|
101
103
|
define_accessors
|
102
104
|
define_interface
|
105
|
+
attrs(*Array(options.fetch(:attrs, [])))
|
103
106
|
|
104
107
|
if block.present?
|
105
108
|
if block.arity == 1
|
@@ -110,9 +113,15 @@ module NxtRegistry
|
|
110
113
|
end
|
111
114
|
end
|
112
115
|
|
116
|
+
def to_s
|
117
|
+
"Registry[#{name}] -> #{store.to_s}"
|
118
|
+
end
|
119
|
+
|
120
|
+
alias_method :inspect, :to_s
|
121
|
+
|
113
122
|
private
|
114
123
|
|
115
|
-
attr_reader :namespace, :parent, :config, :store
|
124
|
+
attr_reader :namespace, :parent, :config, :store, :options
|
116
125
|
attr_accessor :is_leaf
|
117
126
|
|
118
127
|
def is_leaf?
|
@@ -124,8 +133,7 @@ module NxtRegistry
|
|
124
133
|
|
125
134
|
raise ArgumentError, "Not allowed to register values in a registry that contains nested registries" unless is_leaf
|
126
135
|
raise KeyError, "Keys are restricted to #{attrs.keys}" if attribute_not_allowed?(key)
|
127
|
-
|
128
|
-
on_key_already_registered.call(key) if store[key] && raise
|
136
|
+
on_key_already_registered && on_key_already_registered.call(key) if store[key] && raise
|
129
137
|
|
130
138
|
store[key] = value
|
131
139
|
end
|
@@ -140,7 +148,7 @@ module NxtRegistry
|
|
140
148
|
if default.is_a?(Blank)
|
141
149
|
return unless raise
|
142
150
|
|
143
|
-
on_key_not_registered.call(key)
|
151
|
+
on_key_not_registered && on_key_not_registered.call(key)
|
144
152
|
else
|
145
153
|
value = resolve_default
|
146
154
|
return value unless memoize
|
@@ -198,6 +206,7 @@ module NxtRegistry
|
|
198
206
|
@call = options.fetch(:call) { true }
|
199
207
|
@resolver = options.fetch(:resolver, false)
|
200
208
|
@transform_keys = options.fetch(:transform_keys) { ->(key) { key.to_s } }
|
209
|
+
|
201
210
|
@on_key_already_registered = options.fetch(:on_key_already_registered) { ->(key) { raise_key_already_registered_error(key) } }
|
202
211
|
@on_key_not_registered = options.fetch(:on_key_not_registered) { ->(key) { raise_key_not_registered_error(key) } }
|
203
212
|
end
|
@@ -253,5 +262,11 @@ module NxtRegistry
|
|
253
262
|
end
|
254
263
|
end
|
255
264
|
end
|
265
|
+
|
266
|
+
def initialize_copy(original)
|
267
|
+
super
|
268
|
+
@store = original.send(:store).deep_dup
|
269
|
+
@options = original.send(:options).deep_dup
|
270
|
+
end
|
256
271
|
end
|
257
272
|
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module NxtRegistry
|
2
|
+
module Singleton
|
3
|
+
def self.extended(subclass)
|
4
|
+
subclass.singleton_class.class_eval do
|
5
|
+
default_name = (subclass.name || 'registry')
|
6
|
+
|
7
|
+
define_method :registry do |name = default_name, **options, &block|
|
8
|
+
@registry ||= NxtRegistry::Registry.new(name, **options, &block)
|
9
|
+
end
|
10
|
+
|
11
|
+
delegate_missing_to :registry
|
12
|
+
|
13
|
+
define_method(:instance) { registry }
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
data/lib/nxt_registry/version.rb
CHANGED
data/nxt_registry.gemspec
CHANGED
@@ -35,7 +35,7 @@ Gem::Specification.new do |spec|
|
|
35
35
|
spec.add_dependency "activesupport"
|
36
36
|
|
37
37
|
spec.add_development_dependency "bundler", "~> 2.0"
|
38
|
-
spec.add_development_dependency "rake", "~>
|
38
|
+
spec.add_development_dependency "rake", "~> 12"
|
39
39
|
spec.add_development_dependency "rspec", "~> 3.0"
|
40
40
|
spec.add_development_dependency "pry"
|
41
41
|
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.
|
4
|
+
version: 0.2.0
|
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-07-27 00:00:00.000000000 Z
|
15
15
|
dependencies:
|
16
16
|
- !ruby/object:Gem::Dependency
|
17
17
|
name: activesupport
|
@@ -47,14 +47,14 @@ dependencies:
|
|
47
47
|
requirements:
|
48
48
|
- - "~>"
|
49
49
|
- !ruby/object:Gem::Version
|
50
|
-
version: '
|
50
|
+
version: '12'
|
51
51
|
type: :development
|
52
52
|
prerelease: false
|
53
53
|
version_requirements: !ruby/object:Gem::Requirement
|
54
54
|
requirements:
|
55
55
|
- - "~>"
|
56
56
|
- !ruby/object:Gem::Version
|
57
|
-
version: '
|
57
|
+
version: '12'
|
58
58
|
- !ruby/object:Gem::Dependency
|
59
59
|
name: rspec
|
60
60
|
requirement: !ruby/object:Gem::Requirement
|
@@ -90,10 +90,13 @@ executables: []
|
|
90
90
|
extensions: []
|
91
91
|
extra_rdoc_files: []
|
92
92
|
files:
|
93
|
+
- ".circleci/config.yml"
|
94
|
+
- ".editorconfig"
|
93
95
|
- ".gitignore"
|
94
96
|
- ".rakeTasks"
|
95
97
|
- ".rspec"
|
96
98
|
- ".travis.yml"
|
99
|
+
- CHANGELOG.md
|
97
100
|
- Gemfile
|
98
101
|
- Gemfile.lock
|
99
102
|
- LICENSE.txt
|
@@ -108,6 +111,7 @@ files:
|
|
108
111
|
- lib/nxt_registry/nested_registry_builder.rb
|
109
112
|
- lib/nxt_registry/recursive_registry.rb
|
110
113
|
- lib/nxt_registry/registry.rb
|
114
|
+
- lib/nxt_registry/singleton.rb
|
111
115
|
- lib/nxt_registry/version.rb
|
112
116
|
- nxt_registry.gemspec
|
113
117
|
homepage: https://github.com/nxt-insurance
|
@@ -132,7 +136,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
132
136
|
- !ruby/object:Gem::Version
|
133
137
|
version: '0'
|
134
138
|
requirements: []
|
135
|
-
rubygems_version: 3.0.
|
139
|
+
rubygems_version: 3.0.3
|
136
140
|
signing_key:
|
137
141
|
specification_version: 4
|
138
142
|
summary: nxt_registry is a simple implementation of the container pattern
|