dry-initializer 3.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (88) hide show
  1. checksums.yaml +7 -0
  2. data/.codeclimate.yml +12 -0
  3. data/.github/ISSUE_TEMPLATE/----please-don-t-ask-for-support-via-issues.md +10 -0
  4. data/.github/ISSUE_TEMPLATE/---bug-report.md +34 -0
  5. data/.github/ISSUE_TEMPLATE/---feature-request.md +18 -0
  6. data/.github/workflows/custom_ci.yml +74 -0
  7. data/.github/workflows/docsite.yml +34 -0
  8. data/.github/workflows/sync_configs.yml +34 -0
  9. data/.gitignore +12 -0
  10. data/.rspec +4 -0
  11. data/.rubocop.yml +89 -0
  12. data/CHANGELOG.md +890 -0
  13. data/CODE_OF_CONDUCT.md +13 -0
  14. data/CONTRIBUTING.md +29 -0
  15. data/Gemfile +38 -0
  16. data/Guardfile +5 -0
  17. data/LICENSE +20 -0
  18. data/LICENSE.txt +21 -0
  19. data/README.md +89 -0
  20. data/Rakefile +8 -0
  21. data/benchmarks/compare_several_defaults.rb +82 -0
  22. data/benchmarks/plain_options.rb +63 -0
  23. data/benchmarks/plain_params.rb +84 -0
  24. data/benchmarks/with_coercion.rb +71 -0
  25. data/benchmarks/with_defaults.rb +66 -0
  26. data/benchmarks/with_defaults_and_coercion.rb +59 -0
  27. data/docsite/source/attributes.html.md +106 -0
  28. data/docsite/source/container-version.html.md +39 -0
  29. data/docsite/source/index.html.md +43 -0
  30. data/docsite/source/inheritance.html.md +43 -0
  31. data/docsite/source/optionals-and-defaults.html.md +130 -0
  32. data/docsite/source/options-tolerance.html.md +27 -0
  33. data/docsite/source/params-and-options.html.md +74 -0
  34. data/docsite/source/rails-support.html.md +101 -0
  35. data/docsite/source/readers.html.md +43 -0
  36. data/docsite/source/skip-undefined.html.md +59 -0
  37. data/docsite/source/type-constraints.html.md +160 -0
  38. data/dry-initializer.gemspec +20 -0
  39. data/lib/dry-initializer.rb +1 -0
  40. data/lib/dry/initializer.rb +61 -0
  41. data/lib/dry/initializer/builders.rb +7 -0
  42. data/lib/dry/initializer/builders/attribute.rb +81 -0
  43. data/lib/dry/initializer/builders/initializer.rb +61 -0
  44. data/lib/dry/initializer/builders/reader.rb +50 -0
  45. data/lib/dry/initializer/builders/signature.rb +32 -0
  46. data/lib/dry/initializer/config.rb +184 -0
  47. data/lib/dry/initializer/definition.rb +65 -0
  48. data/lib/dry/initializer/dispatchers.rb +112 -0
  49. data/lib/dry/initializer/dispatchers/build_nested_type.rb +59 -0
  50. data/lib/dry/initializer/dispatchers/check_type.rb +43 -0
  51. data/lib/dry/initializer/dispatchers/prepare_default.rb +40 -0
  52. data/lib/dry/initializer/dispatchers/prepare_ivar.rb +12 -0
  53. data/lib/dry/initializer/dispatchers/prepare_optional.rb +13 -0
  54. data/lib/dry/initializer/dispatchers/prepare_reader.rb +30 -0
  55. data/lib/dry/initializer/dispatchers/prepare_source.rb +28 -0
  56. data/lib/dry/initializer/dispatchers/prepare_target.rb +44 -0
  57. data/lib/dry/initializer/dispatchers/unwrap_type.rb +22 -0
  58. data/lib/dry/initializer/dispatchers/wrap_type.rb +27 -0
  59. data/lib/dry/initializer/dsl.rb +43 -0
  60. data/lib/dry/initializer/mixin.rb +15 -0
  61. data/lib/dry/initializer/mixin/local.rb +19 -0
  62. data/lib/dry/initializer/mixin/root.rb +11 -0
  63. data/lib/dry/initializer/struct.rb +39 -0
  64. data/lib/dry/initializer/undefined.rb +2 -0
  65. data/lib/tasks/benchmark.rake +41 -0
  66. data/lib/tasks/profile.rake +78 -0
  67. data/spec/attributes_spec.rb +38 -0
  68. data/spec/coercion_of_nil_spec.rb +25 -0
  69. data/spec/custom_dispatchers_spec.rb +35 -0
  70. data/spec/custom_initializer_spec.rb +30 -0
  71. data/spec/default_values_spec.rb +83 -0
  72. data/spec/definition_spec.rb +111 -0
  73. data/spec/invalid_default_spec.rb +13 -0
  74. data/spec/list_type_spec.rb +32 -0
  75. data/spec/missed_default_spec.rb +14 -0
  76. data/spec/nested_type_spec.rb +48 -0
  77. data/spec/optional_spec.rb +71 -0
  78. data/spec/options_tolerance_spec.rb +11 -0
  79. data/spec/public_attributes_utility_spec.rb +22 -0
  80. data/spec/reader_spec.rb +87 -0
  81. data/spec/repetitive_definitions_spec.rb +69 -0
  82. data/spec/several_assignments_spec.rb +41 -0
  83. data/spec/spec_helper.rb +29 -0
  84. data/spec/subclassing_spec.rb +49 -0
  85. data/spec/type_argument_spec.rb +35 -0
  86. data/spec/type_constraint_spec.rb +78 -0
  87. data/spec/value_coercion_via_dry_types_spec.rb +29 -0
  88. metadata +209 -0
@@ -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
@@ -0,0 +1,29 @@
1
+ if ENV['COVERAGE'] == 'true'
2
+ require 'simplecov'
3
+ SimpleCov.start do
4
+ add_filter '/spec/'
5
+ end
6
+ end
7
+
8
+ require "dry/initializer"
9
+
10
+ begin
11
+ require "pry"
12
+ rescue LoadError
13
+ nil
14
+ end
15
+
16
+ RSpec.configure do |config|
17
+ config.order = :random
18
+ config.filter_run focus: true
19
+ config.run_all_when_everything_filtered = true
20
+
21
+ # Prepare the Test namespace for constants defined in specs
22
+ config.around(:each) do |example|
23
+ Test = Class.new(Module)
24
+ example.run
25
+ Object.send :remove_const, :Test
26
+ end
27
+
28
+ config.warnings = true
29
+ end
@@ -0,0 +1,49 @@
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
@@ -0,0 +1,35 @@
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
@@ -0,0 +1,78 @@
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
+ class Test::Foo
37
+ extend Dry::Initializer
38
+ param :foo, Dry::Types["strict.string"], optional: true
39
+ end
40
+ end
41
+
42
+ context "in case of mismatch" do
43
+ subject { Test::Foo.new 1 }
44
+
45
+ it "raises ArgumentError" do
46
+ expect { subject }.to raise_error Dry::Types::ConstraintError, /1/
47
+ end
48
+ end
49
+
50
+ context "in case of match" do
51
+ subject { Test::Foo.new "foo" }
52
+
53
+ it "completes the initialization" do
54
+ expect { subject }.not_to raise_error
55
+ end
56
+ end
57
+
58
+ context "if optional value not set" do
59
+ subject { Test::Foo.new }
60
+
61
+ it "not applicable to Dry::Initializer::UNDEFINED" do
62
+ expect(subject.instance_variable_get(:@foo))
63
+ .to eq Dry::Initializer::UNDEFINED
64
+ end
65
+ end
66
+ end
67
+
68
+ context "by invalid constraint" do
69
+ it "raises ArgumentError" do
70
+ expect do
71
+ class Test::Foo
72
+ extend Dry::Initializer
73
+ param :foo, type: String
74
+ end
75
+ end.to raise_error(ArgumentError)
76
+ end
77
+ end
78
+ end
@@ -0,0 +1,29 @@
1
+ require "dry-types"
2
+
3
+ describe "value coercion via dry-types" do
4
+ before do
5
+ module Test::Types
6
+ include Dry.Types
7
+ end
8
+
9
+ class Test::Foo
10
+ extend Dry::Initializer
11
+
12
+ param :foo, type: Test::Types::Coercible::String
13
+ option :bar, proc(&:to_i), default: proc { "16" }
14
+ end
15
+ end
16
+
17
+ it "coerces assigned values" do
18
+ subject = Test::Foo.new :foo, bar: "13"
19
+
20
+ expect(subject.foo).to eql "foo"
21
+ expect(subject.bar).to eql 13
22
+ end
23
+
24
+ it "coerces defaults as well" do
25
+ subject = Test::Foo.new :foo
26
+
27
+ expect(subject.bar).to eql 16
28
+ end
29
+ end
metadata ADDED
@@ -0,0 +1,209 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dry-initializer
3
+ version: !ruby/object:Gem::Version
4
+ version: 3.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Vladimir Kochnev (marshall-lee)
8
+ - Andrew Kozin (nepalez)
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2019-11-06 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - "~>"
19
+ - !ruby/object:Gem::Version
20
+ version: '3.0'
21
+ type: :development
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - "~>"
26
+ - !ruby/object:Gem::Version
27
+ version: '3.0'
28
+ - !ruby/object:Gem::Dependency
29
+ name: rake
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ">"
33
+ - !ruby/object:Gem::Version
34
+ version: '10'
35
+ type: :development
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">"
40
+ - !ruby/object:Gem::Version
41
+ version: '10'
42
+ - !ruby/object:Gem::Dependency
43
+ name: dry-types
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">"
47
+ - !ruby/object:Gem::Version
48
+ version: 0.5.1
49
+ type: :development
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">"
54
+ - !ruby/object:Gem::Version
55
+ version: 0.5.1
56
+ - !ruby/object:Gem::Dependency
57
+ name: rubocop
58
+ requirement: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - "~>"
61
+ - !ruby/object:Gem::Version
62
+ version: 0.49.0
63
+ type: :development
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - "~>"
68
+ - !ruby/object:Gem::Version
69
+ version: 0.49.0
70
+ description:
71
+ email: andrew.kozin@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files:
75
+ - README.md
76
+ - LICENSE
77
+ - CHANGELOG.md
78
+ files:
79
+ - ".codeclimate.yml"
80
+ - ".github/ISSUE_TEMPLATE/----please-don-t-ask-for-support-via-issues.md"
81
+ - ".github/ISSUE_TEMPLATE/---bug-report.md"
82
+ - ".github/ISSUE_TEMPLATE/---feature-request.md"
83
+ - ".github/workflows/custom_ci.yml"
84
+ - ".github/workflows/docsite.yml"
85
+ - ".github/workflows/sync_configs.yml"
86
+ - ".gitignore"
87
+ - ".rspec"
88
+ - ".rubocop.yml"
89
+ - CHANGELOG.md
90
+ - CODE_OF_CONDUCT.md
91
+ - CONTRIBUTING.md
92
+ - Gemfile
93
+ - Guardfile
94
+ - LICENSE
95
+ - LICENSE.txt
96
+ - README.md
97
+ - Rakefile
98
+ - benchmarks/compare_several_defaults.rb
99
+ - benchmarks/plain_options.rb
100
+ - benchmarks/plain_params.rb
101
+ - benchmarks/with_coercion.rb
102
+ - benchmarks/with_defaults.rb
103
+ - benchmarks/with_defaults_and_coercion.rb
104
+ - docsite/source/attributes.html.md
105
+ - docsite/source/container-version.html.md
106
+ - docsite/source/index.html.md
107
+ - docsite/source/inheritance.html.md
108
+ - docsite/source/optionals-and-defaults.html.md
109
+ - docsite/source/options-tolerance.html.md
110
+ - docsite/source/params-and-options.html.md
111
+ - docsite/source/rails-support.html.md
112
+ - docsite/source/readers.html.md
113
+ - docsite/source/skip-undefined.html.md
114
+ - docsite/source/type-constraints.html.md
115
+ - dry-initializer.gemspec
116
+ - lib/dry-initializer.rb
117
+ - lib/dry/initializer.rb
118
+ - lib/dry/initializer/builders.rb
119
+ - lib/dry/initializer/builders/attribute.rb
120
+ - lib/dry/initializer/builders/initializer.rb
121
+ - lib/dry/initializer/builders/reader.rb
122
+ - lib/dry/initializer/builders/signature.rb
123
+ - lib/dry/initializer/config.rb
124
+ - lib/dry/initializer/definition.rb
125
+ - lib/dry/initializer/dispatchers.rb
126
+ - lib/dry/initializer/dispatchers/build_nested_type.rb
127
+ - lib/dry/initializer/dispatchers/check_type.rb
128
+ - lib/dry/initializer/dispatchers/prepare_default.rb
129
+ - lib/dry/initializer/dispatchers/prepare_ivar.rb
130
+ - lib/dry/initializer/dispatchers/prepare_optional.rb
131
+ - lib/dry/initializer/dispatchers/prepare_reader.rb
132
+ - lib/dry/initializer/dispatchers/prepare_source.rb
133
+ - lib/dry/initializer/dispatchers/prepare_target.rb
134
+ - lib/dry/initializer/dispatchers/unwrap_type.rb
135
+ - lib/dry/initializer/dispatchers/wrap_type.rb
136
+ - lib/dry/initializer/dsl.rb
137
+ - lib/dry/initializer/mixin.rb
138
+ - lib/dry/initializer/mixin/local.rb
139
+ - lib/dry/initializer/mixin/root.rb
140
+ - lib/dry/initializer/struct.rb
141
+ - lib/dry/initializer/undefined.rb
142
+ - lib/tasks/benchmark.rake
143
+ - lib/tasks/profile.rake
144
+ - spec/attributes_spec.rb
145
+ - spec/coercion_of_nil_spec.rb
146
+ - spec/custom_dispatchers_spec.rb
147
+ - spec/custom_initializer_spec.rb
148
+ - spec/default_values_spec.rb
149
+ - spec/definition_spec.rb
150
+ - spec/invalid_default_spec.rb
151
+ - spec/list_type_spec.rb
152
+ - spec/missed_default_spec.rb
153
+ - spec/nested_type_spec.rb
154
+ - spec/optional_spec.rb
155
+ - spec/options_tolerance_spec.rb
156
+ - spec/public_attributes_utility_spec.rb
157
+ - spec/reader_spec.rb
158
+ - spec/repetitive_definitions_spec.rb
159
+ - spec/several_assignments_spec.rb
160
+ - spec/spec_helper.rb
161
+ - spec/subclassing_spec.rb
162
+ - spec/type_argument_spec.rb
163
+ - spec/type_constraint_spec.rb
164
+ - spec/value_coercion_via_dry_types_spec.rb
165
+ homepage: https://github.com/dry-rb/dry-initializer
166
+ licenses:
167
+ - MIT
168
+ metadata: {}
169
+ post_install_message:
170
+ rdoc_options: []
171
+ require_paths:
172
+ - lib
173
+ required_ruby_version: !ruby/object:Gem::Requirement
174
+ requirements:
175
+ - - ">="
176
+ - !ruby/object:Gem::Version
177
+ version: '2.3'
178
+ required_rubygems_version: !ruby/object:Gem::Requirement
179
+ requirements:
180
+ - - ">="
181
+ - !ruby/object:Gem::Version
182
+ version: '0'
183
+ requirements: []
184
+ rubygems_version: 3.0.6
185
+ signing_key:
186
+ specification_version: 4
187
+ summary: DSL for declaring params and options of the initializer
188
+ test_files:
189
+ - spec/attributes_spec.rb
190
+ - spec/coercion_of_nil_spec.rb
191
+ - spec/custom_dispatchers_spec.rb
192
+ - spec/custom_initializer_spec.rb
193
+ - spec/default_values_spec.rb
194
+ - spec/definition_spec.rb
195
+ - spec/invalid_default_spec.rb
196
+ - spec/list_type_spec.rb
197
+ - spec/missed_default_spec.rb
198
+ - spec/nested_type_spec.rb
199
+ - spec/optional_spec.rb
200
+ - spec/options_tolerance_spec.rb
201
+ - spec/public_attributes_utility_spec.rb
202
+ - spec/reader_spec.rb
203
+ - spec/repetitive_definitions_spec.rb
204
+ - spec/several_assignments_spec.rb
205
+ - spec/spec_helper.rb
206
+ - spec/subclassing_spec.rb
207
+ - spec/type_argument_spec.rb
208
+ - spec/type_constraint_spec.rb
209
+ - spec/value_coercion_via_dry_types_spec.rb