dry-initializer 3.0.3 → 3.0.4
Sign up to get free protection for your applications and to get access to all the features.
- 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
@@ -1,22 +0,0 @@
|
|
1
|
-
describe Dry::Initializer, '.dry_initializer.public_attributes' do
|
2
|
-
subject { instance.class.dry_initializer.public_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), desc: 'a weird parameter'
|
9
|
-
option :moo, optional: true
|
10
|
-
option :bar, default: proc { 1 }, reader: false
|
11
|
-
option :baz, optional: true, reader: :protected
|
12
|
-
option :qux, proc(&:to_s), as: :quxx, reader: :private
|
13
|
-
end
|
14
|
-
end
|
15
|
-
|
16
|
-
let(:instance) { Test::Foo.new(:FOO, bar: :BAR, baz: :BAZ, qux: :QUX) }
|
17
|
-
|
18
|
-
it 'collects public options only' do
|
19
|
-
expect(subject).to eq({ foo: 'FOO', moo: nil })
|
20
|
-
end
|
21
|
-
end
|
22
|
-
end
|
data/spec/reader_spec.rb
DELETED
@@ -1,87 +0,0 @@
|
|
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
|
-
expect(subject).not_to respond_to :foo
|
5
|
-
expect(subject).not_to respond_to :bar
|
6
|
-
end
|
7
|
-
end
|
8
|
-
|
9
|
-
context 'with reader: :public or no reader: option' do
|
10
|
-
subject do
|
11
|
-
class Test::Foo
|
12
|
-
extend Dry::Initializer
|
13
|
-
|
14
|
-
param :foo
|
15
|
-
param :foo2, reader: :public
|
16
|
-
option :bar
|
17
|
-
option :bar2, reader: :public
|
18
|
-
end
|
19
|
-
|
20
|
-
Test::Foo.new 1, 2, bar: 3, bar2: 4
|
21
|
-
end
|
22
|
-
|
23
|
-
it 'defines a public attr_reader by default' do
|
24
|
-
expect(subject).to respond_to(:foo, :foo2)
|
25
|
-
expect(subject).to respond_to :bar
|
26
|
-
expect(subject).to respond_to :bar2
|
27
|
-
end
|
28
|
-
end
|
29
|
-
|
30
|
-
context 'with reader: false' do
|
31
|
-
before do
|
32
|
-
class Test::Foo
|
33
|
-
extend Dry::Initializer
|
34
|
-
|
35
|
-
param :foo, reader: false
|
36
|
-
option :bar, reader: false
|
37
|
-
end
|
38
|
-
end
|
39
|
-
|
40
|
-
subject { Test::Foo.new 1, bar: 2 }
|
41
|
-
|
42
|
-
it_behaves_like 'it has no public attr_reader'
|
43
|
-
|
44
|
-
it 'keeps assigning variables' do
|
45
|
-
expect(subject.instance_variable_get(:@foo)).to eql 1
|
46
|
-
expect(subject.instance_variable_get(:@bar)).to eql 2
|
47
|
-
end
|
48
|
-
end
|
49
|
-
|
50
|
-
context 'with reader: :private' do
|
51
|
-
before do
|
52
|
-
class Test::Foo
|
53
|
-
extend Dry::Initializer
|
54
|
-
|
55
|
-
param :foo, reader: :private
|
56
|
-
option :bar, reader: :private
|
57
|
-
end
|
58
|
-
end
|
59
|
-
|
60
|
-
subject { Test::Foo.new 1, bar: 2 }
|
61
|
-
|
62
|
-
it_behaves_like 'it has no public attr_reader'
|
63
|
-
|
64
|
-
it 'adds a private attr_reader' do
|
65
|
-
expect(subject.send(:foo)).to eql 1
|
66
|
-
expect(subject.send(:bar)).to eql 2
|
67
|
-
end
|
68
|
-
end
|
69
|
-
|
70
|
-
context 'with reader: :protected' do
|
71
|
-
subject do
|
72
|
-
class Test::Foo
|
73
|
-
extend Dry::Initializer
|
74
|
-
|
75
|
-
param :foo, reader: :protected
|
76
|
-
option :bar, reader: :protected
|
77
|
-
end
|
78
|
-
|
79
|
-
Test::Foo.new 1, bar: 2
|
80
|
-
end
|
81
|
-
|
82
|
-
it 'adds a protected attr_reader' do
|
83
|
-
protected_instance_methods = subject.class.protected_instance_methods
|
84
|
-
expect(protected_instance_methods).to match_array(%i[foo bar])
|
85
|
-
end
|
86
|
-
end
|
87
|
-
end
|
@@ -1,69 +0,0 @@
|
|
1
|
-
describe 'repetitive definitions' do
|
2
|
-
subject { Test::Foo.new }
|
3
|
-
|
4
|
-
context 'of params' do
|
5
|
-
before do
|
6
|
-
class Test::Foo
|
7
|
-
extend Dry::Initializer
|
8
|
-
|
9
|
-
param :foo, default: proc { 0 }
|
10
|
-
param :bar, default: proc { 1 }
|
11
|
-
param :foo, default: proc { 2 }
|
12
|
-
end
|
13
|
-
end
|
14
|
-
|
15
|
-
it 'reloads the attribute' do
|
16
|
-
expect(subject.foo).to eq 2
|
17
|
-
end
|
18
|
-
end
|
19
|
-
|
20
|
-
context 'of options' do
|
21
|
-
before do
|
22
|
-
class Test::Foo
|
23
|
-
extend Dry::Initializer
|
24
|
-
|
25
|
-
option :foo, default: proc { 0 }
|
26
|
-
option :bar, default: proc { 1 }
|
27
|
-
option :foo, default: proc { 2 }
|
28
|
-
end
|
29
|
-
end
|
30
|
-
|
31
|
-
it 'reloads the attribute' do
|
32
|
-
expect(subject.foo).to eq 2
|
33
|
-
end
|
34
|
-
end
|
35
|
-
|
36
|
-
context 'of param and option' do
|
37
|
-
before do
|
38
|
-
class Test::Foo
|
39
|
-
extend Dry::Initializer
|
40
|
-
|
41
|
-
param :foo, default: proc { 0 }
|
42
|
-
option :bar, default: proc { 1 }
|
43
|
-
option :foo, default: proc { 2 }
|
44
|
-
end
|
45
|
-
end
|
46
|
-
|
47
|
-
it 'reloads the attribute' do
|
48
|
-
expect(subject.foo).to eq 2
|
49
|
-
end
|
50
|
-
end
|
51
|
-
|
52
|
-
context 'of optional param and option' do
|
53
|
-
before do
|
54
|
-
class Test::Foo
|
55
|
-
extend Dry::Initializer
|
56
|
-
|
57
|
-
param :baz, optional: true, as: :foo
|
58
|
-
option :bar, optional: true
|
59
|
-
option :foo, optional: true
|
60
|
-
end
|
61
|
-
end
|
62
|
-
|
63
|
-
it 'allows various assignments' do
|
64
|
-
expect(Test::Foo.new(1).foo).to eq 1
|
65
|
-
expect(Test::Foo.new(foo: 2).foo).to eq 2
|
66
|
-
expect(Test::Foo.new(1, foo: 2).foo).to eq 2
|
67
|
-
end
|
68
|
-
end
|
69
|
-
end
|
@@ -1,41 +0,0 @@
|
|
1
|
-
describe 'attribute with several assignments' do
|
2
|
-
before do
|
3
|
-
class Test::Foo
|
4
|
-
extend Dry::Initializer
|
5
|
-
|
6
|
-
option :bar, proc(&:to_s), optional: true
|
7
|
-
option :"some foo", as: :bar, optional: true
|
8
|
-
end
|
9
|
-
end
|
10
|
-
|
11
|
-
context 'when not defined' do
|
12
|
-
subject { Test::Foo.new }
|
13
|
-
|
14
|
-
it 'is left undefined' do
|
15
|
-
expect(subject.bar).to be_nil
|
16
|
-
expect(subject.instance_variable_get :@bar)
|
17
|
-
.to eq Dry::Initializer::UNDEFINED
|
18
|
-
end
|
19
|
-
end
|
20
|
-
|
21
|
-
context 'when set directly' do
|
22
|
-
subject { Test::Foo.new bar: :BAZ }
|
23
|
-
|
24
|
-
it 'sets the attribute' do
|
25
|
-
expect(subject.bar).to eq 'BAZ'
|
26
|
-
end
|
27
|
-
end
|
28
|
-
|
29
|
-
context 'when renamed' do
|
30
|
-
subject { Test::Foo.new "some foo": :BAZ }
|
31
|
-
|
32
|
-
it 'renames the attribute' do
|
33
|
-
expect(subject.bar).to eq :BAZ
|
34
|
-
expect(subject).not_to respond_to :foo
|
35
|
-
end
|
36
|
-
|
37
|
-
it 'renames the variable' do
|
38
|
-
expect(subject.instance_variable_get(:@bar)).to eq :BAZ
|
39
|
-
end
|
40
|
-
end
|
41
|
-
end
|
data/spec/spec_helper.rb
DELETED
@@ -1,24 +0,0 @@
|
|
1
|
-
require_relative 'support/coverage'
|
2
|
-
|
3
|
-
require 'dry/initializer'
|
4
|
-
|
5
|
-
begin
|
6
|
-
require 'pry'
|
7
|
-
rescue LoadError
|
8
|
-
nil
|
9
|
-
end
|
10
|
-
|
11
|
-
RSpec.configure do |config|
|
12
|
-
config.order = :random
|
13
|
-
config.filter_run focus: true
|
14
|
-
config.run_all_when_everything_filtered = true
|
15
|
-
|
16
|
-
# Prepare the Test namespace for constants defined in specs
|
17
|
-
config.around(:each) do |example|
|
18
|
-
Test = Class.new(Module)
|
19
|
-
example.run
|
20
|
-
Object.send :remove_const, :Test
|
21
|
-
end
|
22
|
-
|
23
|
-
config.warnings = true
|
24
|
-
end
|
data/spec/subclassing_spec.rb
DELETED
@@ -1,49 +0,0 @@
|
|
1
|
-
describe 'subclassing' do
|
2
|
-
before do
|
3
|
-
class Test::Foo
|
4
|
-
extend Dry::Initializer[undefined: false]
|
5
|
-
param :foo
|
6
|
-
option :bar
|
7
|
-
end
|
8
|
-
|
9
|
-
class Test::Bar < Test::Foo
|
10
|
-
param :baz
|
11
|
-
option :qux
|
12
|
-
end
|
13
|
-
end
|
14
|
-
|
15
|
-
let(:instance_of_superclass) do
|
16
|
-
Test::Foo.new 1, bar: 3
|
17
|
-
end
|
18
|
-
|
19
|
-
let(:instance_of_subclass) do
|
20
|
-
Test::Bar.new 1, 2, bar: 3, qux: 4
|
21
|
-
end
|
22
|
-
|
23
|
-
it 'preserves null definition' do
|
24
|
-
expect(Test::Foo.dry_initializer.null).to be_nil
|
25
|
-
expect(Test::Bar.dry_initializer.null).to be_nil
|
26
|
-
end
|
27
|
-
|
28
|
-
it 'preserves definitions made in the superclass' do
|
29
|
-
expect(instance_of_subclass.foo).to eql 1
|
30
|
-
expect(instance_of_subclass.baz).to eql 2
|
31
|
-
expect(instance_of_subclass.bar).to eql 3
|
32
|
-
expect(instance_of_subclass.qux).to eql 4
|
33
|
-
end
|
34
|
-
|
35
|
-
it 'does not pollute superclass with definitions from subclass' do
|
36
|
-
expect(instance_of_superclass).not_to respond_to :baz
|
37
|
-
expect(instance_of_superclass).not_to respond_to :qux
|
38
|
-
end
|
39
|
-
|
40
|
-
it 'calls .inherited hook added by other mixin' do
|
41
|
-
called = false
|
42
|
-
mixin = Module.new { define_method(:inherited) { |_| called = true } }
|
43
|
-
|
44
|
-
base = Class.new { extend mixin; extend Dry::Initializer }
|
45
|
-
Class.new(base)
|
46
|
-
|
47
|
-
expect(called).to be true
|
48
|
-
end
|
49
|
-
end
|
data/spec/support/coverage.rb
DELETED
data/spec/support/warnings.rb
DELETED
data/spec/type_argument_spec.rb
DELETED
@@ -1,35 +0,0 @@
|
|
1
|
-
require 'dry-types'
|
2
|
-
|
3
|
-
describe 'type argument' do
|
4
|
-
before do
|
5
|
-
class Test::Foo
|
6
|
-
extend Dry::Initializer
|
7
|
-
param :foo, Dry::Types['strict.string']
|
8
|
-
option :bar, Dry::Types['strict.string']
|
9
|
-
end
|
10
|
-
end
|
11
|
-
|
12
|
-
context 'in case of param mismatch' do
|
13
|
-
subject { Test::Foo.new 1, bar: '2' }
|
14
|
-
|
15
|
-
it 'raises TypeError' do
|
16
|
-
expect { subject }.to raise_error Dry::Types::ConstraintError, /1/
|
17
|
-
end
|
18
|
-
end
|
19
|
-
|
20
|
-
context 'in case of option mismatch' do
|
21
|
-
subject { Test::Foo.new '1', bar: 2 }
|
22
|
-
|
23
|
-
it 'raises TypeError' do
|
24
|
-
expect { subject }.to raise_error Dry::Types::ConstraintError, /2/
|
25
|
-
end
|
26
|
-
end
|
27
|
-
|
28
|
-
context 'in case of match' do
|
29
|
-
subject { Test::Foo.new '1', bar: '2' }
|
30
|
-
|
31
|
-
it 'completes the initialization' do
|
32
|
-
expect { subject }.not_to raise_error
|
33
|
-
end
|
34
|
-
end
|
35
|
-
end
|
@@ -1,96 +0,0 @@
|
|
1
|
-
require 'dry-types'
|
2
|
-
|
3
|
-
describe 'type constraint' do
|
4
|
-
context 'by a proc with 1 argument' do
|
5
|
-
before do
|
6
|
-
class Test::Foo
|
7
|
-
extend Dry::Initializer
|
8
|
-
param :__foo__, proc(&:to_s), optional: true
|
9
|
-
end
|
10
|
-
end
|
11
|
-
|
12
|
-
subject { Test::Foo.new :foo }
|
13
|
-
|
14
|
-
it 'coerces a value' do
|
15
|
-
expect(subject.__foo__).to eq 'foo'
|
16
|
-
end
|
17
|
-
end
|
18
|
-
|
19
|
-
context 'by a proc with 2 arguments' do
|
20
|
-
before do
|
21
|
-
class Test::Foo
|
22
|
-
extend Dry::Initializer
|
23
|
-
param :foo, proc { |val, obj| "#{obj.hash}:#{val}" }, optional: true
|
24
|
-
end
|
25
|
-
end
|
26
|
-
|
27
|
-
subject { Test::Foo.new :foo }
|
28
|
-
|
29
|
-
it 'coerces a value with self as a second argument' do
|
30
|
-
expect(subject.foo).to eq "#{subject.hash}:foo"
|
31
|
-
end
|
32
|
-
end
|
33
|
-
|
34
|
-
context 'by dry-type' do
|
35
|
-
before do
|
36
|
-
constraint = self.constraint
|
37
|
-
|
38
|
-
Test::Foo = Class.new do
|
39
|
-
extend Dry::Initializer
|
40
|
-
param :foo, constraint, optional: true
|
41
|
-
end
|
42
|
-
end
|
43
|
-
|
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 }
|
49
|
-
|
50
|
-
it 'raises ArgumentError' do
|
51
|
-
expect { subject }.to raise_error Dry::Types::ConstraintError, /1/
|
52
|
-
end
|
53
|
-
end
|
54
|
-
|
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 }
|
65
|
-
|
66
|
-
it 'not applicable to Dry::Initializer::UNDEFINED' do
|
67
|
-
expect(subject.instance_variable_get(:@foo))
|
68
|
-
.to eq Dry::Initializer::UNDEFINED
|
69
|
-
end
|
70
|
-
end
|
71
|
-
end
|
72
|
-
|
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'] }
|
78
|
-
|
79
|
-
it 'completes the initialization' do
|
80
|
-
expect { subject }.not_to raise_error
|
81
|
-
end
|
82
|
-
end
|
83
|
-
end
|
84
|
-
end
|
85
|
-
|
86
|
-
context 'by invalid constraint' do
|
87
|
-
it 'raises ArgumentError' do
|
88
|
-
expect do
|
89
|
-
class Test::Foo
|
90
|
-
extend Dry::Initializer
|
91
|
-
param :foo, type: String
|
92
|
-
end
|
93
|
-
end.to raise_error(ArgumentError)
|
94
|
-
end
|
95
|
-
end
|
96
|
-
end
|