dry-initializer 0.11.0 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (37) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +1 -0
  3. data/.travis.yml +18 -11
  4. data/CHANGELOG.md +85 -43
  5. data/benchmarks/options.rb +4 -4
  6. data/benchmarks/with_types.rb +2 -4
  7. data/benchmarks/with_types_and_defaults.rb +2 -4
  8. data/dry-initializer.gemspec +1 -1
  9. data/lib/dry/initializer.rb +77 -13
  10. data/lib/dry/initializer/attribute.rb +52 -0
  11. data/lib/dry/initializer/builder.rb +79 -72
  12. data/lib/dry/initializer/exceptions/default_value_error.rb +8 -0
  13. data/lib/dry/initializer/exceptions/params_order_error.rb +8 -0
  14. data/lib/dry/initializer/exceptions/type_constraint_error.rb +7 -0
  15. data/lib/dry/initializer/option.rb +55 -0
  16. data/lib/dry/initializer/param.rb +49 -0
  17. data/spec/missed_default_spec.rb +2 -2
  18. data/spec/optional_spec.rb +16 -6
  19. data/spec/options_var_spec.rb +39 -0
  20. data/spec/repetitive_definitions_spec.rb +38 -18
  21. data/spec/type_constraint_spec.rb +3 -3
  22. metadata +10 -18
  23. data/lib/dry/initializer/errors.rb +0 -10
  24. data/lib/dry/initializer/errors/default_value_error.rb +0 -6
  25. data/lib/dry/initializer/errors/order_error.rb +0 -7
  26. data/lib/dry/initializer/errors/plugin_error.rb +0 -6
  27. data/lib/dry/initializer/errors/redefinition_error.rb +0 -5
  28. data/lib/dry/initializer/errors/type_constraint_error.rb +0 -5
  29. data/lib/dry/initializer/mixin.rb +0 -77
  30. data/lib/dry/initializer/plugins.rb +0 -10
  31. data/lib/dry/initializer/plugins/base.rb +0 -47
  32. data/lib/dry/initializer/plugins/default_proc.rb +0 -28
  33. data/lib/dry/initializer/plugins/signature.rb +0 -28
  34. data/lib/dry/initializer/plugins/type_constraint.rb +0 -21
  35. data/lib/dry/initializer/plugins/variable_setter.rb +0 -30
  36. data/lib/dry/initializer/signature.rb +0 -61
  37. data/spec/plugin_registry_spec.rb +0 -45
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 604898d7c7112e84b0e097a2913e81ba54918e25
4
- data.tar.gz: 417c9a1e41b76162b12d7ce36dd19a156ca408e2
3
+ metadata.gz: 7c99b0349e94feff82082c286d57dab769c73e6c
4
+ data.tar.gz: b4c23613507bd539f72dda1ae60b68ebf81024fd
5
5
  SHA512:
6
- metadata.gz: aec2d5e6f46522584d247e1c106eaab9bcec8b2787c4a827ddb24e1f533c81b242b3c04796207f97c4cb0a179d7581146e94495296f995e31b5a9e0a6c269ccf
7
- data.tar.gz: 0ee2c8d900d400312a8c8ddae17648e1b37d28edb61034a76b0c01e87b2ed740cb135281f391f2e3d3f6170ece71faa977772e463ed5603d8c7a1039d51a0948
6
+ metadata.gz: 2a916174077ac5ebdd6894bf416c10e0b6802658f1c9f848dd3902890c2e5b9c000468a60cea0b17ba4545a16f8d5c39474fe05849b14f5d67caeb51b5f67ada
7
+ data.tar.gz: d42931e206096f1dfd528675891ffbae169324f5c80a08204fb118d17c2ebce0c9a2daec91dfcb8f9063cd7172e0769603d75686ac60f6fed7613401e32e3b64
data/.gitignore CHANGED
@@ -7,3 +7,4 @@
7
7
  /pkg/
8
8
  /spec/reports/
9
9
  /tmp/
10
+ /*.gem
data/.travis.yml CHANGED
@@ -1,20 +1,27 @@
1
1
  ---
2
- sudo: false
3
2
  language: ruby
4
- cache: bundler
3
+ sudo: false
4
+ cache: bundler
5
+ bundler_args: --without benchmarks tools
5
6
  script:
6
- - bundle exec rspec
7
- - bundle exec rubocop
7
+ - bundle exec rake spec
8
8
  rvm:
9
- - '2.2'
10
- - '2.3.0'
11
- - '2.4.0'
12
- # - rbx-2
9
+ - 2.2
10
+ - 2.3.0
11
+ - 2.4.0
12
+ - jruby-9000
13
+ - rbx-2
14
+ - rbx-3
13
15
  - ruby-head
14
- - jruby-9.1.0.0
15
- - jruby-head
16
- before_install: gem install bundler -v 1.12.5
16
+ env:
17
+ global:
18
+ - JRUBY_OPTS='--dev -J-Xmx1024M'
17
19
  matrix:
18
20
  allow_failures:
21
+ - rvm: rbx-2
22
+ - rvm: rbx-3
19
23
  - rvm: ruby-head
20
24
  - rvm: jruby-head
25
+ include:
26
+ - rvm: jruby-head
27
+ before_install: gem install bundler --no-ri --no-rdoc
data/CHANGELOG.md CHANGED
@@ -1,12 +1,54 @@
1
+ ## v1.0.0 2017-01-22
2
+
3
+ In this version the code has been rewritten for simplicity
4
+
5
+ # BREAKING CHANGES
6
+ - when `param` or `option` was not defined, the corresponding **variable** is set to `Dry::Initializer::UNDEFINED`, but the **reader** (when defined) will return `nil` (nepalez)
7
+
8
+ # Added:
9
+ - support for reloading `param` and `option` definitions (nepalez)
10
+
11
+ class User
12
+ extend Dry::Initializer
13
+ param :name
14
+ param :phone, optional: true
15
+ end
16
+
17
+ User.new # => Boom!
18
+
19
+ class Admin < User
20
+ param :name, default: proc { 'Merlin' }
21
+ end
22
+
23
+ # order of the param not changed
24
+ Admin.new.name # => "Merlin"
25
+
26
+ - support for assignment of attributes via several options (nepalez)
27
+
28
+ class User
29
+ extend Dry::Initializer
30
+ option :phone
31
+ option :number, as: :phone
32
+ end
33
+
34
+ # Both ways provide the same result
35
+ User.new(phone: '1234567890').phone # => '1234567890'
36
+ User.new(number: '1234567890').phone # => '1234567890'
37
+
38
+ # Internals
39
+ - `Dry::Initializer` and `Dry::Initializer::Mixin` became aliases (nepalez)
40
+
41
+ [Compare v0.11.0...v1.0.0](https://github.com/dry-rb/dry-initializer/compare/v0.11.0...v1.0.0)
42
+
1
43
  ## v0.11.0 2017-01-02
2
44
 
3
45
  ### Added
4
46
 
5
- * Support of reloading `#initializer` with `super` (@nepalez)
47
+ * Support of reloading `#initializer` with `super` (nepalez)
6
48
 
7
49
  ### Internal
8
50
 
9
- * Refactor the way [#initializer] method is (re)defined (@nepalez)
51
+ * Refactor the way [#initializer] method is (re)defined (nepalez)
10
52
 
11
53
  When you extend class with `Dry::Initializer::Mixin`, the initializer is
12
54
  defined not "inside" the class per se, but inside the included module. The
@@ -37,11 +79,11 @@
37
79
 
38
80
  ### Added
39
81
 
40
- * Support of Ruby 2.4 (@flas-gordon)
82
+ * Support of Ruby 2.4 (flas-gordon)
41
83
 
42
84
  ### Internal
43
85
 
44
- * Code clearance for ROM integration (@flash-gordon)
86
+ * Code clearance for ROM integration (flash-gordon)
45
87
 
46
88
  [Compare v0.10.1...v0.10.2](https://github.com/dry-rb/dry-initializer/compare/v0.10.1...v0.10.2)
47
89
 
@@ -49,7 +91,7 @@
49
91
 
50
92
  ### Fixed
51
93
 
52
- * Wrong arity when there were no options and the last param had a default (@nolith)
94
+ * Wrong arity when there were no options and the last param had a default (nolith)
53
95
 
54
96
  [Compare v0.10.0...v0.10.1](https://github.com/dry-rb/dry-initializer/compare/v0.10.0...v0.10.1)
55
97
 
@@ -57,7 +99,7 @@
57
99
 
58
100
  ### Deleted (BREAKING CHANGE!)
59
101
 
60
- * Deprecated method DSL#using (@nepalez)
102
+ * Deprecated method DSL#using (nepalez)
61
103
 
62
104
  [Compare v0.9.3...v0.10.0](https://github.com/dry-rb/dry-initializer/compare/v0.9.3...v0.10.0)
63
105
 
@@ -67,11 +109,11 @@
67
109
 
68
110
  * After discussion in [PR #17](https://github.com/dry-rb/dry-initializer/pull/17)
69
111
  (many thanks to @sahal2080 and @hrom512 for starting that issue and PR),
70
- the method `using` is deprecated and will be removed from v0.10.0 (@nepalez)
112
+ the method `using` is deprecated and will be removed from v0.10.0 (nepalez)
71
113
 
72
114
  ### Fixed
73
115
 
74
- * Support of weird option names (@nepalez)
116
+ * Support of weird option names (nepalez)
75
117
 
76
118
  ```ruby
77
119
  option :"First name", as: :first_name
@@ -83,7 +125,7 @@
83
125
 
84
126
  ### Fixed
85
127
 
86
- * Validation of attributes (params and options) (@nepalez)
128
+ * Validation of attributes (params and options) (nepalez)
87
129
 
88
130
  [Compare v0.9.1...v0.9.2](https://github.com/dry-rb/dry-initializer/compare/v0.9.1...v0.9.2)
89
131
 
@@ -91,7 +133,7 @@
91
133
 
92
134
  ### Added
93
135
 
94
- * Support for renaming an option during initialization (@nepalez)
136
+ * Support for renaming an option during initialization (nepalez)
95
137
 
96
138
  option :name, as: :username # to take :name option and create :username attribute
97
139
 
@@ -101,21 +143,21 @@
101
143
 
102
144
  ### Added
103
145
 
104
- * The method `#initialize` is defined when a class extended the module (@nepalez)
146
+ * The method `#initialize` is defined when a class extended the module (nepalez)
105
147
 
106
148
  In previous versions the method was defined only by `param` and `option` calls.
107
149
 
108
150
  ### Breaking Changes
109
151
 
110
- * The initializer accepts any option (but skips unknown) from the very beginning (@nepalez)
152
+ * The initializer accepts any option (but skips unknown) from the very beginning (nepalez)
111
153
 
112
154
  ### Deleted
113
155
 
114
- * Deprecated methods `tolerant_to_unknown_options` and `intolerant_to_unknown_options` (@nepalez)
156
+ * Deprecated methods `tolerant_to_unknown_options` and `intolerant_to_unknown_options` (nepalez)
115
157
 
116
158
  ### Internal
117
159
 
118
- * Refactor scope (`using`) to support methods renaming and aliasing (@nepalez)
160
+ * Refactor scope (`using`) to support methods renaming and aliasing (nepalez)
119
161
 
120
162
  [Compare v0.8.1...v0.9.0](https://github.com/dry-rb/dry-initializer/compare/v0.8.1...v0.9.0)
121
163
 
@@ -123,7 +165,7 @@
123
165
 
124
166
  ### Added
125
167
 
126
- * Support for `dry-struct`ish syntax for constraints (type as a second parameter) (@nepalez)
168
+ * Support for `dry-struct`ish syntax for constraints (type as a second parameter) (nepalez)
127
169
 
128
170
  option :name, Dry::Types['strict.string']
129
171
 
@@ -158,11 +200,11 @@ are deprecated and will be removed in the next version of the gem.
158
200
 
159
201
  ### Added
160
202
 
161
- * support for special options like `option :end`, `option :begin` etc. (@nepalez)
203
+ * support for special options like `option :end`, `option :begin` etc. (nepalez)
162
204
 
163
205
  ### Internals
164
206
 
165
- * switched from key arguments to serialized hash argument in the initializer (@nepalez)
207
+ * switched from key arguments to serialized hash argument in the initializer (nepalez)
166
208
 
167
209
  ### Breaking Changes
168
210
 
@@ -183,7 +225,7 @@ are deprecated and will be removed in the next version of the gem.
183
225
 
184
226
  ### Added
185
227
 
186
- * Shared settings with `#using` method (@nepalez)
228
+ * Shared settings with `#using` method (nepalez)
187
229
 
188
230
  [Compare v0.6.0...v0.7.0](https://github.com/dry-rb/dry-initializer/compare/v0.6.0...v0.7.0)
189
231
 
@@ -191,7 +233,7 @@ are deprecated and will be removed in the next version of the gem.
191
233
 
192
234
  ### Added
193
235
 
194
- * Support for private and protected readers in the `reader:` option (@jmgarnier)
236
+ * Support for private and protected readers in the `reader:` option (jmgarnier)
195
237
 
196
238
  [Compare v0.5.0...v0.6.0](https://github.com/dry-rb/dry-initializer/compare/v0.5.0...v0.6.0)
197
239
 
@@ -199,7 +241,7 @@ are deprecated and will be removed in the next version of the gem.
199
241
 
200
242
  ### Added
201
243
 
202
- * Allow `optional` attribute to be left undefined (@nepalez)
244
+ * Allow `optional` attribute to be left undefined (nepalez)
203
245
 
204
246
  [Compare v0.4.0...v0.5.0](https://github.com/dry-rb/dry-initializer/compare/v0.4.0...v0.5.0)
205
247
 
@@ -207,13 +249,13 @@ are deprecated and will be removed in the next version of the gem.
207
249
 
208
250
  ### Deleted (backward-incompatible changes)
209
251
 
210
- * Support of modules and case equality as type constraints (@nepalez)
252
+ * Support of modules and case equality as type constraints (nepalez)
211
253
 
212
254
  [Compare v0.3.3...v0.4.0](https://github.com/dry-rb/dry-initializer/compare/v0.3.3...v0.4.0)
213
255
 
214
256
  ## v0.3.3 2016-05-28
215
257
 
216
- * Add deprecation warnings about modules and case equality as type constraints (@nepalez)
258
+ * Add deprecation warnings about modules and case equality as type constraints (nepalez)
217
259
 
218
260
  [Compare v0.3.2...v0.3.3](https://github.com/dry-rb/dry-initializer/compare/v0.3.2...v0.3.3)
219
261
 
@@ -221,7 +263,7 @@ are deprecated and will be removed in the next version of the gem.
221
263
 
222
264
  ### Bugs Fixed
223
265
 
224
- * Add explicit requirement for ruby 'set' (@rickenharp)
266
+ * Add explicit requirement for ruby 'set' (rickenharp)
225
267
 
226
268
  [Compare v0.3.1...v0.3.2](https://github.com/dry-rb/dry-initializer/compare/v0.3.1...v0.3.2)
227
269
 
@@ -229,7 +271,7 @@ are deprecated and will be removed in the next version of the gem.
229
271
 
230
272
  ### Added
231
273
 
232
- * Support for tolerance to unknown options (@nepalez)
274
+ * Support for tolerance to unknown options (nepalez)
233
275
 
234
276
  [Compare v0.3.0...v0.3.1](https://github.com/dry-rb/dry-initializer/compare/v0.3.0...v0.3.1)
235
277
 
@@ -256,20 +298,20 @@ its method #register doesn't mutate the builder instance.
256
298
 
257
299
  ### Changed (backward-incompatible changes)
258
300
 
259
- * Made Mixin##initializer_builder method private (@nepalez)
260
- * Add Mixin#register_initializer_plugin(plugin) method (@nepalez)
301
+ * Made Mixin##initializer_builder method private (nepalez)
302
+ * Add Mixin#register_initializer_plugin(plugin) method (nepalez)
261
303
 
262
304
  ### Bugs Fixed
263
305
 
264
- * Prevent plugin's registry from polluting superclass (@nepalez)
306
+ * Prevent plugin's registry from polluting superclass (nepalez)
265
307
 
266
308
  [Compare v0.2.1...v0.3.0](https://github.com/dry-rb/dry-initializer/compare/v0.2.1...v0.3.0)
267
309
 
268
310
  ### Internals
269
311
 
270
- * Make all instances (Builder and Signature) immutable (@nepalez)
271
- * Decouple mixin from a builder to prevent pollution (@nepalez)
272
- * Ensure default value block can use private variables (@jeremyf)
312
+ * Make all instances (Builder and Signature) immutable (nepalez)
313
+ * Decouple mixin from a builder to prevent pollution (nepalez)
314
+ * Ensure default value block can use private variables (jeremyf)
273
315
 
274
316
  [Compare v0.2.0...v0.2.1](https://github.com/dry-rb/dry-initializer/compare/v0.2.0...v0.2.1)
275
317
 
@@ -277,13 +319,13 @@ its method #register doesn't mutate the builder instance.
277
319
 
278
320
  ### Bugs Fixed
279
321
 
280
- * Fix polluting superclass with declarations from subclass (@nepalez)
322
+ * Fix polluting superclass with declarations from subclass (nepalez)
281
323
 
282
324
  ### Internals
283
325
 
284
- * Make all instances (Builder and Signature) immutable (@nepalez)
285
- * Decouple mixin from a builder to prevent pollution (@nepalez)
286
- * Ensure default value block can use private variables (@jeremyf)
326
+ * Make all instances (Builder and Signature) immutable (nepalez)
327
+ * Decouple mixin from a builder to prevent pollution (nepalez)
328
+ * Ensure default value block can use private variables (jeremyf)
287
329
 
288
330
  [Compare v0.2.0...v0.2.1](https://github.com/dry-rb/dry-initializer/compare/v0.2.0...v0.2.1)
289
331
 
@@ -300,14 +342,14 @@ Default assignments became slower (while plain type constraint are not)!
300
342
 
301
343
  ### Changed (backward-incompatible changes)
302
344
 
303
- * Make dry-types constraint to coerce variables (@nepalez)
345
+ * Make dry-types constraint to coerce variables (nepalez)
304
346
 
305
347
  ```ruby
306
348
  # This will coerce `name: :foo` to `"foo"`
307
349
  option :name, type: Dry::Types::Coercible::String
308
350
  ```
309
351
 
310
- * Stop supporing proc type constraint (@nepalez)
352
+ * Stop supporing proc type constraint (nepalez)
311
353
 
312
354
  ```ruby
313
355
  option :name, type: ->(v) { String === v } # this does NOT work any more
@@ -328,19 +370,19 @@ Default assignments became slower (while plain type constraint are not)!
328
370
 
329
371
  ### Added
330
372
 
331
- * Support type constraint via every object's case equality (@nepalez)
373
+ * Support type constraint via every object's case equality (nepalez)
332
374
 
333
375
  ```ruby
334
376
  option :name, type: /foo/
335
377
  option :name, type: (1...14)
336
378
  ```
337
379
 
338
- * Support defaults and type constraints for the "container" syntax (@nepalez)
339
- * Support adding extensions via plugin system (@nepalez)
380
+ * Support defaults and type constraints for the "container" syntax (nepalez)
381
+ * Support adding extensions via plugin system (nepalez)
340
382
 
341
383
  ### Internal
342
384
 
343
- * Private method `##__after_initialize__` is added by the `Mixin` along with `#initialize` (@nepalez)
385
+ * Private method `##__after_initialize__` is added by the `Mixin` along with `#initialize` (nepalez)
344
386
 
345
387
  The previous implementation required defaults and types to be stored in the class method `.initializer_builder`.
346
388
  That made "container" syntax to support neither defaults nor types.
@@ -356,7 +398,7 @@ Default assignments became slower (while plain type constraint are not)!
356
398
 
357
399
  ### Added
358
400
 
359
- * `include Dry::Initializer.define -> do ... end` syntax (@flash-gordon)
401
+ * `include Dry::Initializer.define -> do ... end` syntax (flash-gordon)
360
402
 
361
403
  [Compare v0.1.0...v0.1.1](https://github.com/dry-rb/dry-initializer/compare/v0.1.0...v0.1.1)
362
404
 
@@ -367,11 +409,11 @@ Backward compatibility is broken.
367
409
 
368
410
  ### Changed (backward-incompatible changes)
369
411
 
370
- * Use `extend Dry::Initializer::Mixin` instead of `extend Dry::Initializer` (@nepalez)
412
+ * Use `extend Dry::Initializer::Mixin` instead of `extend Dry::Initializer` (nepalez)
371
413
 
372
414
  ### Added
373
415
 
374
- * Use `include Dry::Initializer.define(&block)` as an alternative to extending the class (@nepalez)
416
+ * Use `include Dry::Initializer.define(&block)` as an alternative to extending the class (nepalez)
375
417
 
376
418
  [Compare v0.0.1...v0.1.0](https://github.com/dry-rb/dry-initializer/compare/v0.0.1...v0.1.0)
377
419
 
@@ -18,15 +18,15 @@ end
18
18
  class TypesTest
19
19
  extend Dry::Initializer::Mixin
20
20
 
21
- param :foo, type: String
22
- option :bar, type: String
21
+ param :foo, proc(&:to_s)
22
+ option :bar, proc(&:to_s)
23
23
  end
24
24
 
25
25
  class DefaultsAndTypesTest
26
26
  extend Dry::Initializer::Mixin
27
27
 
28
- param :foo, type: String, default: proc { "FOO" }
29
- option :bar, type: String, default: proc { "BAR" }
28
+ param :foo, proc(&:to_s), default: proc { "FOO" }
29
+ option :bar, proc(&:to_s), default: proc { "BAR" }
30
30
  end
31
31
 
32
32
  puts "Benchmark for various options"
@@ -12,13 +12,11 @@ class PlainRubyTest
12
12
  end
13
13
 
14
14
  require "dry-initializer"
15
- require "dry/initializer/types"
16
15
  class DryTest
17
16
  extend Dry::Initializer::Mixin
18
- extend Dry::Initializer::Types
19
17
 
20
- option :foo, type: String
21
- option :bar, type: String
18
+ option :foo, &(:to_s)
19
+ option :bar, &(:to_s)
22
20
  end
23
21
 
24
22
  require "virtus"
@@ -12,13 +12,11 @@ class PlainRubyTest
12
12
  end
13
13
 
14
14
  require "dry-initializer"
15
- require "dry/initializer/types"
16
15
  class DryTest
17
16
  extend Dry::Initializer::Mixin
18
- extend Dry::Initializer::Types
19
17
 
20
- option :foo, type: String, default: proc { "FOO" }
21
- option :bar, type: String, default: proc { "BAR" }
18
+ option :foo, proc(&:to_s), default: proc { "FOO" }
19
+ option :bar, proc(&:to_s), default: proc { "BAR" }
22
20
  end
23
21
 
24
22
  require "virtus"
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |gem|
2
2
  gem.name = "dry-initializer"
3
- gem.version = "0.11.0"
3
+ gem.version = "1.0.0"
4
4
  gem.author = ["Vladimir Kochnev (marshall-lee)", "Andrew Kozin (nepalez)"]
5
5
  gem.email = ["hashtable@yandex.ru", "andrew.kozin@gmail.com"]
6
6
  gem.homepage = "https://github.com/dryrb/dry-initializer"