dry-initializer 0.7.0 → 0.8.0

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: 0de7dd513351b39d831351662ec6268ebf1920a9
4
- data.tar.gz: 1435433ffc6eabba75f481e302ea4b7dcc47d37b
3
+ metadata.gz: dc5b54635b8cfc7c85c33e0aac287c54919cb3de
4
+ data.tar.gz: dde1379e2f5f9f86ad1de906e8402a326a850c70
5
5
  SHA512:
6
- metadata.gz: 7ed882e91dfeed3a4dcc8ade69792beca84334d9850e700de67462cead64434fdfeed5039d188b42799b204d1cb871394e0c9c0e8a7c26ba5d02d6a2c72c08d3
7
- data.tar.gz: a477d334fd746c41d09bfc49bdde5af80f4aa22176996518092853bf3a660f3fa4f77f30a84f495d04f54f1592b95d077a3f17473dbd759bf7e310c9ef0396bd
6
+ metadata.gz: 512704ff1885b0c9bbb147020b950e97e70eacd4405548cad5478b728eee2861c47e58f19ff4603c21edf08965372820386645fb451e3c93edfdb61f3185d3e2
7
+ data.tar.gz: 7090b29b8c66d79c1b7c9f9ccb4019ec9ccc9ae07126a9c2b670eb4f6a0f6b4b669f450dd701514e24cab465a9d760b81443db5959d38834c4cec327ad9ec336
@@ -1,3 +1,53 @@
1
+ ## v0.8.0 2016-11-05
2
+
3
+ In this version we switched from key arguments to ** to support special keys:
4
+
5
+ option :end
6
+ option :begin
7
+
8
+ In previous versions this was translated to
9
+
10
+ def initialize(end:, begin:)
11
+ @end = end # BOOM! SyntaxError!
12
+ @begin = begin # Potential BOOM (unreached)
13
+ end
14
+
15
+ Now the assignment is imlemented like this:
16
+
17
+ def initialize(**__options__)
18
+ @end = __options__.fetch(:end)
19
+ @begin = __options__.fetch(:begin)
20
+ end
21
+
22
+ As a side effect of the change the initializer becomes tolerant
23
+ to any unknown option if, and only if some `option` was set explicitly.
24
+
25
+ Methods `tolerant_to_unknown_options` and `intolerant_to_unknown_options`
26
+ are deprecated and will be removed in the next version of the gem.
27
+
28
+ ### Added
29
+
30
+ * support for special options like `option :end`, `option :begin` etc. (@nepalez)
31
+
32
+ ### Internals
33
+
34
+ * switched from key arguments to serialized hash argument in the initializer (@nepalez)
35
+
36
+ ### Breaking Changes
37
+
38
+ * the initializer becomes tolerant to unknown options when any `option` was set,
39
+ ignoring `intolerant_to_unknown_options` helper.
40
+
41
+ * the initializer becomes intolerant to options when no `option` was set,
42
+ ignoring `tolerant_to_unknown_options` helper.
43
+
44
+ ### Deprecated
45
+
46
+ * `tolerant_to_unknown_options`
47
+ * `intolerant_to_unknown_options`
48
+
49
+ [Compare v0.7.0...v0.8.0](https://github.com/dry-rb/dry-initializer/compare/v0.7.0..v0.8.0)
50
+
1
51
  ## v0.7.0 2016-10-11
2
52
 
3
53
  ### Added
data/README.md CHANGED
@@ -43,15 +43,17 @@ $ gem install dry-initializer
43
43
 
44
44
  ```ruby
45
45
  require 'dry-initializer'
46
+ require 'dry-types'
46
47
 
47
48
  class User
48
49
  extend Dry::Initializer::Mixin
49
50
 
50
51
  # Params of the initializer along with corresponding readers
51
- param :name
52
+ param :name, type: Dry::Types["strict.string"]
52
53
  param :role, default: proc { 'customer' }
53
54
  # Options of the initializer along with corresponding readers
54
55
  option :admin, default: proc { false }
56
+ option :vip, optional: true
55
57
  end
56
58
 
57
59
  # Defines the initializer with params and options
@@ -61,6 +63,7 @@ user = User.new 'Vladimir', 'admin', admin: true
61
63
  user.name # => 'Vladimir'
62
64
  user.role # => 'admin'
63
65
  user.admin # => true
66
+ user.vip # => Dry::Initializer::UNDEFINED
64
67
  ```
65
68
 
66
69
  See full documentation on the [Dry project official site][docs]
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |gem|
2
2
  gem.name = "dry-initializer"
3
- gem.version = "0.7.0"
3
+ gem.version = "0.8.0"
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"
@@ -25,22 +25,6 @@ module Dry::Initializer
25
25
  copy { @plugins = plugins }
26
26
  end
27
27
 
28
- # Makes builder to provide options-tolerant initializer
29
- #
30
- # @return [Dry::Initializer::Builder]
31
- #
32
- def tolerant_to_unknown_options
33
- copy { @tolerant = "**" }
34
- end
35
-
36
- # Makes builder to provide options-intolerant initializer
37
- #
38
- # @return [Dry::Initializer::Builder]
39
- #
40
- def intolerant_to_unknown_options
41
- copy { @tolerant = nil }
42
- end
43
-
44
28
  # Defines new agrument and reloads mixin definitions
45
29
  #
46
30
  # @param [#to_sym] name
@@ -57,8 +57,13 @@ module Dry::Initializer
57
57
  # @return [self] itself
58
58
  #
59
59
  def tolerant_to_unknown_options
60
- @initializer_builder = initializer_builder.tolerant_to_unknown_options
61
- initializer_builder.call(self)
60
+ warn <<-MESSAGE.gsub(/ +\|/, "")
61
+ |[DEPRECATION] Helper method `tolerant_to_unknown_options` was deprecated,
62
+ |and will be removed in v0.9.0.
63
+ |If any `option` was defined, the initializer becomes tolerant to all the others.
64
+ |This is necessary to support "special" names in options like `option :end`.
65
+ |It forbids options only when no one was declared explicitly.
66
+ MESSAGE
62
67
  end
63
68
 
64
69
  # Makes initializer intolerant to unknown options
@@ -66,8 +71,13 @@ module Dry::Initializer
66
71
  # @return [self] itself
67
72
  #
68
73
  def intolerant_to_unknown_options
69
- @initializer_builder = initializer_builder.intolerant_to_unknown_options
70
- initializer_builder.call(self)
74
+ warn <<-MESSAGE.gsub(/ +\|/, "")
75
+ |[DEPRECATION] Helper method `intolerant_to_unknown_options` was deprecated.
76
+ |and will be removed in v0.9.0.
77
+ |If any `option` was defined, the initializer becomes tolerant to all the others.
78
+ |This is necessary to support "special" names in options like `option :end`.
79
+ |It forbids options only when no one was declared explicitly.
80
+ MESSAGE
71
81
  end
72
82
 
73
83
  private
@@ -2,38 +2,28 @@ module Dry::Initializer::Plugins
2
2
  # Plugin builds a chunk of code for the initializer's signature:
3
3
  #
4
4
  # @example
5
- # Signature.call(:user, option: true)
6
- # # => "user:"
5
+ # Signature.call(:user)
6
+ # # => "user"
7
7
  #
8
8
  # Signature.call(:user, default: -> { nil })
9
9
  # # => "user = Dry::Initializer::UNDEFINED"
10
10
  #
11
+ # Signature.call(:user, option: true)
12
+ # # => "**__options__"
13
+ #
11
14
  class Signature < Base
12
15
  def param?
13
16
  settings[:option] != true
14
17
  end
15
18
 
16
- def default?
17
- settings.key? :default
18
- end
19
-
20
- def optional?
21
- default? || settings.key?(:optional)
19
+ def required?
20
+ !settings.key?(:default) && !settings[:optional]
22
21
  end
23
22
 
24
23
  def call
25
- case [param?, optional?]
26
- when [true, false] then name.to_s
27
- when [false, false] then "#{name}:"
28
- when [true, true] then "#{name} = #{undefined}"
29
- when [false, true] then "#{name}: #{undefined}"
30
- end
31
- end
32
-
33
- private
34
-
35
- def undefined
36
- @undefined ||= "Dry::Initializer::UNDEFINED"
24
+ return "**__options__" unless param?
25
+ return name.to_s if required?
26
+ "#{name} = Dry::Initializer::UNDEFINED"
37
27
  end
38
28
  end
39
29
  end
@@ -2,11 +2,28 @@ module Dry::Initializer::Plugins
2
2
  # Plugin builds a code for variable setter:
3
3
  #
4
4
  # @example
5
- # VariableSetter.call(:user, {}) # => "@user = user"
5
+ # VariableSetter.call(:user, option: false)
6
+ # # => "@user = user"
7
+ #
8
+ # VariableSetter.call(:user, option: true)
9
+ # # => "@user = __options__.fetch(:user)"
10
+ #
11
+ # VariableSetter.call(:user, option: true, optional: true)
12
+ # # => "@user = __options__.fetch(:user, Dry::Initializer::UNDEFINED)"
6
13
  #
7
14
  class VariableSetter < Base
15
+ def param?
16
+ settings[:option] != true
17
+ end
18
+
19
+ def required?
20
+ !settings.key?(:default) && !settings[:optional]
21
+ end
22
+
8
23
  def call
9
- "@#{name} = #{name}"
24
+ return "@#{name} = #{name}" if param?
25
+ return "@#{name} = __options__.fetch(:#{name})" if required?
26
+ "@#{name} = __options__.fetch(:#{name}, Dry::Initializer::UNDEFINED)"
10
27
  end
11
28
  end
12
29
  end
@@ -25,7 +25,7 @@ module Dry::Initializer
25
25
  end
26
26
 
27
27
  def call
28
- map(&:call).join(", ")
28
+ map(&:call).compact.uniq.join(", ")
29
29
  end
30
30
 
31
31
  private
@@ -43,8 +43,8 @@ module Dry::Initializer
43
43
  end
44
44
 
45
45
  def validates_order_of(signature)
46
- return unless signature.param? && !signature.optional?
47
- return unless any? { |item| item.param? && item.optional? }
46
+ return unless signature.param? && signature.required?
47
+ return unless any? { |item| item.param? && !item.required? }
48
48
 
49
49
  fail OrderError.new(signature.name)
50
50
  end
File without changes
@@ -0,0 +1,20 @@
1
+ describe "options tolerance" do
2
+ before do
3
+ class Test::Foo
4
+ extend Dry::Initializer::Mixin
5
+ option :foo
6
+ end
7
+
8
+ class Test::Bar
9
+ extend Dry::Initializer::Mixin
10
+ end
11
+ end
12
+
13
+ it "allows unknown options when someone defined" do
14
+ expect { Test::Foo.new foo: :bar, bar: :baz }.not_to raise_error
15
+ end
16
+
17
+ it "forbids options when no one defined" do
18
+ expect { Test::Bar.new bar: :baz }.to raise_error(ArgumentError)
19
+ end
20
+ end
@@ -0,0 +1,35 @@
1
+ describe "shared definition" do
2
+ subject do
3
+ class Test::Foo
4
+ extend Dry::Initializer::Mixin
5
+
6
+ using default: proc { nil } do
7
+ param :foo
8
+ option :end
9
+ option :baz, default: proc { 0 }
10
+ end
11
+
12
+ using optional: true do
13
+ option :qux
14
+ option :quxx, optional: false
15
+ end
16
+ end
17
+ end
18
+
19
+ it "is applied to params and options" do
20
+ instance = subject.new(quxx: 1)
21
+
22
+ expect(instance.foo).to be_nil
23
+ expect(instance.end).to be_nil
24
+ end
25
+
26
+ it "can be reloaded" do
27
+ instance = subject.new(quxx: 1)
28
+
29
+ expect(instance.baz).to eq 0
30
+ end
31
+
32
+ it "can reload :optional setting" do
33
+ expect { subject.new }.to raise_error(KeyError, /quxx/)
34
+ end
35
+ 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.7.0
4
+ version: 0.8.0
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-10-11 00:00:00.000000000 Z
12
+ date: 2016-11-05 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
@@ -121,22 +121,22 @@ files:
121
121
  - lib/dry/initializer/plugins/variable_setter.rb
122
122
  - lib/dry/initializer/scope.rb
123
123
  - lib/dry/initializer/signature.rb
124
- - spec/dry/base_spec.rb
125
- - spec/dry/container_spec.rb
126
- - spec/dry/default_nil_spec.rb
127
- - spec/dry/default_values_spec.rb
128
- - spec/dry/invalid_default_spec.rb
129
- - spec/dry/missed_default_spec.rb
130
- - spec/dry/optional_spec.rb
131
- - spec/dry/options_tolerance_spec.rb
132
- - spec/dry/plugin_registry_spec.rb
133
- - spec/dry/reader_spec.rb
134
- - spec/dry/repetitive_definitions_spec.rb
135
- - spec/dry/shared_definitions_spec.rb
136
- - spec/dry/subclassing_spec.rb
137
- - spec/dry/type_constraint_spec.rb
138
- - spec/dry/value_coercion_via_dry_types_spec.rb
124
+ - spec/base_spec.rb
125
+ - spec/container_spec.rb
126
+ - spec/default_nil_spec.rb
127
+ - spec/default_values_spec.rb
128
+ - spec/invalid_default_spec.rb
129
+ - spec/missed_default_spec.rb
130
+ - spec/optional_spec.rb
131
+ - spec/options_tolerance_spec.rb
132
+ - spec/plugin_registry_spec.rb
133
+ - spec/reader_spec.rb
134
+ - spec/repetitive_definitions_spec.rb
135
+ - spec/shared_definitions_spec.rb
139
136
  - spec/spec_helper.rb
137
+ - spec/subclassing_spec.rb
138
+ - spec/type_constraint_spec.rb
139
+ - spec/value_coercion_via_dry_types_spec.rb
140
140
  homepage: https://github.com/dryrb/dry-initializer
141
141
  licenses:
142
142
  - MIT
@@ -162,19 +162,19 @@ signing_key:
162
162
  specification_version: 4
163
163
  summary: DSL for declaring params and options of the initializer
164
164
  test_files:
165
- - spec/dry/base_spec.rb
166
- - spec/dry/container_spec.rb
167
- - spec/dry/default_nil_spec.rb
168
- - spec/dry/default_values_spec.rb
169
- - spec/dry/invalid_default_spec.rb
170
- - spec/dry/missed_default_spec.rb
171
- - spec/dry/optional_spec.rb
172
- - spec/dry/options_tolerance_spec.rb
173
- - spec/dry/plugin_registry_spec.rb
174
- - spec/dry/reader_spec.rb
175
- - spec/dry/repetitive_definitions_spec.rb
176
- - spec/dry/shared_definitions_spec.rb
177
- - spec/dry/subclassing_spec.rb
178
- - spec/dry/type_constraint_spec.rb
179
- - spec/dry/value_coercion_via_dry_types_spec.rb
165
+ - spec/base_spec.rb
166
+ - spec/container_spec.rb
167
+ - spec/default_nil_spec.rb
168
+ - spec/default_values_spec.rb
169
+ - spec/invalid_default_spec.rb
170
+ - spec/missed_default_spec.rb
171
+ - spec/optional_spec.rb
172
+ - spec/options_tolerance_spec.rb
173
+ - spec/plugin_registry_spec.rb
174
+ - spec/reader_spec.rb
175
+ - spec/repetitive_definitions_spec.rb
176
+ - spec/shared_definitions_spec.rb
180
177
  - spec/spec_helper.rb
178
+ - spec/subclassing_spec.rb
179
+ - spec/type_constraint_spec.rb
180
+ - spec/value_coercion_via_dry_types_spec.rb
@@ -1,27 +0,0 @@
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
@@ -1,22 +0,0 @@
1
- describe "shared definition" do
2
- subject do
3
- class Test::Foo
4
- extend Dry::Initializer::Mixin
5
-
6
- using default: proc { nil } do
7
- param :foo
8
- option :bar
9
- option :baz, default: proc { 0 }
10
- end
11
- end
12
- end
13
-
14
- it "is applied to params and options" do
15
- expect(subject.new.foo).to be_nil
16
- expect(subject.new.bar).to be_nil
17
- end
18
-
19
- it "can be reloaded" do
20
- expect(subject.new.baz).to eq 0
21
- end
22
- end