dry-container 0.7.1 → 0.7.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +17 -0
- data/lib/dry/container/item.rb +21 -0
- data/lib/dry/container/item/callable.rb +0 -7
- data/lib/dry/container/item/memoizable.rb +1 -1
- data/lib/dry/container/mixin.rb +19 -14
- data/lib/dry/container/resolver.rb +10 -2
- data/lib/dry/container/version.rb +1 -1
- data/spec/support/shared_examples/container.rb +32 -6
- metadata +18 -17
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ac1a656d67e37d72da3265e2f092e507460cba4615db8f3727ee1acbca41584c
|
4
|
+
data.tar.gz: be191bd45d43147d12c858d8683463be5e59a66ef2f473e8c19d7010bda0794c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c95deccc6ad3f976d31949d1a39e1bc4d6f5797604045aed3f7254d3a1e88ca35c76ee0483ad14bbc6abe0aed8fb13d157cd793c6eb1ff417343a0037a56358f
|
7
|
+
data.tar.gz: a6bac7cb3f1819b6bb89a2989de1fe0c3d9a182144a556394cec1f8a1d48bf848589c57ffc7b4daaac106cb0a5673b086f901f2b1deba81c64796a6a1038c6c0
|
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,20 @@
|
|
1
|
+
## v0.7.2 - 2019-07-09
|
2
|
+
|
3
|
+
## Added
|
4
|
+
|
5
|
+
* `.resolve` accepts an optional fallback block, similar to how `Hash#fetch` works ([flash-gordon](https://github.com/flash-gordon))
|
6
|
+
```ruby
|
7
|
+
container.resolve('missing_key') { :fallback } # => :fallback
|
8
|
+
```
|
9
|
+
* `.decorate` can (again) work with static values. Also, it can take a block instead of `with` ([flash-gordon](https://github.com/flash-gordon))
|
10
|
+
```ruby
|
11
|
+
container.register('key', 'value')
|
12
|
+
container.decorate('key') { |v| "<'#{v}'>" }
|
13
|
+
container.resolve('key') # => "<'value'>"
|
14
|
+
```
|
15
|
+
|
16
|
+
[Compare v0.7.1...0.7.2](https://github.com/dry-rb/dry-container/compare/v0.7.1...v0.7.2)
|
17
|
+
|
1
18
|
## v0.7.1 - 2019-06-07
|
2
19
|
|
3
20
|
## Fixed
|
data/lib/dry/container/item.rb
CHANGED
@@ -23,6 +23,27 @@ module Dry
|
|
23
23
|
def call
|
24
24
|
raise NotImplementedError
|
25
25
|
end
|
26
|
+
|
27
|
+
# @private
|
28
|
+
def value?
|
29
|
+
!callable?
|
30
|
+
end
|
31
|
+
|
32
|
+
# @private
|
33
|
+
def callable?
|
34
|
+
options[:call]
|
35
|
+
end
|
36
|
+
|
37
|
+
# Build a new item with transformation applied
|
38
|
+
#
|
39
|
+
# @private
|
40
|
+
def map(func)
|
41
|
+
if callable?
|
42
|
+
self.class.new(-> { func.(item.call) }, options)
|
43
|
+
else
|
44
|
+
self.class.new(func.(item), options)
|
45
|
+
end
|
46
|
+
end
|
26
47
|
end
|
27
48
|
end
|
28
49
|
end
|
data/lib/dry/container/mixin.rb
CHANGED
@@ -6,6 +6,9 @@ module Dry
|
|
6
6
|
PREFIX_NAMESPACE = lambda do |namespace, key, config|
|
7
7
|
[namespace, key].join(config.namespace_separator)
|
8
8
|
end
|
9
|
+
|
10
|
+
EMPTY_HASH = {}.freeze
|
11
|
+
|
9
12
|
# Mixin to expose Inversion of Control (IoC) container behaviour
|
10
13
|
#
|
11
14
|
# @example
|
@@ -90,7 +93,7 @@ module Dry
|
|
90
93
|
# @return [Dry::Container::Mixin] self
|
91
94
|
#
|
92
95
|
# @api public
|
93
|
-
def register(key, contents = nil, options =
|
96
|
+
def register(key, contents = nil, options = EMPTY_HASH, &block)
|
94
97
|
if block_given?
|
95
98
|
item = block
|
96
99
|
options = contents if contents.is_a?(::Hash)
|
@@ -107,12 +110,15 @@ module Dry
|
|
107
110
|
#
|
108
111
|
# @param [Mixed] key
|
109
112
|
# The key for the item you wish to resolve
|
113
|
+
# @yield
|
114
|
+
# Fallback block to call when a key is missing. Its result will be returned
|
115
|
+
# @yieldparam [Mixed] key Missing key
|
110
116
|
#
|
111
117
|
# @return [Mixed]
|
112
118
|
#
|
113
119
|
# @api public
|
114
|
-
def resolve(key)
|
115
|
-
config.resolver.call(_container, key)
|
120
|
+
def resolve(key, &block)
|
121
|
+
config.resolver.call(_container, key, &block)
|
116
122
|
end
|
117
123
|
|
118
124
|
# Resolve an item from the container
|
@@ -205,23 +211,22 @@ module Dry
|
|
205
211
|
# @return [Dry::Container::Mixin] self
|
206
212
|
#
|
207
213
|
# @api public
|
208
|
-
def decorate(key, with:)
|
209
|
-
|
214
|
+
def decorate(key, with: nil, &block)
|
215
|
+
key = key.to_s
|
216
|
+
original = _container.delete(key) do
|
210
217
|
raise Error, "Nothing registered with the key #{key.inspect}"
|
211
218
|
end
|
212
219
|
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
decorated = -> { decorator.new(original.call) }
|
218
|
-
elsif decorator.respond_to?(:call)
|
219
|
-
decorated = -> { decorator.call(original.call) }
|
220
|
+
if with.is_a?(Class)
|
221
|
+
decorator = with.method(:new)
|
222
|
+
elsif block.nil? && !with.respond_to?(:call)
|
223
|
+
raise Error, "Decorator needs to be a Class, block, or respond to the `call` method"
|
220
224
|
else
|
221
|
-
|
225
|
+
decorator = with || block
|
222
226
|
end
|
223
227
|
|
224
|
-
|
228
|
+
_container[key] = original.map(decorator)
|
229
|
+
self
|
225
230
|
end
|
226
231
|
|
227
232
|
# Evaluate block and register items in namespace
|
@@ -10,16 +10,24 @@ module Dry
|
|
10
10
|
# The container
|
11
11
|
# @param [Mixed] key
|
12
12
|
# The key for the item you wish to resolve
|
13
|
+
# @yield
|
14
|
+
# Fallback block to call when a key is missing. Its result will be returned
|
15
|
+
# @yieldparam [Mixed] key Missing key
|
13
16
|
#
|
14
17
|
# @raise [Dry::Conainer::Error]
|
15
|
-
# If the given key is not registered with the container
|
18
|
+
# If the given key is not registered with the container (and no block provided)
|
19
|
+
#
|
16
20
|
#
|
17
21
|
# @return [Mixed]
|
18
22
|
#
|
19
23
|
# @api public
|
20
24
|
def call(container, key)
|
21
25
|
item = container.fetch(key.to_s) do
|
22
|
-
|
26
|
+
if block_given?
|
27
|
+
return yield(key)
|
28
|
+
else
|
29
|
+
raise Error, "Nothing registered with the key #{key.inspect}"
|
30
|
+
end
|
23
31
|
end
|
24
32
|
|
25
33
|
item.call
|
@@ -426,14 +426,34 @@ RSpec.shared_examples 'a container' do
|
|
426
426
|
end
|
427
427
|
|
428
428
|
context 'for not callable item' do
|
429
|
-
|
430
|
-
|
431
|
-
|
429
|
+
describe 'wrapping' do
|
430
|
+
before do
|
431
|
+
container.register(key, call: false) { "value" }
|
432
|
+
container.decorate(key, with: SimpleDelegator)
|
433
|
+
end
|
434
|
+
|
435
|
+
it 'expected to be an instance of SimpleDelegator' do
|
436
|
+
expect(container.resolve(key)).to be_instance_of(SimpleDelegator)
|
437
|
+
expect(container.resolve(key).__getobj__.call).to eql("value")
|
438
|
+
end
|
432
439
|
end
|
433
440
|
|
434
|
-
|
435
|
-
|
436
|
-
|
441
|
+
describe 'memoization' do
|
442
|
+
before do
|
443
|
+
@called = 0
|
444
|
+
container.register(key, 'value')
|
445
|
+
|
446
|
+
container.decorate(key) do |value|
|
447
|
+
@called += 1
|
448
|
+
"<#{value}>"
|
449
|
+
end
|
450
|
+
end
|
451
|
+
|
452
|
+
it 'decorates static value only once' do
|
453
|
+
expect(container.resolve(key)).to eql('<value>')
|
454
|
+
expect(container.resolve(key)).to eql('<value>')
|
455
|
+
expect(@called).to be(1)
|
456
|
+
end
|
437
457
|
end
|
438
458
|
end
|
439
459
|
|
@@ -635,4 +655,10 @@ RSpec.shared_examples 'a container' do
|
|
635
655
|
expect(container.clone._container).to be(container._container)
|
636
656
|
end
|
637
657
|
end
|
658
|
+
|
659
|
+
describe '.resolve' do
|
660
|
+
it 'accepts a fallback block' do
|
661
|
+
expect(container.resolve('missing') { :fallback }).to be(:fallback)
|
662
|
+
end
|
663
|
+
end
|
638
664
|
end
|
metadata
CHANGED
@@ -1,31 +1,30 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: dry-container
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.7.
|
4
|
+
version: 0.7.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Andy Holland
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-
|
11
|
+
date: 2019-07-09 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
|
-
name: concurrent-ruby
|
15
14
|
requirement: !ruby/object:Gem::Requirement
|
16
15
|
requirements:
|
17
16
|
- - "~>"
|
18
17
|
- !ruby/object:Gem::Version
|
19
18
|
version: '1.0'
|
20
|
-
|
19
|
+
name: concurrent-ruby
|
21
20
|
prerelease: false
|
21
|
+
type: :runtime
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '1.0'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
|
-
name: dry-configurable
|
29
28
|
requirement: !ruby/object:Gem::Requirement
|
30
29
|
requirements:
|
31
30
|
- - "~>"
|
@@ -34,8 +33,9 @@ dependencies:
|
|
34
33
|
- - ">="
|
35
34
|
- !ruby/object:Gem::Version
|
36
35
|
version: 0.1.3
|
37
|
-
|
36
|
+
name: dry-configurable
|
38
37
|
prerelease: false
|
38
|
+
type: :runtime
|
39
39
|
version_requirements: !ruby/object:Gem::Requirement
|
40
40
|
requirements:
|
41
41
|
- - "~>"
|
@@ -45,48 +45,48 @@ dependencies:
|
|
45
45
|
- !ruby/object:Gem::Version
|
46
46
|
version: 0.1.3
|
47
47
|
- !ruby/object:Gem::Dependency
|
48
|
-
name: bundler
|
49
48
|
requirement: !ruby/object:Gem::Requirement
|
50
49
|
requirements:
|
51
50
|
- - ">="
|
52
51
|
- !ruby/object:Gem::Version
|
53
52
|
version: '0'
|
54
|
-
|
53
|
+
name: bundler
|
55
54
|
prerelease: false
|
55
|
+
type: :development
|
56
56
|
version_requirements: !ruby/object:Gem::Requirement
|
57
57
|
requirements:
|
58
58
|
- - ">="
|
59
59
|
- !ruby/object:Gem::Version
|
60
60
|
version: '0'
|
61
61
|
- !ruby/object:Gem::Dependency
|
62
|
-
name: rake
|
63
62
|
requirement: !ruby/object:Gem::Requirement
|
64
63
|
requirements:
|
65
64
|
- - ">="
|
66
65
|
- !ruby/object:Gem::Version
|
67
66
|
version: '0'
|
68
|
-
|
67
|
+
name: rake
|
69
68
|
prerelease: false
|
69
|
+
type: :development
|
70
70
|
version_requirements: !ruby/object:Gem::Requirement
|
71
71
|
requirements:
|
72
72
|
- - ">="
|
73
73
|
- !ruby/object:Gem::Version
|
74
74
|
version: '0'
|
75
75
|
- !ruby/object:Gem::Dependency
|
76
|
-
name: rspec
|
77
76
|
requirement: !ruby/object:Gem::Requirement
|
78
77
|
requirements:
|
79
78
|
- - ">="
|
80
79
|
- !ruby/object:Gem::Version
|
81
80
|
version: '0'
|
82
|
-
|
81
|
+
name: rspec
|
83
82
|
prerelease: false
|
83
|
+
type: :development
|
84
84
|
version_requirements: !ruby/object:Gem::Requirement
|
85
85
|
requirements:
|
86
86
|
- - ">="
|
87
87
|
- !ruby/object:Gem::Version
|
88
88
|
version: '0'
|
89
|
-
description:
|
89
|
+
description:
|
90
90
|
email:
|
91
91
|
- andyholland1991@aol.com
|
92
92
|
executables: []
|
@@ -129,7 +129,7 @@ homepage: https://github.com/dry-rb/dry-container
|
|
129
129
|
licenses:
|
130
130
|
- MIT
|
131
131
|
metadata: {}
|
132
|
-
post_install_message:
|
132
|
+
post_install_message:
|
133
133
|
rdoc_options: []
|
134
134
|
require_paths:
|
135
135
|
- lib
|
@@ -144,8 +144,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
144
144
|
- !ruby/object:Gem::Version
|
145
145
|
version: '0'
|
146
146
|
requirements: []
|
147
|
-
|
148
|
-
|
147
|
+
rubyforge_project:
|
148
|
+
rubygems_version: 2.7.9
|
149
|
+
signing_key:
|
149
150
|
specification_version: 4
|
150
151
|
summary: A simple container intended for use as an IoC container
|
151
152
|
test_files:
|