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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 89386cb5792215d73c4b754921c1f5df7111f7d76a87f4fe7ad2777fd8c71abf
4
- data.tar.gz: 24008af254d6531e665d49292f20c7ae7b483d67f97838b5c460df1d203f9987
3
+ metadata.gz: ac1a656d67e37d72da3265e2f092e507460cba4615db8f3727ee1acbca41584c
4
+ data.tar.gz: be191bd45d43147d12c858d8683463be5e59a66ef2f473e8c19d7010bda0794c
5
5
  SHA512:
6
- metadata.gz: 89350f42cdabd64616241d100c760c385f53bc0a9ba3baf662b575b34beaed06ca33d4fce40f8497a72505b0fda28ee9cc9874dbed3ee91df02363517d0a8f13
7
- data.tar.gz: af233252c5c04f68650c89ed65079028bc4a7a8636134c132379c60af13d1b2719fd77784931204864b3ce9e43b49bdc905034f84e59d90f7d885afc69638919
6
+ metadata.gz: c95deccc6ad3f976d31949d1a39e1bc4d6f5797604045aed3f7254d3a1e88ca35c76ee0483ad14bbc6abe0aed8fb13d157cd793c6eb1ff417343a0037a56358f
7
+ data.tar.gz: a6bac7cb3f1819b6bb89a2989de1fe0c3d9a182144a556394cec1f8a1d48bf848589c57ffc7b4daaac106cb0a5673b086f901f2b1deba81c64796a6a1038c6c0
@@ -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
@@ -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
@@ -14,13 +14,6 @@ module Dry
14
14
  def call
15
15
  callable? ? item.call : item
16
16
  end
17
-
18
- private
19
-
20
- # @private
21
- def callable?
22
- options[:call]
23
- end
24
17
  end
25
18
  end
26
19
  end
@@ -21,7 +21,7 @@ module Dry
21
21
  # @return [Dry::Container::Item::Base]
22
22
  def initialize(item, options = {})
23
23
  super
24
- raise_not_supported_error unless item.is_a?(::Proc)
24
+ raise_not_supported_error unless callable?
25
25
 
26
26
  @memoize_mutex = ::Mutex.new
27
27
  end
@@ -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 = {}, &block)
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
- original = _container.delete(key.to_s) do
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
- decorator = with
214
- memoize = original.is_a?(Item::Memoizable)
215
-
216
- if decorator.is_a?(Class)
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
- raise Error, "Decorator needs to be a Class or responds to the `call` method"
225
+ decorator = with || block
222
226
  end
223
227
 
224
- register(key, memoize: memoize, &decorated)
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
- raise Error, "Nothing registered with the key #{key.inspect}"
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
@@ -1,6 +1,6 @@
1
1
  module Dry
2
2
  class Container
3
3
  # @api public
4
- VERSION = '0.7.1'.freeze
4
+ VERSION = '0.7.2'.freeze
5
5
  end
6
6
  end
@@ -426,14 +426,34 @@ RSpec.shared_examples 'a container' do
426
426
  end
427
427
 
428
428
  context 'for not callable item' do
429
- before do
430
- container.register(key, call: false) { "value" }
431
- container.decorate(key, with: SimpleDelegator)
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
- it 'expected to be an instance of SimpleDelegator' do
435
- expect(container.resolve(key)).to be_instance_of(SimpleDelegator)
436
- expect(container.resolve(key).__getobj__.call).to eql("value")
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.1
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-06-07 00:00:00.000000000 Z
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
- type: :runtime
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
- type: :runtime
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
- type: :development
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
- type: :development
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
- type: :development
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
- rubygems_version: 3.0.3
148
- signing_key:
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: