dry-initializer 0.3.0 → 0.3.1

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
  SHA1:
3
- metadata.gz: ae0e61fb5894f53da09eea595818f5c6d9dc5482
4
- data.tar.gz: d4b4eb054a1a5ccaf9d8211aed807c841fe3b0a0
3
+ metadata.gz: 0c2d0bd0d5582696fdc1b1cd2190f8114220f142
4
+ data.tar.gz: 949cbe9873d11f23b1289f61de684b92de4cb23d
5
5
  SHA512:
6
- metadata.gz: ab37761b6ef475ba335e92b2751db1276d68b67a43d2e6f69a9e6e98f5e0ba2ffeb916739d2aab863327b8ab3fc64c30569144ceba881c90108628e4e50eb065
7
- data.tar.gz: 866ec3b26e494e5f40066e0dd8f98a926f7cd696fdda71837ff0d06f099ab83252cca2e68e175f7632fe28c34902705ea4396246a9860ad731da62742002d648
6
+ metadata.gz: 31f337c8c1e6d71f05ad3145c6bedf6ed92a532261ca3e26fbbd7449d5e303e2dc452342fd210632eb532813a6cc0148dab4bedb4c88dff2d6319f03cdc6658f
7
+ data.tar.gz: f86d25c3b3004aa3a0792cf5fc6ea18a6e3d3baf855cb0636045de6079c882b77a2b9f3028037c902b967ea0cb687559f3c14b41176150bd48940a5abe40371b
data/CHANGELOG.md CHANGED
@@ -1,3 +1,11 @@
1
+ ## v0.3.1 2016-05-22
2
+
3
+ ### Added
4
+
5
+ * Support for tolerance to unknown options (@nepalez)
6
+
7
+ [Compare v0.3.0...v0.3.1](https://github.com/dry-rb/dry-initializer/compare/v0.3.0..v0.3.1)
8
+
1
9
  ## v0.3.0 2016-05-19
2
10
 
3
11
  Breaks interface for adding new plugins. Register new plugin via:
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |gem|
2
2
  gem.name = "dry-initializer"
3
- gem.version = "0.3.0"
3
+ gem.version = "0.3.1"
4
4
  gem.author = ["Vladimir Kochnev (marshall-lee)", "Andrew Kozin (nepalez)"]
5
5
  gem.email = ["hashtable@yandex.ru", "andrew.kozin@gmail.com"]
6
6
  gem.homepage = "https://github.com/dryrb/dry-initializer"
@@ -17,27 +17,43 @@ module Dry::Initializer
17
17
  #
18
18
  # @param [Dry::Initializer::Plugin]
19
19
  #
20
+ # @return [Dry::Initializer::Builder]
21
+ #
20
22
  def register(plugin)
21
23
  plugins = @plugins + [plugin]
22
24
  copy { @plugins = plugins }
23
25
  end
24
26
 
27
+ # Makes builder to provide options-tolerant initializer
28
+ #
29
+ # @return [Dry::Initializer::Builder]
30
+ #
31
+ def tolerant_to_unknown_options
32
+ copy { @tolerant = "**" }
33
+ end
34
+
35
+ # Makes builder to provide options-intolerant initializer
36
+ #
37
+ # @return [Dry::Initializer::Builder]
38
+ #
39
+ def intolerant_to_unknown_options
40
+ copy { @tolerant = nil }
41
+ end
42
+
25
43
  # Defines new agrument and reloads mixin definitions
26
44
  #
27
- # @param (see #call)
28
45
  # @param [#to_sym] name
29
46
  # @param [Hash<Symbol, Object>] settings
30
47
  #
31
- # @return [self] itself
48
+ # @return [Dry::Initializer::Builder]
32
49
  #
33
- def define(mixin, name, settings)
50
+ def define(name, settings)
34
51
  signature = @signature.add(name, settings)
35
52
  parts = @parts + @plugins.map { |p| p.call(name, settings) }.compact
36
53
 
37
54
  copy do
38
55
  @signature = signature
39
56
  @parts = parts
40
- call(mixin)
41
57
  end
42
58
  end
43
59
 
@@ -49,6 +65,7 @@ module Dry::Initializer
49
65
  define_readers(mixin)
50
66
  reload_initializer(mixin)
51
67
  reload_callback(mixin)
68
+ mixin
52
69
  end
53
70
 
54
71
  private
@@ -65,10 +82,12 @@ module Dry::Initializer
65
82
  end
66
83
 
67
84
  def reload_initializer(mixin)
68
- strings = @parts.select { |part| String === part }
69
-
85
+ strings = @parts.select { |part| String === part }
86
+ signature = [@signature.call, @tolerant].map(&:to_s)
87
+ .reject(&:empty?)
88
+ .join(", ")
70
89
  mixin.class_eval <<-RUBY
71
- def initialize(#{@signature.call})
90
+ def initialize(#{signature})
72
91
  #{strings.join("\n")}
73
92
  __after_initialize__
74
93
  end
@@ -12,9 +12,9 @@ module Dry::Initializer
12
12
  # @return [self] itself
13
13
  #
14
14
  def param(name, **options)
15
- @initializer_builder = initializer_builder
16
- .define(self, name, option: false, **options)
17
- self
15
+ @initializer_builder = \
16
+ initializer_builder.define(name, option: false, **options)
17
+ initializer_builder.call(self)
18
18
  end
19
19
 
20
20
  # Declares a named argument
@@ -24,9 +24,9 @@ module Dry::Initializer
24
24
  # @return (see #param)
25
25
  #
26
26
  def option(name, **options)
27
- @initializer_builder = initializer_builder
28
- .define(self, name, option: true, **options)
29
- self
27
+ @initializer_builder = \
28
+ initializer_builder.define(name, option: true, **options)
29
+ initializer_builder.call(self)
30
30
  end
31
31
 
32
32
  # Adds new plugin to the builder
@@ -37,7 +37,24 @@ module Dry::Initializer
37
37
  def register_initializer_plugin(plugin)
38
38
  @initializer_builder = initializer_builder.register(plugin)
39
39
  initializer_builder.call(self)
40
- self
40
+ end
41
+
42
+ # Makes initializer tolerant to unknown options
43
+ #
44
+ # @return [self] itself
45
+ #
46
+ def tolerant_to_unknown_options
47
+ @initializer_builder = initializer_builder.tolerant_to_unknown_options
48
+ initializer_builder.call(self)
49
+ end
50
+
51
+ # Makes initializer intolerant to unknown options
52
+ #
53
+ # @return [self] itself
54
+ #
55
+ def intolerant_to_unknown_options
56
+ @initializer_builder = initializer_builder.intolerant_to_unknown_options
57
+ initializer_builder.call(self)
41
58
  end
42
59
 
43
60
  private
@@ -0,0 +1,27 @@
1
+ describe "options tolerance" do
2
+ before do
3
+ class Test::Foo
4
+ extend Dry::Initializer::Mixin
5
+ end
6
+
7
+ class Test::Bar < Test::Foo
8
+ tolerant_to_unknown_options
9
+ end
10
+
11
+ class Test::Baz < Test::Bar
12
+ intolerant_to_unknown_options
13
+ end
14
+ end
15
+
16
+ it "is strict by default" do
17
+ expect { Test::Foo.new foo: :bar }.to raise_error(ArgumentError)
18
+ end
19
+
20
+ it "allows unknown options when switched on" do
21
+ expect { Test::Bar.new foo: :bar }.not_to raise_error
22
+ end
23
+
24
+ it "can be switched off" do
25
+ expect { Test::Baz.new foo: :bar }.to raise_error(ArgumentError)
26
+ end
27
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dry-initializer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Vladimir Kochnev (marshall-lee)
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2016-05-19 00:00:00.000000000 Z
12
+ date: 2016-05-22 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
@@ -115,6 +115,7 @@ files:
115
115
  - spec/dry/invalid_default_spec.rb
116
116
  - spec/dry/missed_default_spec.rb
117
117
  - spec/dry/object_type_constraint_spec.rb
118
+ - spec/dry/options_tolerance_spec.rb
118
119
  - spec/dry/plain_type_constraint_spec.rb
119
120
  - spec/dry/plugin_registry_spec.rb
120
121
  - spec/dry/reader_spec.rb
@@ -155,6 +156,7 @@ test_files:
155
156
  - spec/dry/invalid_default_spec.rb
156
157
  - spec/dry/missed_default_spec.rb
157
158
  - spec/dry/object_type_constraint_spec.rb
159
+ - spec/dry/options_tolerance_spec.rb
158
160
  - spec/dry/plain_type_constraint_spec.rb
159
161
  - spec/dry/plugin_registry_spec.rb
160
162
  - spec/dry/reader_spec.rb