dry-initializer 3.0.3 → 3.0.4
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 +4 -4
- data/CHANGELOG.md +234 -242
- data/dry-initializer.gemspec +28 -15
- data/lib/dry/initializer/builders/attribute.rb +1 -1
- data/lib/dry/initializer/version.rb +1 -1
- metadata +24 -104
- data/.codeclimate.yml +0 -12
- data/.github/ISSUE_TEMPLATE/----please-don-t-ask-for-support-via-issues.md +0 -10
- data/.github/ISSUE_TEMPLATE/---bug-report.md +0 -30
- data/.github/ISSUE_TEMPLATE/---feature-request.md +0 -18
- data/.github/workflows/custom_ci.yml +0 -58
- data/.github/workflows/docsite.yml +0 -34
- data/.github/workflows/sync_configs.yml +0 -56
- data/.gitignore +0 -12
- data/.rspec +0 -4
- data/.rubocop.yml +0 -102
- data/CODE_OF_CONDUCT.md +0 -13
- data/CONTRIBUTING.md +0 -29
- data/Gemfile +0 -36
- data/Gemfile.devtools +0 -16
- data/Guardfile +0 -5
- data/LICENSE.txt +0 -21
- data/Rakefile +0 -8
- data/benchmarks/compare_several_defaults.rb +0 -82
- data/benchmarks/plain_options.rb +0 -63
- data/benchmarks/plain_params.rb +0 -84
- data/benchmarks/with_coercion.rb +0 -71
- data/benchmarks/with_defaults.rb +0 -66
- data/benchmarks/with_defaults_and_coercion.rb +0 -59
- data/bin/.gitkeep +0 -0
- data/docsite/source/attributes.html.md +0 -106
- data/docsite/source/container-version.html.md +0 -39
- data/docsite/source/index.html.md +0 -43
- data/docsite/source/inheritance.html.md +0 -43
- data/docsite/source/optionals-and-defaults.html.md +0 -130
- data/docsite/source/options-tolerance.html.md +0 -27
- data/docsite/source/params-and-options.html.md +0 -74
- data/docsite/source/rails-support.html.md +0 -101
- data/docsite/source/readers.html.md +0 -43
- data/docsite/source/skip-undefined.html.md +0 -59
- data/docsite/source/type-constraints.html.md +0 -160
- data/project.yml +0 -2
- data/spec/attributes_spec.rb +0 -38
- data/spec/coercion_of_nil_spec.rb +0 -25
- data/spec/custom_dispatchers_spec.rb +0 -35
- data/spec/custom_initializer_spec.rb +0 -30
- data/spec/default_values_spec.rb +0 -83
- data/spec/definition_spec.rb +0 -111
- data/spec/invalid_default_spec.rb +0 -13
- data/spec/list_type_spec.rb +0 -32
- data/spec/missed_default_spec.rb +0 -14
- data/spec/nested_type_spec.rb +0 -48
- data/spec/optional_spec.rb +0 -71
- data/spec/options_tolerance_spec.rb +0 -11
- data/spec/public_attributes_utility_spec.rb +0 -22
- data/spec/reader_spec.rb +0 -87
- data/spec/repetitive_definitions_spec.rb +0 -69
- data/spec/several_assignments_spec.rb +0 -41
- data/spec/spec_helper.rb +0 -24
- data/spec/subclassing_spec.rb +0 -49
- data/spec/support/coverage.rb +0 -7
- data/spec/support/warnings.rb +0 -7
- data/spec/type_argument_spec.rb +0 -35
- data/spec/type_constraint_spec.rb +0 -96
- data/spec/value_coercion_via_dry_types_spec.rb +0 -29
data/project.yml
DELETED
data/spec/attributes_spec.rb
DELETED
@@ -1,38 +0,0 @@
|
|
1
|
-
describe Dry::Initializer, 'dry_initializer.attributes' do
|
2
|
-
subject { instance.class.dry_initializer.attributes(instance) }
|
3
|
-
|
4
|
-
context 'when class has params' do
|
5
|
-
before do
|
6
|
-
class Test::Foo
|
7
|
-
extend Dry::Initializer
|
8
|
-
param :foo, proc(&:to_s)
|
9
|
-
param :bar, default: proc { 1 }
|
10
|
-
param :baz, optional: true
|
11
|
-
end
|
12
|
-
end
|
13
|
-
|
14
|
-
let(:instance) { Test::Foo.new(:FOO) }
|
15
|
-
|
16
|
-
it 'collects coerced params with default values' do
|
17
|
-
expect(subject).to eq({ foo: 'FOO', bar: 1 })
|
18
|
-
end
|
19
|
-
end
|
20
|
-
|
21
|
-
context 'when class has options' do
|
22
|
-
before do
|
23
|
-
class Test::Foo
|
24
|
-
extend Dry::Initializer
|
25
|
-
option :foo
|
26
|
-
option :bar, default: proc { 1 }
|
27
|
-
option :baz, optional: true
|
28
|
-
option :qux, proc(&:to_s), as: :quxx
|
29
|
-
end
|
30
|
-
end
|
31
|
-
|
32
|
-
let(:instance) { Test::Foo.new(foo: :FOO, qux: :QUX) }
|
33
|
-
|
34
|
-
it 'collects coerced and renamed options with default values' do
|
35
|
-
expect(subject).to eq({ foo: :FOO, bar: 1, quxx: 'QUX' })
|
36
|
-
end
|
37
|
-
end
|
38
|
-
end
|
@@ -1,25 +0,0 @@
|
|
1
|
-
describe 'coercion of nil' do
|
2
|
-
before do
|
3
|
-
class Test::Foo
|
4
|
-
extend Dry::Initializer
|
5
|
-
param :bar, proc(&:to_i)
|
6
|
-
end
|
7
|
-
|
8
|
-
class Test::Baz
|
9
|
-
include Dry::Initializer.define -> do
|
10
|
-
param :qux, proc(&:to_i)
|
11
|
-
end
|
12
|
-
end
|
13
|
-
end
|
14
|
-
|
15
|
-
let(:foo) { Test::Foo.new(nil) }
|
16
|
-
let(:baz) { Test::Baz.new(nil) }
|
17
|
-
|
18
|
-
it 'works with extend syntax' do
|
19
|
-
expect(foo.bar).to eq 0
|
20
|
-
end
|
21
|
-
|
22
|
-
it 'works with include syntax' do
|
23
|
-
expect(baz.qux).to eq 0
|
24
|
-
end
|
25
|
-
end
|
@@ -1,35 +0,0 @@
|
|
1
|
-
describe 'custom dispatchers' do
|
2
|
-
subject { Test::Foo.new '123' }
|
3
|
-
|
4
|
-
before do
|
5
|
-
dispatcher = ->(op) { op[:integer] ? op.merge(type: proc(&:to_i)) : op }
|
6
|
-
Dry::Initializer::Dispatchers << dispatcher
|
7
|
-
end
|
8
|
-
|
9
|
-
context 'with extend syntax' do
|
10
|
-
before do
|
11
|
-
class Test::Foo
|
12
|
-
extend Dry::Initializer
|
13
|
-
param :id, integer: true
|
14
|
-
end
|
15
|
-
end
|
16
|
-
|
17
|
-
it 'adds syntax sugar' do
|
18
|
-
expect(subject.id).to eq 123
|
19
|
-
end
|
20
|
-
end
|
21
|
-
|
22
|
-
context 'with include syntax' do
|
23
|
-
before do
|
24
|
-
class Test::Foo
|
25
|
-
include Dry::Initializer.define -> do
|
26
|
-
param :id, integer: true
|
27
|
-
end
|
28
|
-
end
|
29
|
-
end
|
30
|
-
|
31
|
-
it 'adds syntax sugar' do
|
32
|
-
expect(subject.id).to eq 123
|
33
|
-
end
|
34
|
-
end
|
35
|
-
end
|
@@ -1,30 +0,0 @@
|
|
1
|
-
describe 'custom initializer' do
|
2
|
-
before do
|
3
|
-
class Test::Foo
|
4
|
-
extend Dry::Initializer
|
5
|
-
|
6
|
-
param :bar
|
7
|
-
|
8
|
-
def initialize(*args)
|
9
|
-
super
|
10
|
-
@bar *= 3
|
11
|
-
end
|
12
|
-
end
|
13
|
-
|
14
|
-
class Test::Baz < Test::Foo
|
15
|
-
param :qux
|
16
|
-
|
17
|
-
def initialize(*args)
|
18
|
-
super
|
19
|
-
@qux += 1
|
20
|
-
end
|
21
|
-
end
|
22
|
-
end
|
23
|
-
|
24
|
-
it 'reloads the initializer' do
|
25
|
-
baz = Test::Baz.new(5, 5)
|
26
|
-
|
27
|
-
expect(baz.bar).to eq 15 # 5 * 3
|
28
|
-
expect(baz.qux).to eq 6 # 5 + 1
|
29
|
-
end
|
30
|
-
end
|
data/spec/default_values_spec.rb
DELETED
@@ -1,83 +0,0 @@
|
|
1
|
-
describe 'default values' do
|
2
|
-
before do
|
3
|
-
class Test::Foo
|
4
|
-
extend Dry::Initializer
|
5
|
-
|
6
|
-
param :foo, default: proc { :FOO }
|
7
|
-
param :bar, default: proc { :BAR }
|
8
|
-
option :baz, default: -> { :BAZ }
|
9
|
-
option :qux, default: proc { foo }
|
10
|
-
option :mox, default: -> { default_mox }
|
11
|
-
|
12
|
-
private
|
13
|
-
|
14
|
-
def default_mox
|
15
|
-
:MOX
|
16
|
-
end
|
17
|
-
end
|
18
|
-
end
|
19
|
-
|
20
|
-
it 'instantiate arguments' do
|
21
|
-
subject = Test::Foo.new(1, 2, baz: 3, qux: 4)
|
22
|
-
|
23
|
-
expect(subject.foo).to eql 1
|
24
|
-
expect(subject.bar).to eql 2
|
25
|
-
expect(subject.baz).to eql 3
|
26
|
-
expect(subject.qux).to eql 4
|
27
|
-
end
|
28
|
-
|
29
|
-
it 'applies default values' do
|
30
|
-
subject = Test::Foo.new
|
31
|
-
|
32
|
-
expect(subject.foo).to eql :FOO
|
33
|
-
expect(subject.bar).to eql :BAR
|
34
|
-
expect(subject.baz).to eql :BAZ
|
35
|
-
expect(subject.qux).to eql :FOO
|
36
|
-
end
|
37
|
-
|
38
|
-
it 'applies default values partially' do
|
39
|
-
subject = Test::Foo.new 1, baz: 3
|
40
|
-
|
41
|
-
expect(subject.foo).to eql 1
|
42
|
-
expect(subject.bar).to eql :BAR
|
43
|
-
expect(subject.baz).to eql 3
|
44
|
-
expect(subject.qux).to eql 1
|
45
|
-
end
|
46
|
-
|
47
|
-
it 'applies default values from private methods' do
|
48
|
-
subject = Test::Foo.new
|
49
|
-
expect(subject.mox).to eql :MOX
|
50
|
-
end
|
51
|
-
|
52
|
-
describe 'when the last param has a default and there are no options' do
|
53
|
-
before do
|
54
|
-
class Test::Bar
|
55
|
-
extend Dry::Initializer
|
56
|
-
|
57
|
-
param :foo
|
58
|
-
param :bar, default: proc { {} }
|
59
|
-
end
|
60
|
-
end
|
61
|
-
|
62
|
-
it 'instantiates arguments' do
|
63
|
-
subject = Test::Bar.new(1, 2)
|
64
|
-
|
65
|
-
expect(subject.foo).to eql 1
|
66
|
-
expect(subject.bar).to eql 2
|
67
|
-
end
|
68
|
-
|
69
|
-
it 'applies default values' do
|
70
|
-
subject = Test::Bar.new(1)
|
71
|
-
|
72
|
-
expect(subject.foo).to eql 1
|
73
|
-
expect(subject.bar).to eql({})
|
74
|
-
end
|
75
|
-
|
76
|
-
it 'instantiates arguments also if the last is an hash' do
|
77
|
-
subject = Test::Bar.new(1, { baz: 2, qux: 3 })
|
78
|
-
|
79
|
-
expect(subject.foo).to eql 1
|
80
|
-
expect(subject.bar).to eql({ baz: 2, qux: 3 })
|
81
|
-
end
|
82
|
-
end
|
83
|
-
end
|
data/spec/definition_spec.rb
DELETED
@@ -1,111 +0,0 @@
|
|
1
|
-
describe 'definition' do
|
2
|
-
shared_examples :initializer do |in_context|
|
3
|
-
subject { Test::Foo.new(1, bar: 2) }
|
4
|
-
|
5
|
-
it "sets variables when defined via `#{in_context}`" do
|
6
|
-
expect(subject.instance_variable_get(:@foo)).to eql 1
|
7
|
-
expect(subject.instance_variable_get(:@bar)).to eql 2
|
8
|
-
end
|
9
|
-
end
|
10
|
-
|
11
|
-
it_behaves_like :initializer, 'extend Dry::Initializer' do
|
12
|
-
before do
|
13
|
-
class Test::Foo
|
14
|
-
extend Dry::Initializer
|
15
|
-
param :foo
|
16
|
-
option :bar
|
17
|
-
end
|
18
|
-
end
|
19
|
-
|
20
|
-
it 'preservers definition params' do
|
21
|
-
params = Test::Foo.dry_initializer.params.map do |definition|
|
22
|
-
[definition.source, definition.options]
|
23
|
-
end
|
24
|
-
|
25
|
-
expect(params).to eq [
|
26
|
-
[:foo, { as: :foo, reader: :public, optional: false }]
|
27
|
-
]
|
28
|
-
end
|
29
|
-
|
30
|
-
it 'preservers definition options' do
|
31
|
-
options = Test::Foo.dry_initializer.options.map do |definition|
|
32
|
-
[definition.source, definition.options]
|
33
|
-
end
|
34
|
-
|
35
|
-
expect(options).to eq [
|
36
|
-
[:bar, { as: :bar, reader: :public, optional: false }]
|
37
|
-
]
|
38
|
-
end
|
39
|
-
end
|
40
|
-
|
41
|
-
it_behaves_like :initializer, 'extend Dry::Initializer' do
|
42
|
-
before do
|
43
|
-
class Test::Foo
|
44
|
-
extend Dry::Initializer
|
45
|
-
param :foo
|
46
|
-
option :bar
|
47
|
-
end
|
48
|
-
end
|
49
|
-
end
|
50
|
-
|
51
|
-
it_behaves_like :initializer, 'extend Dry::Initializer[undefined: false]' do
|
52
|
-
before do
|
53
|
-
class Test::Foo
|
54
|
-
extend Dry::Initializer[undefined: false]
|
55
|
-
param :foo
|
56
|
-
option :bar
|
57
|
-
end
|
58
|
-
end
|
59
|
-
end
|
60
|
-
|
61
|
-
it_behaves_like :initializer, 'include Dry::Initializer with block' do
|
62
|
-
before do
|
63
|
-
class Test::Foo
|
64
|
-
include(
|
65
|
-
Dry::Initializer.define do
|
66
|
-
param :foo
|
67
|
-
option :bar
|
68
|
-
end
|
69
|
-
)
|
70
|
-
end
|
71
|
-
end
|
72
|
-
end
|
73
|
-
|
74
|
-
it_behaves_like :initializer, 'include Dry::Initializer with lambda' do
|
75
|
-
before do
|
76
|
-
class Test::Foo
|
77
|
-
include Dry::Initializer.define -> do
|
78
|
-
param :foo
|
79
|
-
option :bar
|
80
|
-
end
|
81
|
-
end
|
82
|
-
end
|
83
|
-
end
|
84
|
-
|
85
|
-
it_behaves_like :initializer, 'include Dry::Initializer[undefined: false]' do
|
86
|
-
before do
|
87
|
-
class Test::Foo
|
88
|
-
include(
|
89
|
-
Dry::Initializer[undefined: false].define do
|
90
|
-
param :foo
|
91
|
-
option :bar
|
92
|
-
end
|
93
|
-
)
|
94
|
-
end
|
95
|
-
end
|
96
|
-
end
|
97
|
-
|
98
|
-
# @deprecated
|
99
|
-
it_behaves_like :initializer, 'include Dry::Initializer::Mixin' do
|
100
|
-
before do
|
101
|
-
class Test::Foo
|
102
|
-
include(
|
103
|
-
Dry::Initializer::Mixin.define do
|
104
|
-
param :foo
|
105
|
-
option :bar
|
106
|
-
end
|
107
|
-
)
|
108
|
-
end
|
109
|
-
end
|
110
|
-
end
|
111
|
-
end
|
data/spec/list_type_spec.rb
DELETED
@@ -1,32 +0,0 @@
|
|
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
DELETED
@@ -1,14 +0,0 @@
|
|
1
|
-
describe 'missed default values' do
|
2
|
-
subject do
|
3
|
-
class Test::Foo
|
4
|
-
extend Dry::Initializer
|
5
|
-
|
6
|
-
param :foo, default: proc { :FOO }
|
7
|
-
param :bar, required: true
|
8
|
-
end
|
9
|
-
end
|
10
|
-
|
11
|
-
it 'raises SyntaxError' do
|
12
|
-
expect { subject }.to raise_error SyntaxError, /bar/
|
13
|
-
end
|
14
|
-
end
|
data/spec/nested_type_spec.rb
DELETED
@@ -1,48 +0,0 @@
|
|
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
DELETED
@@ -1,71 +0,0 @@
|
|
1
|
-
describe 'optional value' do
|
2
|
-
context 'when has no default value' do
|
3
|
-
before do
|
4
|
-
class Test::Foo
|
5
|
-
extend Dry::Initializer
|
6
|
-
|
7
|
-
param :foo
|
8
|
-
param :bar, optional: true
|
9
|
-
end
|
10
|
-
end
|
11
|
-
|
12
|
-
it 'quacks like nil' do
|
13
|
-
subject = Test::Foo.new(1)
|
14
|
-
|
15
|
-
expect(subject.bar).to eq nil
|
16
|
-
end
|
17
|
-
|
18
|
-
it 'keeps info about been UNDEFINED' do
|
19
|
-
subject = Test::Foo.new(1)
|
20
|
-
|
21
|
-
expect(subject.instance_variable_get(:@bar))
|
22
|
-
.to eq Dry::Initializer::UNDEFINED
|
23
|
-
end
|
24
|
-
|
25
|
-
it 'can be set explicitly' do
|
26
|
-
subject = Test::Foo.new(1, 'qux')
|
27
|
-
|
28
|
-
expect(subject.bar).to eq 'qux'
|
29
|
-
end
|
30
|
-
end
|
31
|
-
|
32
|
-
context 'with undefined: false' do
|
33
|
-
before do
|
34
|
-
class Test::Foo
|
35
|
-
extend Dry::Initializer[undefined: false]
|
36
|
-
|
37
|
-
param :foo
|
38
|
-
param :bar, optional: true
|
39
|
-
end
|
40
|
-
end
|
41
|
-
|
42
|
-
it 'sets undefined values to nil' do
|
43
|
-
subject = Test::Foo.new(1)
|
44
|
-
|
45
|
-
expect(subject.instance_variable_get(:@bar)).to be_nil
|
46
|
-
end
|
47
|
-
end
|
48
|
-
|
49
|
-
context 'when has a default value' do
|
50
|
-
before do
|
51
|
-
class Test::Foo
|
52
|
-
extend Dry::Initializer
|
53
|
-
|
54
|
-
param :foo
|
55
|
-
param :bar, optional: true, default: proc { 'baz' }
|
56
|
-
end
|
57
|
-
end
|
58
|
-
|
59
|
-
it 'is takes default value' do
|
60
|
-
subject = Test::Foo.new(1)
|
61
|
-
|
62
|
-
expect(subject.bar).to eq 'baz'
|
63
|
-
end
|
64
|
-
|
65
|
-
it 'can be set explicitly' do
|
66
|
-
subject = Test::Foo.new(1, 'qux')
|
67
|
-
|
68
|
-
expect(subject.bar).to eq 'qux'
|
69
|
-
end
|
70
|
-
end
|
71
|
-
end
|