dry-initializer 2.4.0 → 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.
- checksums.yaml +5 -5
- data/.codeclimate.yml +10 -21
- data/.github/ISSUE_TEMPLATE/----please-don-t-ask-for-support-via-issues.md +10 -0
- data/.github/ISSUE_TEMPLATE/---bug-report.md +30 -0
- data/.github/ISSUE_TEMPLATE/---feature-request.md +18 -0
- data/.github/workflows/custom_ci.yml +58 -0
- data/.github/workflows/docsite.yml +34 -0
- data/.github/workflows/sync_configs.yml +56 -0
- data/.gitignore +2 -0
- data/.rspec +1 -1
- data/.rubocop.yml +76 -25
- data/CHANGELOG.md +150 -14
- data/CODE_OF_CONDUCT.md +13 -0
- data/CONTRIBUTING.md +29 -0
- data/Gemfile +25 -18
- data/Gemfile.devtools +16 -0
- data/Guardfile +3 -3
- data/LICENSE +20 -0
- data/README.md +17 -79
- data/Rakefile +4 -4
- data/benchmarks/compare_several_defaults.rb +27 -27
- data/benchmarks/plain_options.rb +14 -14
- data/benchmarks/plain_params.rb +22 -22
- data/benchmarks/with_coercion.rb +14 -14
- data/benchmarks/with_defaults.rb +17 -17
- data/benchmarks/with_defaults_and_coercion.rb +14 -14
- data/bin/.gitkeep +0 -0
- data/docsite/source/attributes.html.md +106 -0
- data/docsite/source/container-version.html.md +39 -0
- data/docsite/source/index.html.md +43 -0
- data/docsite/source/inheritance.html.md +43 -0
- data/docsite/source/optionals-and-defaults.html.md +130 -0
- data/docsite/source/options-tolerance.html.md +27 -0
- data/docsite/source/params-and-options.html.md +74 -0
- data/docsite/source/rails-support.html.md +101 -0
- data/docsite/source/readers.html.md +43 -0
- data/docsite/source/skip-undefined.html.md +59 -0
- data/docsite/source/type-constraints.html.md +160 -0
- data/dry-initializer.gemspec +13 -13
- data/lib/dry-initializer.rb +1 -1
- data/lib/dry/initializer.rb +17 -16
- data/lib/dry/initializer/builders.rb +2 -2
- data/lib/dry/initializer/builders/attribute.rb +16 -11
- data/lib/dry/initializer/builders/initializer.rb +9 -13
- data/lib/dry/initializer/builders/reader.rb +4 -2
- data/lib/dry/initializer/builders/signature.rb +3 -3
- data/lib/dry/initializer/config.rb +25 -12
- data/lib/dry/initializer/definition.rb +20 -71
- data/lib/dry/initializer/dispatchers.rb +101 -33
- data/lib/dry/initializer/dispatchers/build_nested_type.rb +59 -0
- data/lib/dry/initializer/dispatchers/check_type.rb +43 -0
- data/lib/dry/initializer/dispatchers/prepare_default.rb +40 -0
- data/lib/dry/initializer/dispatchers/prepare_ivar.rb +12 -0
- data/lib/dry/initializer/dispatchers/prepare_optional.rb +13 -0
- data/lib/dry/initializer/dispatchers/prepare_reader.rb +30 -0
- data/lib/dry/initializer/dispatchers/prepare_source.rb +28 -0
- data/lib/dry/initializer/dispatchers/prepare_target.rb +44 -0
- data/lib/dry/initializer/dispatchers/unwrap_type.rb +22 -0
- data/lib/dry/initializer/dispatchers/wrap_type.rb +28 -0
- data/lib/dry/initializer/mixin.rb +4 -4
- data/lib/dry/initializer/mixin/root.rb +1 -0
- data/lib/dry/initializer/struct.rb +39 -0
- data/lib/dry/initializer/undefined.rb +2 -0
- data/lib/dry/initializer/version.rb +5 -0
- data/lib/tasks/benchmark.rake +13 -13
- data/lib/tasks/profile.rake +16 -16
- data/project.yml +2 -0
- data/spec/attributes_spec.rb +7 -7
- data/spec/coercion_of_nil_spec.rb +25 -0
- data/spec/custom_dispatchers_spec.rb +6 -6
- data/spec/custom_initializer_spec.rb +2 -2
- data/spec/default_values_spec.rb +9 -9
- data/spec/definition_spec.rb +16 -12
- data/spec/invalid_default_spec.rb +2 -2
- data/spec/list_type_spec.rb +32 -0
- data/spec/missed_default_spec.rb +2 -2
- data/spec/nested_type_spec.rb +48 -0
- data/spec/optional_spec.rb +16 -16
- data/spec/options_tolerance_spec.rb +2 -2
- data/spec/public_attributes_utility_spec.rb +5 -5
- data/spec/reader_spec.rb +13 -13
- data/spec/repetitive_definitions_spec.rb +9 -9
- data/spec/several_assignments_spec.rb +9 -9
- data/spec/spec_helper.rb +6 -3
- data/spec/subclassing_spec.rb +5 -5
- data/spec/support/coverage.rb +7 -0
- data/spec/support/warnings.rb +7 -0
- data/spec/type_argument_spec.rb +15 -15
- data/spec/type_constraint_spec.rb +46 -28
- data/spec/value_coercion_via_dry_types_spec.rb +8 -8
- metadata +51 -34
- data/.travis.yml +0 -24
@@ -1,4 +1,4 @@
|
|
1
|
-
describe
|
1
|
+
describe 'invalid default value assignment' do
|
2
2
|
subject do
|
3
3
|
class Test::Foo
|
4
4
|
extend Dry::Initializer
|
@@ -7,7 +7,7 @@ describe "invalid default value assignment" do
|
|
7
7
|
end
|
8
8
|
end
|
9
9
|
|
10
|
-
it
|
10
|
+
it 'raises TypeError' do
|
11
11
|
expect { subject }.to raise_error TypeError
|
12
12
|
end
|
13
13
|
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
require 'dry-types'
|
2
|
+
|
3
|
+
describe 'list type argument' do
|
4
|
+
before do
|
5
|
+
class Test::Foo
|
6
|
+
extend Dry::Initializer
|
7
|
+
param :foo, [proc(&:to_s)]
|
8
|
+
option :bar, [Dry::Types['strict.string']]
|
9
|
+
option :baz, []
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
context 'with single items' do
|
14
|
+
subject { Test::Foo.new(1, bar: '2', baz: { qux: :QUX }) }
|
15
|
+
|
16
|
+
it 'coerces and wraps them to arrays' do
|
17
|
+
expect(subject.foo).to eq %w[1]
|
18
|
+
expect(subject.bar).to eq %w[2]
|
19
|
+
expect(subject.baz).to eq [{ qux: :QUX }]
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
context 'with arrays' do
|
24
|
+
subject { Test::Foo.new([1], bar: %w[2], baz: [{ qux: :QUX }]) }
|
25
|
+
|
26
|
+
it 'coerces elements' do
|
27
|
+
expect(subject.foo).to eq %w[1]
|
28
|
+
expect(subject.bar).to eq %w[2]
|
29
|
+
expect(subject.baz).to eq [{ qux: :QUX }]
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
data/spec/missed_default_spec.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
describe
|
1
|
+
describe 'missed default values' do
|
2
2
|
subject do
|
3
3
|
class Test::Foo
|
4
4
|
extend Dry::Initializer
|
@@ -8,7 +8,7 @@ describe "missed default values" do
|
|
8
8
|
end
|
9
9
|
end
|
10
10
|
|
11
|
-
it
|
11
|
+
it 'raises SyntaxError' do
|
12
12
|
expect { subject }.to raise_error SyntaxError, /bar/
|
13
13
|
end
|
14
14
|
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
describe 'nested type argument' do
|
2
|
+
subject { Test::Xyz.new('bar' => { 'baz' => 42 }) }
|
3
|
+
|
4
|
+
context 'with nested definition only' do
|
5
|
+
before do
|
6
|
+
class Test::Xyz
|
7
|
+
extend Dry::Initializer
|
8
|
+
|
9
|
+
param :foo, as: :x do
|
10
|
+
option :bar, as: :y do
|
11
|
+
option :baz, proc(&:to_s), as: :z
|
12
|
+
option :qux, as: :w, optional: true
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
it 'builds the type' do
|
19
|
+
expect(subject.x.y.z).to eq '42'
|
20
|
+
end
|
21
|
+
|
22
|
+
it 'converts the nested type to hash' do
|
23
|
+
expect(subject.x.to_h).to eq('y' => { 'z' => '42' })
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
context 'with nested and wrapped definitions' do
|
28
|
+
before do
|
29
|
+
class Test::Xyz
|
30
|
+
extend Dry::Initializer
|
31
|
+
|
32
|
+
param :foo, [], as: :x do
|
33
|
+
option :bar, as: :y do
|
34
|
+
option :baz, proc(&:to_s), as: :z
|
35
|
+
option :qux, as: :w, optional: true
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
it 'builds the type' do
|
42
|
+
x = subject.x
|
43
|
+
expect(x).to be_instance_of Array
|
44
|
+
|
45
|
+
expect(x.first.y.z).to eq '42'
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
data/spec/optional_spec.rb
CHANGED
@@ -1,5 +1,5 @@
|
|
1
|
-
describe
|
2
|
-
context
|
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
|
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
|
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
|
26
|
-
subject = Test::Foo.new(1,
|
25
|
+
it 'can be set explicitly' do
|
26
|
+
subject = Test::Foo.new(1, 'qux')
|
27
27
|
|
28
|
-
expect(subject.bar).to eq
|
28
|
+
expect(subject.bar).to eq 'qux'
|
29
29
|
end
|
30
30
|
end
|
31
31
|
|
32
|
-
context
|
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
|
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
|
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 {
|
55
|
+
param :bar, optional: true, default: proc { 'baz' }
|
56
56
|
end
|
57
57
|
end
|
58
58
|
|
59
|
-
it
|
59
|
+
it 'is takes default value' do
|
60
60
|
subject = Test::Foo.new(1)
|
61
61
|
|
62
|
-
expect(subject.bar).to eq
|
62
|
+
expect(subject.bar).to eq 'baz'
|
63
63
|
end
|
64
64
|
|
65
|
-
it
|
66
|
-
subject = Test::Foo.new(1,
|
65
|
+
it 'can be set explicitly' do
|
66
|
+
subject = Test::Foo.new(1, 'qux')
|
67
67
|
|
68
|
-
expect(subject.bar).to eq
|
68
|
+
expect(subject.bar).to eq 'qux'
|
69
69
|
end
|
70
70
|
end
|
71
71
|
end
|
@@ -1,11 +1,11 @@
|
|
1
|
-
describe
|
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
|
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,
|
1
|
+
describe Dry::Initializer, '.dry_initializer.public_attributes' do
|
2
2
|
subject { instance.class.dry_initializer.public_attributes(instance) }
|
3
3
|
|
4
|
-
context
|
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:
|
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
|
19
|
-
expect(subject).to eq({ foo:
|
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
|
data/spec/reader_spec.rb
CHANGED
@@ -1,12 +1,12 @@
|
|
1
|
-
describe
|
2
|
-
shared_examples
|
3
|
-
it
|
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
|
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
|
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
|
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
|
42
|
+
it_behaves_like 'it has no public attr_reader'
|
43
43
|
|
44
|
-
it
|
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
|
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
|
62
|
+
it_behaves_like 'it has no public attr_reader'
|
63
63
|
|
64
|
-
it
|
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
|
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
|
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
|
1
|
+
describe 'repetitive definitions' do
|
2
2
|
subject { Test::Foo.new }
|
3
3
|
|
4
|
-
context
|
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
|
15
|
+
it 'reloads the attribute' do
|
16
16
|
expect(subject.foo).to eq 2
|
17
17
|
end
|
18
18
|
end
|
19
19
|
|
20
|
-
context
|
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
|
31
|
+
it 'reloads the attribute' do
|
32
32
|
expect(subject.foo).to eq 2
|
33
33
|
end
|
34
34
|
end
|
35
35
|
|
36
|
-
context
|
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
|
47
|
+
it 'reloads the attribute' do
|
48
48
|
expect(subject.foo).to eq 2
|
49
49
|
end
|
50
50
|
end
|
51
51
|
|
52
|
-
context
|
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
|
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
|
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
|
11
|
+
context 'when not defined' do
|
12
12
|
subject { Test::Foo.new }
|
13
13
|
|
14
|
-
it
|
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
|
21
|
+
context 'when set directly' do
|
22
22
|
subject { Test::Foo.new bar: :BAZ }
|
23
23
|
|
24
|
-
it
|
25
|
-
expect(subject.bar).to eq
|
24
|
+
it 'sets the attribute' do
|
25
|
+
expect(subject.bar).to eq 'BAZ'
|
26
26
|
end
|
27
27
|
end
|
28
28
|
|
29
|
-
context
|
29
|
+
context 'when renamed' do
|
30
30
|
subject { Test::Foo.new "some foo": :BAZ }
|
31
31
|
|
32
|
-
it
|
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
|
37
|
+
it 'renames the variable' do
|
38
38
|
expect(subject.instance_variable_get(:@bar)).to eq :BAZ
|
39
39
|
end
|
40
40
|
end
|
data/spec/spec_helper.rb
CHANGED
data/spec/subclassing_spec.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
describe
|
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
|
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
|
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
|
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
|
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
|
|