dry-initializer 3.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (69) hide show
  1. checksums.yaml +7 -0
  2. data/.codeclimate.yml +23 -0
  3. data/.gitignore +10 -0
  4. data/.rspec +4 -0
  5. data/.rubocop.yml +51 -0
  6. data/.travis.yml +28 -0
  7. data/CHANGELOG.md +883 -0
  8. data/Gemfile +29 -0
  9. data/Guardfile +5 -0
  10. data/LICENSE.txt +21 -0
  11. data/README.md +90 -0
  12. data/Rakefile +8 -0
  13. data/benchmarks/compare_several_defaults.rb +82 -0
  14. data/benchmarks/plain_options.rb +63 -0
  15. data/benchmarks/plain_params.rb +84 -0
  16. data/benchmarks/with_coercion.rb +71 -0
  17. data/benchmarks/with_defaults.rb +66 -0
  18. data/benchmarks/with_defaults_and_coercion.rb +59 -0
  19. data/dry-initializer.gemspec +20 -0
  20. data/lib/dry-initializer.rb +1 -0
  21. data/lib/dry/initializer.rb +61 -0
  22. data/lib/dry/initializer/builders.rb +7 -0
  23. data/lib/dry/initializer/builders/attribute.rb +81 -0
  24. data/lib/dry/initializer/builders/initializer.rb +61 -0
  25. data/lib/dry/initializer/builders/reader.rb +50 -0
  26. data/lib/dry/initializer/builders/signature.rb +32 -0
  27. data/lib/dry/initializer/config.rb +184 -0
  28. data/lib/dry/initializer/definition.rb +65 -0
  29. data/lib/dry/initializer/dispatchers.rb +112 -0
  30. data/lib/dry/initializer/dispatchers/build_nested_type.rb +59 -0
  31. data/lib/dry/initializer/dispatchers/check_type.rb +43 -0
  32. data/lib/dry/initializer/dispatchers/prepare_default.rb +40 -0
  33. data/lib/dry/initializer/dispatchers/prepare_ivar.rb +12 -0
  34. data/lib/dry/initializer/dispatchers/prepare_optional.rb +13 -0
  35. data/lib/dry/initializer/dispatchers/prepare_reader.rb +30 -0
  36. data/lib/dry/initializer/dispatchers/prepare_source.rb +28 -0
  37. data/lib/dry/initializer/dispatchers/prepare_target.rb +44 -0
  38. data/lib/dry/initializer/dispatchers/unwrap_type.rb +22 -0
  39. data/lib/dry/initializer/dispatchers/wrap_type.rb +27 -0
  40. data/lib/dry/initializer/dsl.rb +43 -0
  41. data/lib/dry/initializer/mixin.rb +15 -0
  42. data/lib/dry/initializer/mixin/local.rb +19 -0
  43. data/lib/dry/initializer/mixin/root.rb +10 -0
  44. data/lib/dry/initializer/struct.rb +40 -0
  45. data/lib/dry/initializer/undefined.rb +2 -0
  46. data/lib/tasks/benchmark.rake +41 -0
  47. data/lib/tasks/profile.rake +78 -0
  48. data/spec/attributes_spec.rb +38 -0
  49. data/spec/coercion_of_nil_spec.rb +25 -0
  50. data/spec/custom_dispatchers_spec.rb +35 -0
  51. data/spec/custom_initializer_spec.rb +30 -0
  52. data/spec/default_values_spec.rb +83 -0
  53. data/spec/definition_spec.rb +111 -0
  54. data/spec/invalid_default_spec.rb +13 -0
  55. data/spec/list_type_spec.rb +32 -0
  56. data/spec/missed_default_spec.rb +14 -0
  57. data/spec/nested_type_spec.rb +44 -0
  58. data/spec/optional_spec.rb +71 -0
  59. data/spec/options_tolerance_spec.rb +11 -0
  60. data/spec/public_attributes_utility_spec.rb +22 -0
  61. data/spec/reader_spec.rb +87 -0
  62. data/spec/repetitive_definitions_spec.rb +69 -0
  63. data/spec/several_assignments_spec.rb +41 -0
  64. data/spec/spec_helper.rb +22 -0
  65. data/spec/subclassing_spec.rb +49 -0
  66. data/spec/type_argument_spec.rb +35 -0
  67. data/spec/type_constraint_spec.rb +78 -0
  68. data/spec/value_coercion_via_dry_types_spec.rb +29 -0
  69. metadata +189 -0
@@ -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
@@ -0,0 +1,30 @@
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
@@ -0,0 +1,83 @@
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
@@ -0,0 +1,111 @@
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
@@ -0,0 +1,13 @@
1
+ describe "invalid default value assignment" do
2
+ subject do
3
+ class Test::Foo
4
+ extend Dry::Initializer
5
+
6
+ param :foo, default: 1
7
+ end
8
+ end
9
+
10
+ it "raises TypeError" do
11
+ expect { subject }.to raise_error TypeError
12
+ end
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
@@ -0,0 +1,14 @@
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
@@ -0,0 +1,44 @@
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
+ end
22
+
23
+ context "with nested and wrapped definitions" do
24
+ before do
25
+ class Test::Xyz
26
+ extend Dry::Initializer
27
+
28
+ param :foo, [], as: :x do
29
+ option :bar, as: :y do
30
+ option :baz, proc(&:to_s), as: :z
31
+ option :qux, as: :w, optional: true
32
+ end
33
+ end
34
+ end
35
+ end
36
+
37
+ it "builds the type" do
38
+ x = subject.x
39
+ expect(x).to be_instance_of Array
40
+
41
+ expect(x.first.y.z).to eq "42"
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,71 @@
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