dry-initializer 3.0.2 → 3.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (64) hide show
  1. checksums.yaml +4 -4
  2. data/.github/ISSUE_TEMPLATE/---bug-report.md +1 -5
  3. data/.github/workflows/custom_ci.yml +31 -47
  4. data/.github/workflows/sync_configs.yml +29 -7
  5. data/.rspec +1 -1
  6. data/.rubocop.yml +19 -6
  7. data/CHANGELOG.md +6 -0
  8. data/Gemfile +27 -29
  9. data/Gemfile.devtools +16 -0
  10. data/Guardfile +3 -3
  11. data/LICENSE +1 -1
  12. data/README.md +17 -77
  13. data/Rakefile +4 -4
  14. data/benchmarks/compare_several_defaults.rb +27 -27
  15. data/benchmarks/plain_options.rb +14 -14
  16. data/benchmarks/plain_params.rb +22 -22
  17. data/benchmarks/with_coercion.rb +14 -14
  18. data/benchmarks/with_defaults.rb +17 -17
  19. data/benchmarks/with_defaults_and_coercion.rb +14 -14
  20. data/bin/.gitkeep +0 -0
  21. data/dry-initializer.gemspec +13 -13
  22. data/lib/dry-initializer.rb +1 -1
  23. data/lib/dry/initializer.rb +9 -9
  24. data/lib/dry/initializer/builders.rb +2 -2
  25. data/lib/dry/initializer/builders/attribute.rb +12 -7
  26. data/lib/dry/initializer/builders/initializer.rb +9 -13
  27. data/lib/dry/initializer/builders/reader.rb +3 -1
  28. data/lib/dry/initializer/builders/signature.rb +3 -3
  29. data/lib/dry/initializer/config.rb +6 -4
  30. data/lib/dry/initializer/definition.rb +9 -9
  31. data/lib/dry/initializer/dispatchers.rb +10 -10
  32. data/lib/dry/initializer/dispatchers/prepare_default.rb +2 -2
  33. data/lib/dry/initializer/dispatchers/prepare_ivar.rb +1 -1
  34. data/lib/dry/initializer/dispatchers/prepare_reader.rb +3 -3
  35. data/lib/dry/initializer/dispatchers/wrap_type.rb +1 -0
  36. data/lib/dry/initializer/mixin.rb +4 -4
  37. data/lib/dry/initializer/version.rb +5 -0
  38. data/lib/tasks/benchmark.rake +13 -13
  39. data/lib/tasks/profile.rake +16 -16
  40. data/project.yml +2 -0
  41. data/spec/attributes_spec.rb +7 -7
  42. data/spec/coercion_of_nil_spec.rb +3 -3
  43. data/spec/custom_dispatchers_spec.rb +6 -6
  44. data/spec/custom_initializer_spec.rb +2 -2
  45. data/spec/default_values_spec.rb +9 -9
  46. data/spec/definition_spec.rb +10 -10
  47. data/spec/invalid_default_spec.rb +2 -2
  48. data/spec/list_type_spec.rb +8 -8
  49. data/spec/missed_default_spec.rb +2 -2
  50. data/spec/nested_type_spec.rb +10 -10
  51. data/spec/optional_spec.rb +16 -16
  52. data/spec/options_tolerance_spec.rb +2 -2
  53. data/spec/public_attributes_utility_spec.rb +5 -5
  54. data/spec/reader_spec.rb +13 -13
  55. data/spec/repetitive_definitions_spec.rb +9 -9
  56. data/spec/several_assignments_spec.rb +9 -9
  57. data/spec/spec_helper.rb +3 -8
  58. data/spec/subclassing_spec.rb +5 -5
  59. data/spec/support/coverage.rb +7 -0
  60. data/spec/support/warnings.rb +7 -0
  61. data/spec/type_argument_spec.rb +13 -13
  62. data/spec/type_constraint_spec.rb +44 -26
  63. data/spec/value_coercion_via_dry_types_spec.rb +7 -7
  64. metadata +11 -31
@@ -1,5 +1,5 @@
1
- describe "optional value" do
2
- context "when has no default value" do
1
+ describe 'optional value' do
2
+ context 'when has no default value' do
3
3
  before do
4
4
  class Test::Foo
5
5
  extend Dry::Initializer
@@ -9,27 +9,27 @@ describe "optional value" do
9
9
  end
10
10
  end
11
11
 
12
- it "quacks like nil" do
12
+ it 'quacks like nil' do
13
13
  subject = Test::Foo.new(1)
14
14
 
15
15
  expect(subject.bar).to eq nil
16
16
  end
17
17
 
18
- it "keeps info about been UNDEFINED" do
18
+ it 'keeps info about been UNDEFINED' do
19
19
  subject = Test::Foo.new(1)
20
20
 
21
21
  expect(subject.instance_variable_get(:@bar))
22
22
  .to eq Dry::Initializer::UNDEFINED
23
23
  end
24
24
 
25
- it "can be set explicitly" do
26
- subject = Test::Foo.new(1, "qux")
25
+ it 'can be set explicitly' do
26
+ subject = Test::Foo.new(1, 'qux')
27
27
 
28
- expect(subject.bar).to eq "qux"
28
+ expect(subject.bar).to eq 'qux'
29
29
  end
30
30
  end
31
31
 
32
- context "with undefined: false" do
32
+ context 'with undefined: false' do
33
33
  before do
34
34
  class Test::Foo
35
35
  extend Dry::Initializer[undefined: false]
@@ -39,33 +39,33 @@ describe "optional value" do
39
39
  end
40
40
  end
41
41
 
42
- it "sets undefined values to nil" do
42
+ it 'sets undefined values to nil' do
43
43
  subject = Test::Foo.new(1)
44
44
 
45
45
  expect(subject.instance_variable_get(:@bar)).to be_nil
46
46
  end
47
47
  end
48
48
 
49
- context "when has a default value" do
49
+ context 'when has a default value' do
50
50
  before do
51
51
  class Test::Foo
52
52
  extend Dry::Initializer
53
53
 
54
54
  param :foo
55
- param :bar, optional: true, default: proc { "baz" }
55
+ param :bar, optional: true, default: proc { 'baz' }
56
56
  end
57
57
  end
58
58
 
59
- it "is takes default value" do
59
+ it 'is takes default value' do
60
60
  subject = Test::Foo.new(1)
61
61
 
62
- expect(subject.bar).to eq "baz"
62
+ expect(subject.bar).to eq 'baz'
63
63
  end
64
64
 
65
- it "can be set explicitly" do
66
- subject = Test::Foo.new(1, "qux")
65
+ it 'can be set explicitly' do
66
+ subject = Test::Foo.new(1, 'qux')
67
67
 
68
- expect(subject.bar).to eq "qux"
68
+ expect(subject.bar).to eq 'qux'
69
69
  end
70
70
  end
71
71
  end
@@ -1,11 +1,11 @@
1
- describe "options tolerance" do
1
+ describe 'options tolerance' do
2
2
  before do
3
3
  class Test::Foo
4
4
  extend Dry::Initializer
5
5
  end
6
6
  end
7
7
 
8
- it "allows options before any definition" do
8
+ it 'allows options before any definition' do
9
9
  expect { Test::Foo.new bar: :baz }.not_to raise_error
10
10
  end
11
11
  end
@@ -1,11 +1,11 @@
1
- describe Dry::Initializer, ".dry_initializer.public_attributes" do
1
+ describe Dry::Initializer, '.dry_initializer.public_attributes' do
2
2
  subject { instance.class.dry_initializer.public_attributes(instance) }
3
3
 
4
- context "when class has params" do
4
+ context 'when class has params' do
5
5
  before do
6
6
  class Test::Foo
7
7
  extend Dry::Initializer
8
- param :foo, proc(&:to_s), desc: "a weird parameter"
8
+ param :foo, proc(&:to_s), desc: 'a weird parameter'
9
9
  option :moo, optional: true
10
10
  option :bar, default: proc { 1 }, reader: false
11
11
  option :baz, optional: true, reader: :protected
@@ -15,8 +15,8 @@ describe Dry::Initializer, ".dry_initializer.public_attributes" do
15
15
 
16
16
  let(:instance) { Test::Foo.new(:FOO, bar: :BAR, baz: :BAZ, qux: :QUX) }
17
17
 
18
- it "collects public options only" do
19
- expect(subject).to eq({ foo: "FOO", moo: nil })
18
+ it 'collects public options only' do
19
+ expect(subject).to eq({ foo: 'FOO', moo: nil })
20
20
  end
21
21
  end
22
22
  end
@@ -1,12 +1,12 @@
1
- describe "reader" do
2
- shared_examples "it has no public attr_reader" do
3
- it "does not define a public attr_reader" do
1
+ describe 'reader' do
2
+ shared_examples 'it has no public attr_reader' do
3
+ it 'does not define a public attr_reader' do
4
4
  expect(subject).not_to respond_to :foo
5
5
  expect(subject).not_to respond_to :bar
6
6
  end
7
7
  end
8
8
 
9
- context "with reader: :public or no reader: option" do
9
+ context 'with reader: :public or no reader: option' do
10
10
  subject do
11
11
  class Test::Foo
12
12
  extend Dry::Initializer
@@ -20,14 +20,14 @@ describe "reader" do
20
20
  Test::Foo.new 1, 2, bar: 3, bar2: 4
21
21
  end
22
22
 
23
- it "defines a public attr_reader by default" do
23
+ it 'defines a public attr_reader by default' do
24
24
  expect(subject).to respond_to(:foo, :foo2)
25
25
  expect(subject).to respond_to :bar
26
26
  expect(subject).to respond_to :bar2
27
27
  end
28
28
  end
29
29
 
30
- context "with reader: false" do
30
+ context 'with reader: false' do
31
31
  before do
32
32
  class Test::Foo
33
33
  extend Dry::Initializer
@@ -39,15 +39,15 @@ describe "reader" do
39
39
 
40
40
  subject { Test::Foo.new 1, bar: 2 }
41
41
 
42
- it_behaves_like "it has no public attr_reader"
42
+ it_behaves_like 'it has no public attr_reader'
43
43
 
44
- it "keeps assigning variables" do
44
+ it 'keeps assigning variables' do
45
45
  expect(subject.instance_variable_get(:@foo)).to eql 1
46
46
  expect(subject.instance_variable_get(:@bar)).to eql 2
47
47
  end
48
48
  end
49
49
 
50
- context "with reader: :private" do
50
+ context 'with reader: :private' do
51
51
  before do
52
52
  class Test::Foo
53
53
  extend Dry::Initializer
@@ -59,15 +59,15 @@ describe "reader" do
59
59
 
60
60
  subject { Test::Foo.new 1, bar: 2 }
61
61
 
62
- it_behaves_like "it has no public attr_reader"
62
+ it_behaves_like 'it has no public attr_reader'
63
63
 
64
- it "adds a private attr_reader" do
64
+ it 'adds a private attr_reader' do
65
65
  expect(subject.send(:foo)).to eql 1
66
66
  expect(subject.send(:bar)).to eql 2
67
67
  end
68
68
  end
69
69
 
70
- context "with reader: :protected" do
70
+ context 'with reader: :protected' do
71
71
  subject do
72
72
  class Test::Foo
73
73
  extend Dry::Initializer
@@ -79,7 +79,7 @@ describe "reader" do
79
79
  Test::Foo.new 1, bar: 2
80
80
  end
81
81
 
82
- it "adds a protected attr_reader" do
82
+ it 'adds a protected attr_reader' do
83
83
  protected_instance_methods = subject.class.protected_instance_methods
84
84
  expect(protected_instance_methods).to match_array(%i[foo bar])
85
85
  end
@@ -1,7 +1,7 @@
1
- describe "repetitive definitions" do
1
+ describe 'repetitive definitions' do
2
2
  subject { Test::Foo.new }
3
3
 
4
- context "of params" do
4
+ context 'of params' do
5
5
  before do
6
6
  class Test::Foo
7
7
  extend Dry::Initializer
@@ -12,12 +12,12 @@ describe "repetitive definitions" do
12
12
  end
13
13
  end
14
14
 
15
- it "reloads the attribute" do
15
+ it 'reloads the attribute' do
16
16
  expect(subject.foo).to eq 2
17
17
  end
18
18
  end
19
19
 
20
- context "of options" do
20
+ context 'of options' do
21
21
  before do
22
22
  class Test::Foo
23
23
  extend Dry::Initializer
@@ -28,12 +28,12 @@ describe "repetitive definitions" do
28
28
  end
29
29
  end
30
30
 
31
- it "reloads the attribute" do
31
+ it 'reloads the attribute' do
32
32
  expect(subject.foo).to eq 2
33
33
  end
34
34
  end
35
35
 
36
- context "of param and option" do
36
+ context 'of param and option' do
37
37
  before do
38
38
  class Test::Foo
39
39
  extend Dry::Initializer
@@ -44,12 +44,12 @@ describe "repetitive definitions" do
44
44
  end
45
45
  end
46
46
 
47
- it "reloads the attribute" do
47
+ it 'reloads the attribute' do
48
48
  expect(subject.foo).to eq 2
49
49
  end
50
50
  end
51
51
 
52
- context "of optional param and option" do
52
+ context 'of optional param and option' do
53
53
  before do
54
54
  class Test::Foo
55
55
  extend Dry::Initializer
@@ -60,7 +60,7 @@ describe "repetitive definitions" do
60
60
  end
61
61
  end
62
62
 
63
- it "allows various assignments" do
63
+ it 'allows various assignments' do
64
64
  expect(Test::Foo.new(1).foo).to eq 1
65
65
  expect(Test::Foo.new(foo: 2).foo).to eq 2
66
66
  expect(Test::Foo.new(1, foo: 2).foo).to eq 2
@@ -1,4 +1,4 @@
1
- describe "attribute with several assignments" do
1
+ describe 'attribute with several assignments' do
2
2
  before do
3
3
  class Test::Foo
4
4
  extend Dry::Initializer
@@ -8,33 +8,33 @@ describe "attribute with several assignments" do
8
8
  end
9
9
  end
10
10
 
11
- context "when not defined" do
11
+ context 'when not defined' do
12
12
  subject { Test::Foo.new }
13
13
 
14
- it "is left undefined" do
14
+ it 'is left undefined' do
15
15
  expect(subject.bar).to be_nil
16
16
  expect(subject.instance_variable_get :@bar)
17
17
  .to eq Dry::Initializer::UNDEFINED
18
18
  end
19
19
  end
20
20
 
21
- context "when set directly" do
21
+ context 'when set directly' do
22
22
  subject { Test::Foo.new bar: :BAZ }
23
23
 
24
- it "sets the attribute" do
25
- expect(subject.bar).to eq "BAZ"
24
+ it 'sets the attribute' do
25
+ expect(subject.bar).to eq 'BAZ'
26
26
  end
27
27
  end
28
28
 
29
- context "when renamed" do
29
+ context 'when renamed' do
30
30
  subject { Test::Foo.new "some foo": :BAZ }
31
31
 
32
- it "renames the attribute" do
32
+ it 'renames the attribute' do
33
33
  expect(subject.bar).to eq :BAZ
34
34
  expect(subject).not_to respond_to :foo
35
35
  end
36
36
 
37
- it "renames the variable" do
37
+ it 'renames the variable' do
38
38
  expect(subject.instance_variable_get(:@bar)).to eq :BAZ
39
39
  end
40
40
  end
@@ -1,14 +1,9 @@
1
- if ENV['COVERAGE'] == 'true'
2
- require 'simplecov'
3
- SimpleCov.start do
4
- add_filter '/spec/'
5
- end
6
- end
1
+ require_relative 'support/coverage'
7
2
 
8
- require "dry/initializer"
3
+ require 'dry/initializer'
9
4
 
10
5
  begin
11
- require "pry"
6
+ require 'pry'
12
7
  rescue LoadError
13
8
  nil
14
9
  end
@@ -1,4 +1,4 @@
1
- describe "subclassing" do
1
+ describe 'subclassing' do
2
2
  before do
3
3
  class Test::Foo
4
4
  extend Dry::Initializer[undefined: false]
@@ -20,24 +20,24 @@ describe "subclassing" do
20
20
  Test::Bar.new 1, 2, bar: 3, qux: 4
21
21
  end
22
22
 
23
- it "preserves null definition" do
23
+ it 'preserves null definition' do
24
24
  expect(Test::Foo.dry_initializer.null).to be_nil
25
25
  expect(Test::Bar.dry_initializer.null).to be_nil
26
26
  end
27
27
 
28
- it "preserves definitions made in the superclass" do
28
+ it 'preserves definitions made in the superclass' do
29
29
  expect(instance_of_subclass.foo).to eql 1
30
30
  expect(instance_of_subclass.baz).to eql 2
31
31
  expect(instance_of_subclass.bar).to eql 3
32
32
  expect(instance_of_subclass.qux).to eql 4
33
33
  end
34
34
 
35
- it "does not pollute superclass with definitions from subclass" do
35
+ it 'does not pollute superclass with definitions from subclass' do
36
36
  expect(instance_of_superclass).not_to respond_to :baz
37
37
  expect(instance_of_superclass).not_to respond_to :qux
38
38
  end
39
39
 
40
- it "calls .inherited hook added by other mixin" do
40
+ it 'calls .inherited hook added by other mixin' do
41
41
  called = false
42
42
  mixin = Module.new { define_method(:inherited) { |_| called = true } }
43
43
 
@@ -0,0 +1,7 @@
1
+ # this file is managed by dry-rb/devtools
2
+
3
+ if ENV['COVERAGE'] == 'true'
4
+ require 'codacy-coverage'
5
+
6
+ Codacy::Reporter.start
7
+ end
@@ -0,0 +1,7 @@
1
+ # this file is managed by dry-rb/devtools project
2
+
3
+ require 'warning'
4
+
5
+ Warning.ignore(%r{rspec/core})
6
+ Warning.ignore(/codacy/)
7
+ Warning[:experimental] = false if Warning.respond_to?(:[])
@@ -1,34 +1,34 @@
1
- require "dry-types"
1
+ require 'dry-types'
2
2
 
3
- describe "type argument" do
3
+ describe 'type argument' do
4
4
  before do
5
5
  class Test::Foo
6
6
  extend Dry::Initializer
7
- param :foo, Dry::Types["strict.string"]
8
- option :bar, Dry::Types["strict.string"]
7
+ param :foo, Dry::Types['strict.string']
8
+ option :bar, Dry::Types['strict.string']
9
9
  end
10
10
  end
11
11
 
12
- context "in case of param mismatch" do
13
- subject { Test::Foo.new 1, bar: "2" }
12
+ context 'in case of param mismatch' do
13
+ subject { Test::Foo.new 1, bar: '2' }
14
14
 
15
- it "raises TypeError" do
15
+ it 'raises TypeError' do
16
16
  expect { subject }.to raise_error Dry::Types::ConstraintError, /1/
17
17
  end
18
18
  end
19
19
 
20
- context "in case of option mismatch" do
21
- subject { Test::Foo.new "1", bar: 2 }
20
+ context 'in case of option mismatch' do
21
+ subject { Test::Foo.new '1', bar: 2 }
22
22
 
23
- it "raises TypeError" do
23
+ it 'raises TypeError' do
24
24
  expect { subject }.to raise_error Dry::Types::ConstraintError, /2/
25
25
  end
26
26
  end
27
27
 
28
- context "in case of match" do
29
- subject { Test::Foo.new "1", bar: "2" }
28
+ context 'in case of match' do
29
+ subject { Test::Foo.new '1', bar: '2' }
30
30
 
31
- it "completes the initialization" do
31
+ it 'completes the initialization' do
32
32
  expect { subject }.not_to raise_error
33
33
  end
34
34
  end
@@ -1,7 +1,7 @@
1
- require "dry-types"
1
+ require 'dry-types'
2
2
 
3
- describe "type constraint" do
4
- context "by a proc with 1 argument" do
3
+ describe 'type constraint' do
4
+ context 'by a proc with 1 argument' do
5
5
  before do
6
6
  class Test::Foo
7
7
  extend Dry::Initializer
@@ -11,12 +11,12 @@ describe "type constraint" do
11
11
 
12
12
  subject { Test::Foo.new :foo }
13
13
 
14
- it "coerces a value" do
15
- expect(subject.__foo__).to eq "foo"
14
+ it 'coerces a value' do
15
+ expect(subject.__foo__).to eq 'foo'
16
16
  end
17
17
  end
18
18
 
19
- context "by a proc with 2 arguments" do
19
+ context 'by a proc with 2 arguments' do
20
20
  before do
21
21
  class Test::Foo
22
22
  extend Dry::Initializer
@@ -26,47 +26,65 @@ describe "type constraint" do
26
26
 
27
27
  subject { Test::Foo.new :foo }
28
28
 
29
- it "coerces a value with self as a second argument" do
29
+ it 'coerces a value with self as a second argument' do
30
30
  expect(subject.foo).to eq "#{subject.hash}:foo"
31
31
  end
32
32
  end
33
33
 
34
- context "by dry-type" do
34
+ context 'by dry-type' do
35
35
  before do
36
- class Test::Foo
36
+ constraint = self.constraint
37
+
38
+ Test::Foo = Class.new do
37
39
  extend Dry::Initializer
38
- param :foo, Dry::Types["strict.string"], optional: true
40
+ param :foo, constraint, optional: true
39
41
  end
40
42
  end
41
43
 
42
- context "in case of mismatch" do
43
- subject { Test::Foo.new 1 }
44
+ context 'with a strict string' do
45
+ let(:constraint) { Dry::Types['strict.string'] }
46
+
47
+ context 'in case of mismatch' do
48
+ subject { Test::Foo.new 1 }
44
49
 
45
- it "raises ArgumentError" do
46
- expect { subject }.to raise_error Dry::Types::ConstraintError, /1/
50
+ it 'raises ArgumentError' do
51
+ expect { subject }.to raise_error Dry::Types::ConstraintError, /1/
52
+ end
47
53
  end
48
- end
49
54
 
50
- context "in case of match" do
51
- subject { Test::Foo.new "foo" }
55
+ context 'in case of match' do
56
+ subject { Test::Foo.new 'foo' }
57
+
58
+ it 'completes the initialization' do
59
+ expect { subject }.not_to raise_error
60
+ end
61
+ end
62
+
63
+ context 'if optional value not set' do
64
+ subject { Test::Foo.new }
52
65
 
53
- it "completes the initialization" do
54
- expect { subject }.not_to raise_error
66
+ it 'not applicable to Dry::Initializer::UNDEFINED' do
67
+ expect(subject.instance_variable_get(:@foo))
68
+ .to eq Dry::Initializer::UNDEFINED
69
+ end
55
70
  end
56
71
  end
57
72
 
58
- context "if optional value not set" do
59
- subject { Test::Foo.new }
73
+ context 'with a member array string' do
74
+ let(:constraint) { Dry::Types['array'].of(Dry::Types['strict.string']) }
75
+
76
+ context 'with arity other than 1' do
77
+ subject { Test::Foo.new ['foo'] }
60
78
 
61
- it "not applicable to Dry::Initializer::UNDEFINED" do
62
- expect(subject.instance_variable_get(:@foo))
63
- .to eq Dry::Initializer::UNDEFINED
79
+ it 'completes the initialization' do
80
+ expect { subject }.not_to raise_error
81
+ end
64
82
  end
65
83
  end
66
84
  end
67
85
 
68
- context "by invalid constraint" do
69
- it "raises ArgumentError" do
86
+ context 'by invalid constraint' do
87
+ it 'raises ArgumentError' do
70
88
  expect do
71
89
  class Test::Foo
72
90
  extend Dry::Initializer