dry-initializer 0.11.0 → 2.5.0
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/.codeclimate.yml +8 -0
- data/.gitignore +1 -0
- data/.rspec +1 -0
- data/.rubocop.yml +14 -62
- data/.travis.yml +15 -11
- data/CHANGELOG.md +538 -158
- data/Gemfile +2 -2
- data/LICENSE.txt +1 -1
- data/README.md +6 -6
- data/Rakefile +2 -41
- data/benchmarks/{several_defaults.rb → compare_several_defaults.rb} +4 -4
- data/benchmarks/{without_options.rb → plain_options.rb} +21 -10
- data/benchmarks/{params.rb → plain_params.rb} +20 -9
- data/benchmarks/{with_types.rb → with_coercion.rb} +23 -16
- data/benchmarks/with_defaults.rb +19 -8
- data/benchmarks/{with_types_and_defaults.rb → with_defaults_and_coercion.rb} +21 -12
- data/dry-initializer.gemspec +4 -4
- data/lib/dry/initializer/builders/attribute.rb +81 -0
- data/lib/dry/initializer/builders/initializer.rb +61 -0
- data/lib/dry/initializer/builders/reader.rb +50 -0
- data/lib/dry/initializer/builders/signature.rb +32 -0
- data/lib/dry/initializer/builders.rb +7 -0
- data/lib/dry/initializer/config.rb +172 -0
- data/lib/dry/initializer/definition.rb +116 -0
- data/lib/dry/initializer/dispatchers.rb +44 -0
- data/lib/dry/initializer/dsl.rb +43 -0
- data/lib/dry/initializer/mixin/local.rb +19 -0
- data/lib/dry/initializer/mixin/root.rb +10 -0
- data/lib/dry/initializer/mixin.rb +8 -70
- data/lib/dry/initializer.rb +49 -12
- data/lib/tasks/benchmark.rake +41 -0
- data/lib/tasks/profile.rake +78 -0
- data/spec/attributes_spec.rb +38 -0
- data/spec/coercion_of_nil_spec.rb +25 -0
- data/spec/custom_dispatchers_spec.rb +35 -0
- data/spec/custom_initializer_spec.rb +1 -1
- data/spec/default_values_spec.rb +8 -8
- data/spec/definition_spec.rb +107 -0
- data/spec/invalid_default_spec.rb +2 -2
- data/spec/missed_default_spec.rb +3 -3
- data/spec/optional_spec.rb +35 -8
- data/spec/options_tolerance_spec.rb +1 -1
- data/spec/public_attributes_utility_spec.rb +22 -0
- data/spec/reader_spec.rb +11 -11
- data/spec/repetitive_definitions_spec.rb +41 -21
- data/spec/several_assignments_spec.rb +41 -0
- data/spec/spec_helper.rb +7 -0
- data/spec/subclassing_spec.rb +7 -3
- data/spec/type_argument_spec.rb +1 -1
- data/spec/type_constraint_spec.rb +38 -7
- data/spec/value_coercion_via_dry_types_spec.rb +12 -4
- metadata +37 -40
- data/benchmarks/options.rb +0 -54
- data/benchmarks/params_vs_options.rb +0 -35
- data/lib/dry/initializer/builder.rb +0 -100
- data/lib/dry/initializer/errors/default_value_error.rb +0 -6
- data/lib/dry/initializer/errors/order_error.rb +0 -7
- data/lib/dry/initializer/errors/plugin_error.rb +0 -6
- data/lib/dry/initializer/errors/redefinition_error.rb +0 -5
- data/lib/dry/initializer/errors/type_constraint_error.rb +0 -5
- data/lib/dry/initializer/errors.rb +0 -10
- data/lib/dry/initializer/plugins/base.rb +0 -47
- data/lib/dry/initializer/plugins/default_proc.rb +0 -28
- data/lib/dry/initializer/plugins/signature.rb +0 -28
- data/lib/dry/initializer/plugins/type_constraint.rb +0 -21
- data/lib/dry/initializer/plugins/variable_setter.rb +0 -30
- data/lib/dry/initializer/plugins.rb +0 -10
- data/lib/dry/initializer/signature.rb +0 -61
- data/spec/base_spec.rb +0 -21
- data/spec/container_spec.rb +0 -45
- data/spec/default_nil_spec.rb +0 -17
- data/spec/plugin_registry_spec.rb +0 -45
- data/spec/renaming_options_spec.rb +0 -20
@@ -0,0 +1,38 @@
|
|
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
|
@@ -0,0 +1,25 @@
|
|
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
|
@@ -0,0 +1,35 @@
|
|
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
|
data/spec/default_values_spec.rb
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
describe "default values" do
|
2
2
|
before do
|
3
3
|
class Test::Foo
|
4
|
-
extend Dry::Initializer
|
4
|
+
extend Dry::Initializer
|
5
5
|
|
6
6
|
param :foo, default: proc { :FOO }
|
7
7
|
param :bar, default: proc { :BAR }
|
8
|
-
option :baz, default:
|
8
|
+
option :baz, default: -> { :BAZ }
|
9
9
|
option :qux, default: proc { foo }
|
10
|
-
option :mox, default:
|
10
|
+
option :mox, default: -> { default_mox }
|
11
11
|
|
12
12
|
private
|
13
13
|
|
@@ -52,14 +52,14 @@ describe "default values" do
|
|
52
52
|
describe "when the last param has a default and there are no options" do
|
53
53
|
before do
|
54
54
|
class Test::Bar
|
55
|
-
extend Dry::Initializer
|
55
|
+
extend Dry::Initializer
|
56
56
|
|
57
|
-
param
|
58
|
-
param
|
57
|
+
param :foo
|
58
|
+
param :bar, default: proc { {} }
|
59
59
|
end
|
60
60
|
end
|
61
61
|
|
62
|
-
it "
|
62
|
+
it "instantiates arguments" do
|
63
63
|
subject = Test::Bar.new(1, 2)
|
64
64
|
|
65
65
|
expect(subject.foo).to eql 1
|
@@ -73,7 +73,7 @@ describe "default values" do
|
|
73
73
|
expect(subject.bar).to eql({})
|
74
74
|
end
|
75
75
|
|
76
|
-
it "
|
76
|
+
it "instantiates arguments also if the last is an hash" do
|
77
77
|
subject = Test::Bar.new(1, { baz: 2, qux: 3 })
|
78
78
|
|
79
79
|
expect(subject.foo).to eql 1
|
@@ -0,0 +1,107 @@
|
|
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 [[:foo, { as: :foo, reader: :public }]]
|
26
|
+
end
|
27
|
+
|
28
|
+
it "preservers definition options" do
|
29
|
+
options = Test::Foo.dry_initializer.options.map do |definition|
|
30
|
+
[definition.source, definition.options]
|
31
|
+
end
|
32
|
+
|
33
|
+
expect(options).to eq [[:bar, { as: :bar, reader: :public }]]
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
it_behaves_like :initializer, "extend Dry::Initializer" do
|
38
|
+
before do
|
39
|
+
class Test::Foo
|
40
|
+
extend Dry::Initializer
|
41
|
+
param :foo
|
42
|
+
option :bar
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
it_behaves_like :initializer, "extend Dry::Initializer[undefined: false]" do
|
48
|
+
before do
|
49
|
+
class Test::Foo
|
50
|
+
extend Dry::Initializer[undefined: false]
|
51
|
+
param :foo
|
52
|
+
option :bar
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
it_behaves_like :initializer, "include Dry::Initializer with block" do
|
58
|
+
before do
|
59
|
+
class Test::Foo
|
60
|
+
include(
|
61
|
+
Dry::Initializer.define do
|
62
|
+
param :foo
|
63
|
+
option :bar
|
64
|
+
end
|
65
|
+
)
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
it_behaves_like :initializer, "include Dry::Initializer with lambda" do
|
71
|
+
before do
|
72
|
+
class Test::Foo
|
73
|
+
include Dry::Initializer.define -> do
|
74
|
+
param :foo
|
75
|
+
option :bar
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
it_behaves_like :initializer, "include Dry::Initializer[undefined: false]" do
|
82
|
+
before do
|
83
|
+
class Test::Foo
|
84
|
+
include(
|
85
|
+
Dry::Initializer[undefined: false].define do
|
86
|
+
param :foo
|
87
|
+
option :bar
|
88
|
+
end
|
89
|
+
)
|
90
|
+
end
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
# @deprecated
|
95
|
+
it_behaves_like :initializer, "include Dry::Initializer::Mixin" do
|
96
|
+
before do
|
97
|
+
class Test::Foo
|
98
|
+
include(
|
99
|
+
Dry::Initializer::Mixin.define do
|
100
|
+
param :foo
|
101
|
+
option :bar
|
102
|
+
end
|
103
|
+
)
|
104
|
+
end
|
105
|
+
end
|
106
|
+
end
|
107
|
+
end
|
@@ -1,13 +1,13 @@
|
|
1
1
|
describe "invalid default value assignment" do
|
2
2
|
subject do
|
3
3
|
class Test::Foo
|
4
|
-
extend Dry::Initializer
|
4
|
+
extend Dry::Initializer
|
5
5
|
|
6
6
|
param :foo, default: 1
|
7
7
|
end
|
8
8
|
end
|
9
9
|
|
10
10
|
it "raises TypeError" do
|
11
|
-
expect { subject }.to raise_error TypeError
|
11
|
+
expect { subject }.to raise_error TypeError
|
12
12
|
end
|
13
13
|
end
|
data/spec/missed_default_spec.rb
CHANGED
@@ -1,10 +1,10 @@
|
|
1
1
|
describe "missed default values" do
|
2
2
|
subject do
|
3
3
|
class Test::Foo
|
4
|
-
extend Dry::Initializer
|
4
|
+
extend Dry::Initializer
|
5
5
|
|
6
|
-
param :foo, default:
|
7
|
-
param :bar
|
6
|
+
param :foo, default: proc { :FOO }
|
7
|
+
param :bar, required: true
|
8
8
|
end
|
9
9
|
end
|
10
10
|
|
data/spec/optional_spec.rb
CHANGED
@@ -2,32 +2,54 @@ describe "optional value" do
|
|
2
2
|
context "when has no default value" do
|
3
3
|
before do
|
4
4
|
class Test::Foo
|
5
|
-
extend Dry::Initializer
|
5
|
+
extend Dry::Initializer
|
6
6
|
|
7
7
|
param :foo
|
8
8
|
param :bar, optional: true
|
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
|
-
expect(subject.
|
16
|
-
|
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
|
17
23
|
end
|
18
24
|
|
19
25
|
it "can be set explicitly" do
|
20
26
|
subject = Test::Foo.new(1, "qux")
|
21
27
|
|
22
|
-
expect(subject.foo).to eq 1
|
23
28
|
expect(subject.bar).to eq "qux"
|
24
29
|
end
|
25
30
|
end
|
26
31
|
|
27
|
-
context "
|
32
|
+
context "with undefined: false" do
|
28
33
|
before do
|
29
34
|
class Test::Foo
|
30
|
-
extend Dry::Initializer
|
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
|
31
53
|
|
32
54
|
param :foo
|
33
55
|
param :bar, optional: true, default: proc { "baz" }
|
@@ -37,8 +59,13 @@ describe "optional value" do
|
|
37
59
|
it "is takes default value" do
|
38
60
|
subject = Test::Foo.new(1)
|
39
61
|
|
40
|
-
expect(subject.foo).to eq 1
|
41
62
|
expect(subject.bar).to eq "baz"
|
42
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
|
43
70
|
end
|
44
71
|
end
|
@@ -0,0 +1,22 @@
|
|
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
CHANGED
@@ -9,7 +9,7 @@ describe "reader" do
|
|
9
9
|
context "with reader: :public or no reader: option" do
|
10
10
|
subject do
|
11
11
|
class Test::Foo
|
12
|
-
extend Dry::Initializer
|
12
|
+
extend Dry::Initializer
|
13
13
|
|
14
14
|
param :foo
|
15
15
|
param :foo2, reader: :public
|
@@ -28,17 +28,17 @@ describe "reader" do
|
|
28
28
|
end
|
29
29
|
|
30
30
|
context "with reader: false" do
|
31
|
-
|
31
|
+
before do
|
32
32
|
class Test::Foo
|
33
|
-
extend Dry::Initializer
|
33
|
+
extend Dry::Initializer
|
34
34
|
|
35
35
|
param :foo, reader: false
|
36
36
|
option :bar, reader: false
|
37
37
|
end
|
38
|
-
|
39
|
-
Test::Foo.new 1, bar: 2
|
40
38
|
end
|
41
39
|
|
40
|
+
subject { Test::Foo.new 1, bar: 2 }
|
41
|
+
|
42
42
|
it_behaves_like "it has no public attr_reader"
|
43
43
|
|
44
44
|
it "keeps assigning variables" do
|
@@ -48,17 +48,17 @@ describe "reader" do
|
|
48
48
|
end
|
49
49
|
|
50
50
|
context "with reader: :private" do
|
51
|
-
|
51
|
+
before do
|
52
52
|
class Test::Foo
|
53
|
-
extend Dry::Initializer
|
53
|
+
extend Dry::Initializer
|
54
54
|
|
55
55
|
param :foo, reader: :private
|
56
56
|
option :bar, reader: :private
|
57
57
|
end
|
58
|
-
|
59
|
-
Test::Foo.new 1, bar: 2
|
60
58
|
end
|
61
59
|
|
60
|
+
subject { Test::Foo.new 1, bar: 2 }
|
61
|
+
|
62
62
|
it_behaves_like "it has no public attr_reader"
|
63
63
|
|
64
64
|
it "adds a private attr_reader" do
|
@@ -70,7 +70,7 @@ describe "reader" do
|
|
70
70
|
context "with reader: :protected" do
|
71
71
|
subject do
|
72
72
|
class Test::Foo
|
73
|
-
extend Dry::Initializer
|
73
|
+
extend Dry::Initializer
|
74
74
|
|
75
75
|
param :foo, reader: :protected
|
76
76
|
option :bar, reader: :protected
|
@@ -81,7 +81,7 @@ describe "reader" do
|
|
81
81
|
|
82
82
|
it "adds a protected attr_reader" do
|
83
83
|
protected_instance_methods = subject.class.protected_instance_methods
|
84
|
-
expect(protected_instance_methods).to match_array([
|
84
|
+
expect(protected_instance_methods).to match_array(%i[foo bar])
|
85
85
|
end
|
86
86
|
end
|
87
87
|
end
|
@@ -1,49 +1,69 @@
|
|
1
1
|
describe "repetitive definitions" do
|
2
|
+
subject { Test::Foo.new }
|
3
|
+
|
2
4
|
context "of params" do
|
3
|
-
|
5
|
+
before do
|
4
6
|
class Test::Foo
|
5
|
-
extend Dry::Initializer
|
7
|
+
extend Dry::Initializer
|
6
8
|
|
7
|
-
param :foo
|
8
|
-
param :bar
|
9
|
-
param :foo
|
9
|
+
param :foo, default: proc { 0 }
|
10
|
+
param :bar, default: proc { 1 }
|
11
|
+
param :foo, default: proc { 2 }
|
10
12
|
end
|
11
13
|
end
|
12
14
|
|
13
|
-
it "
|
14
|
-
expect
|
15
|
+
it "reloads the attribute" do
|
16
|
+
expect(subject.foo).to eq 2
|
15
17
|
end
|
16
18
|
end
|
17
19
|
|
18
20
|
context "of options" do
|
19
|
-
|
21
|
+
before do
|
20
22
|
class Test::Foo
|
21
|
-
extend Dry::Initializer
|
23
|
+
extend Dry::Initializer
|
22
24
|
|
23
|
-
option :foo
|
24
|
-
option :bar
|
25
|
-
option :foo
|
25
|
+
option :foo, default: proc { 0 }
|
26
|
+
option :bar, default: proc { 1 }
|
27
|
+
option :foo, default: proc { 2 }
|
26
28
|
end
|
27
29
|
end
|
28
30
|
|
29
|
-
it "
|
30
|
-
expect
|
31
|
+
it "reloads the attribute" do
|
32
|
+
expect(subject.foo).to eq 2
|
31
33
|
end
|
32
34
|
end
|
33
35
|
|
34
36
|
context "of param and option" do
|
35
|
-
|
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
|
36
54
|
class Test::Foo
|
37
|
-
extend Dry::Initializer
|
55
|
+
extend Dry::Initializer
|
38
56
|
|
39
|
-
param :foo
|
40
|
-
option :bar
|
41
|
-
option :foo
|
57
|
+
param :baz, optional: true, as: :foo
|
58
|
+
option :bar, optional: true
|
59
|
+
option :foo, optional: true
|
42
60
|
end
|
43
61
|
end
|
44
62
|
|
45
|
-
it "
|
46
|
-
expect
|
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
|
47
67
|
end
|
48
68
|
end
|
49
69
|
end
|
@@ -0,0 +1,41 @@
|
|
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
CHANGED
@@ -1,4 +1,9 @@
|
|
1
1
|
require "dry/initializer"
|
2
|
+
begin
|
3
|
+
require "pry"
|
4
|
+
rescue
|
5
|
+
nil
|
6
|
+
end
|
2
7
|
|
3
8
|
RSpec.configure do |config|
|
4
9
|
config.order = :random
|
@@ -11,4 +16,6 @@ RSpec.configure do |config|
|
|
11
16
|
example.run
|
12
17
|
Object.send :remove_const, :Test
|
13
18
|
end
|
19
|
+
|
20
|
+
config.warnings = true
|
14
21
|
end
|
data/spec/subclassing_spec.rb
CHANGED
@@ -1,8 +1,7 @@
|
|
1
1
|
describe "subclassing" do
|
2
2
|
before do
|
3
3
|
class Test::Foo
|
4
|
-
extend Dry::Initializer
|
5
|
-
|
4
|
+
extend Dry::Initializer[undefined: false]
|
6
5
|
param :foo
|
7
6
|
option :bar
|
8
7
|
end
|
@@ -21,6 +20,11 @@ describe "subclassing" do
|
|
21
20
|
Test::Bar.new 1, 2, bar: 3, qux: 4
|
22
21
|
end
|
23
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
|
+
|
24
28
|
it "preserves definitions made in the superclass" do
|
25
29
|
expect(instance_of_subclass.foo).to eql 1
|
26
30
|
expect(instance_of_subclass.baz).to eql 2
|
@@ -37,7 +41,7 @@ describe "subclassing" do
|
|
37
41
|
called = false
|
38
42
|
mixin = Module.new { define_method(:inherited) { |_| called = true } }
|
39
43
|
|
40
|
-
base = Class.new { extend mixin; extend Dry::Initializer
|
44
|
+
base = Class.new { extend mixin; extend Dry::Initializer }
|
41
45
|
Class.new(base)
|
42
46
|
|
43
47
|
expect(called).to be true
|
data/spec/type_argument_spec.rb
CHANGED