dry-initializer 0.0.1 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +13 -0
- data/README.md +56 -140
- data/benchmarks/options.rb +4 -4
- data/benchmarks/params.rb +1 -1
- data/benchmarks/params_vs_options.rb +2 -2
- data/benchmarks/several_defaults.rb +4 -4
- data/benchmarks/with_defaults.rb +1 -1
- data/benchmarks/with_types.rb +1 -1
- data/benchmarks/with_types_and_defaults.rb +1 -1
- data/benchmarks/without_options.rb +1 -1
- data/dry-initializer.gemspec +3 -3
- data/lib/dry/initializer.rb +5 -37
- data/lib/dry/initializer/mixin.rb +44 -0
- data/spec/dry/base_spec.rb +1 -1
- data/spec/dry/container_spec.rb +21 -0
- data/spec/dry/default_nil_spec.rb +1 -1
- data/spec/dry/default_values_spec.rb +1 -1
- data/spec/dry/dry_type_spec.rb +1 -1
- data/spec/dry/invalid_default_spec.rb +1 -1
- data/spec/dry/invalid_type_spec.rb +1 -1
- data/spec/dry/missed_default_spec.rb +1 -1
- data/spec/dry/poro_type_spec.rb +1 -1
- data/spec/dry/proc_type_spec.rb +1 -1
- data/spec/dry/reader_spec.rb +1 -1
- data/spec/dry/repetitive_definitions_spec.rb +3 -3
- data/spec/dry/subclassing_spec.rb +1 -1
- data/spec/spec_helper.rb +0 -1
- metadata +11 -8
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a7d86e04a313435b96cb103dc1f28a9427d7e799
|
4
|
+
data.tar.gz: b4b2556846b6e0ebee4b61808a28afc0c367290e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1d0e559d62195cab0d72796b00a65ad0a6e777dc16076b3a692b4145f5366d9a9574d1eb52187ac19a222e71922bf4479fc49ceffbd7fd1d6a35e57e395ca864
|
7
|
+
data.tar.gz: 3e64a46b03f6956ce80de18fb5f99b7aed2eb61817617d106719222c6b2405073b6b217db4ac79d51765d2d9b5c5f99eb02624d082be8b259890f3751dc86b93
|
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,16 @@
|
|
1
|
+
## v0.1.0 2016-04-26
|
2
|
+
|
3
|
+
Class DSL splitted to mixin and container versions (thanks to @AMHOL for the idea).
|
4
|
+
Backward compatibility is broken.
|
5
|
+
|
6
|
+
### Changed (backward-incompatible changes)
|
7
|
+
|
8
|
+
* Use `extend Dry::Initializer::Mixin` instead of `extend Dry::Initializer` (nepalez)
|
9
|
+
|
10
|
+
### Added
|
11
|
+
|
12
|
+
* Use `include Dry::Initializer.define(&block)` as an alternative to extending the class (nepalez)
|
13
|
+
|
1
14
|
## v0.0.1 2016-04-09
|
2
15
|
|
3
16
|
First public release
|
data/README.md
CHANGED
@@ -42,7 +42,7 @@ $ gem install dry-initializer
|
|
42
42
|
require 'dry-initializer'
|
43
43
|
|
44
44
|
class User
|
45
|
-
extend Dry::Initializer
|
45
|
+
extend Dry::Initializer::Mixin
|
46
46
|
|
47
47
|
# Params of the initializer along with corresponding readers
|
48
48
|
param :name, type: String
|
@@ -76,13 +76,51 @@ class User
|
|
76
76
|
end
|
77
77
|
```
|
78
78
|
|
79
|
+
### Container Version
|
80
|
+
|
81
|
+
Instead of extending a class with the `Dry::Initializer::Mixin`, you can include a container with the initializer:
|
82
|
+
|
83
|
+
```ruby
|
84
|
+
require 'dry-initializer'
|
85
|
+
|
86
|
+
class User
|
87
|
+
# notice `{}` syntax for the block, not `do..end`
|
88
|
+
include Dry::Initializer.define {
|
89
|
+
param :name, type: String
|
90
|
+
param :role, default: proc { 'customer' }
|
91
|
+
option :admin, default: proc { false }
|
92
|
+
}
|
93
|
+
end
|
94
|
+
```
|
95
|
+
|
96
|
+
Now you do not pollute a class with new variables, but isolate them in a special "container" module with the initializer and attribute readers. This method should be preferred when you don't need subclassing.
|
97
|
+
|
98
|
+
If you still need the DSL (`param` and `option`) to be inherited, use the direct extension:
|
99
|
+
|
100
|
+
```ruby
|
101
|
+
require 'dry-initializer'
|
102
|
+
|
103
|
+
class BaseService
|
104
|
+
extend Dry::Initializer::Mixin
|
105
|
+
alias_method :dependency, :param
|
106
|
+
end
|
107
|
+
|
108
|
+
class ShowUser < BaseService
|
109
|
+
dependency :user
|
110
|
+
|
111
|
+
def call
|
112
|
+
puts user&.name
|
113
|
+
end
|
114
|
+
end
|
115
|
+
```
|
116
|
+
|
79
117
|
### Params and Options
|
80
118
|
|
81
119
|
Use `param` to define plain argument:
|
82
120
|
|
83
121
|
```ruby
|
84
122
|
class User
|
85
|
-
extend Dry::Initializer
|
123
|
+
extend Dry::Initializer::Mixin
|
86
124
|
|
87
125
|
param :name
|
88
126
|
param :email
|
@@ -97,7 +135,7 @@ Use `option` to define named (hash) argument:
|
|
97
135
|
|
98
136
|
```ruby
|
99
137
|
class User
|
100
|
-
extend Dry::Initializer
|
138
|
+
extend Dry::Initializer::Mixin
|
101
139
|
|
102
140
|
option :name
|
103
141
|
option :email
|
@@ -112,7 +150,7 @@ All names should be unique:
|
|
112
150
|
|
113
151
|
```ruby
|
114
152
|
class User
|
115
|
-
extend Dry::Initializer
|
153
|
+
extend Dry::Initializer::Mixin
|
116
154
|
|
117
155
|
param :name
|
118
156
|
option :name # => raises #<SyntaxError ...>
|
@@ -125,7 +163,7 @@ By default both params and options are mandatory. Use `:default` key to make the
|
|
125
163
|
|
126
164
|
```ruby
|
127
165
|
class User
|
128
|
-
extend Dry::Initializer
|
166
|
+
extend Dry::Initializer::Mixin
|
129
167
|
|
130
168
|
param :name, default: proc { 'Unknown user' }
|
131
169
|
option :email, default: proc { 'unknown@example.com' }
|
@@ -144,7 +182,7 @@ Set `nil` as a default value explicitly:
|
|
144
182
|
|
145
183
|
```ruby
|
146
184
|
class User
|
147
|
-
extend Dry::Initializer
|
185
|
+
extend Dry::Initializer::Mixin
|
148
186
|
|
149
187
|
param :name
|
150
188
|
option :email, default: proc { nil }
|
@@ -163,7 +201,7 @@ If you need to **assign** proc as a default value, wrap it to another one:
|
|
163
201
|
|
164
202
|
```ruby
|
165
203
|
class User
|
166
|
-
extend Dry::Initializer
|
204
|
+
extend Dry::Initializer::Mixin
|
167
205
|
|
168
206
|
param :name_proc, default: proc { proc { 'Unknown user' } }
|
169
207
|
end
|
@@ -176,7 +214,7 @@ Proc will be executed in a scope of new instance. You can refer to other argumen
|
|
176
214
|
|
177
215
|
```ruby
|
178
216
|
class User
|
179
|
-
extend Dry::Initializer
|
217
|
+
extend Dry::Initializer::Mixin
|
180
218
|
|
181
219
|
param :name
|
182
220
|
param :email, default: proc { "#{name.downcase}@example.com" }
|
@@ -190,7 +228,7 @@ user.email # => 'andrew@example.com'
|
|
190
228
|
|
191
229
|
```ruby
|
192
230
|
class User
|
193
|
-
extend Dry::Initializer
|
231
|
+
extend Dry::Initializer::Mixin
|
194
232
|
|
195
233
|
param :name, default: -> (obj) { 'Dude' }
|
196
234
|
end
|
@@ -204,7 +242,7 @@ You cannot define required parameter after optional ones. The following example
|
|
204
242
|
|
205
243
|
```ruby
|
206
244
|
class User
|
207
|
-
extend Dry::Initializer
|
245
|
+
extend Dry::Initializer::Mixin
|
208
246
|
|
209
247
|
param :name, default: proc { 'Unknown name' }
|
210
248
|
param :email # => #<SyntaxError ...>
|
@@ -217,7 +255,7 @@ To set type constraint use `:type` key:
|
|
217
255
|
|
218
256
|
```ruby
|
219
257
|
class User
|
220
|
-
extend Dry::Initializer
|
258
|
+
extend Dry::Initializer::Mixin
|
221
259
|
|
222
260
|
param :name, type: String
|
223
261
|
end
|
@@ -233,7 +271,7 @@ You can use plain Ruby classes and modules as type constraint (see above), or us
|
|
233
271
|
|
234
272
|
```ruby
|
235
273
|
class User
|
236
|
-
extend Dry::Initializer
|
274
|
+
extend Dry::Initializer::Mixin
|
237
275
|
|
238
276
|
param :name, type: Dry::Types::Coercion::String
|
239
277
|
end
|
@@ -243,7 +281,7 @@ Or you can define custom constraint as a proc:
|
|
243
281
|
|
244
282
|
```ruby
|
245
283
|
class User
|
246
|
-
extend Dry::Initializer
|
284
|
+
extend Dry::Initializer::Mixin
|
247
285
|
|
248
286
|
param :name, type: proc { |v| raise TypeError if String === v }
|
249
287
|
end
|
@@ -262,7 +300,7 @@ To skip the reader, use `reader: false`:
|
|
262
300
|
|
263
301
|
```ruby
|
264
302
|
class User
|
265
|
-
extend Dry::Initializer
|
303
|
+
extend Dry::Initializer::Mixin
|
266
304
|
|
267
305
|
param :name
|
268
306
|
param :email, reader: false
|
@@ -283,7 +321,7 @@ Subclassing preserves all definitions being made inside a superclass:
|
|
283
321
|
|
284
322
|
```ruby
|
285
323
|
class User
|
286
|
-
extend Dry::Initializer
|
324
|
+
extend Dry::Initializer::Mixin
|
287
325
|
|
288
326
|
param :name
|
289
327
|
end
|
@@ -299,130 +337,9 @@ employee.position # => 'supercargo'
|
|
299
337
|
|
300
338
|
## Benchmarks
|
301
339
|
|
302
|
-
|
303
|
-
|
304
|
-
[At first][benchmark-options] we compared initializers for case of no-opts with those with default values and time constraints (for every single argument):
|
305
|
-
|
306
|
-
```
|
307
|
-
no opts: 1186020.0 i/s
|
308
|
-
with 2 types: 744825.4 i/s - 1.59x slower
|
309
|
-
with 2 defaults: 644170.0 i/s - 1.84x slower
|
310
|
-
with defaults and types: 534200.0 i/s - 2.22x slower
|
311
|
-
```
|
312
|
-
|
313
|
-
Defaults are slow. The more defaults you add the slower the instantiation. Let's [add details][benchmark_several_defaults]:
|
314
|
-
|
315
|
-
```
|
316
|
-
without defaults: 3412165.6 i/s
|
317
|
-
with 0 of 1 default used: 1816946.6 i/s - 1.88x slower
|
318
|
-
with 0 of 2 defaults used: 1620908.5 i/s - 2.11x slower
|
319
|
-
with 0 of 3 defaults used: 1493410.6 i/s - 2.28x slower
|
320
|
-
with 1 of 1 default used: 797438.8 i/s - 4.28x slower
|
321
|
-
with 1 of 2 defaults used: 754533.4 i/s - 4.52x slower
|
322
|
-
with 1 of 3 defaults used: 716828.9 i/s - 4.76x slower
|
323
|
-
with 2 of 2 defaults used: 622569.8 i/s - 5.48x slower
|
324
|
-
with 2 of 3 defaults used: 604062.1 i/s - 5.65x slower
|
325
|
-
with 3 of 3 defaults used: 533233.4 i/s - 6.40x slower
|
326
|
-
```
|
327
|
-
|
328
|
-
A single declaration of default values costs about 90% additional time. Its usage costs full 300%, and every next default adds 80% more.
|
329
|
-
|
330
|
-
Avoid defaults when possible!
|
331
|
-
|
332
|
-
### Comparison to Other Gems
|
333
|
-
|
334
|
-
We also compared initializers provided by gems from the [post 'Con-Struct Attibutes' by Jan Lelis][con-struct]:
|
335
|
-
|
336
|
-
* [active_attr][active_attr]
|
337
|
-
* [anima][anima]
|
338
|
-
* [attr_extras][attr_extras]
|
339
|
-
* [concord][concord]
|
340
|
-
* [fast_attr][fast_attr]
|
341
|
-
* [kwattr][kwattr]
|
342
|
-
* [value_struct][value_struct]
|
343
|
-
* [values][values]
|
344
|
-
* [virtus][virtus]
|
345
|
-
|
346
|
-
[con-struct]: http://idiosyncratic-ruby.com/18-con-struct-attributes.html
|
347
|
-
[active_attr]: https://github.com/cgriego/active_attr
|
348
|
-
[anima]: https://github.com/mbj/anima
|
349
|
-
[attr_extras]: https://github.com/barsoom/attr_extras
|
350
|
-
[concord]: https://github.com/mbj/concord
|
351
|
-
[fast_attr]: https://github.com/applift/fast_attributes
|
352
|
-
[kwattr]: https://github.com/etiennebarrie/kwattr
|
353
|
-
[value_struct]: https://github.com/janlelis/value_struct
|
354
|
-
[values]: https://github.com/tcrayford/values
|
355
|
-
[virtus]: https://github.com/solnic/virtus
|
356
|
-
|
357
|
-
Because the gems has their restrictions, in benchmarks we tested only comparable examples.
|
358
|
-
A corresponding code in plain Ruby was taken for comparison.
|
359
|
-
|
360
|
-
### Without Options
|
361
|
-
|
362
|
-
Results for [the examples][benchmark_without_options]
|
363
|
-
|
364
|
-
Benchmark for instantiation of plain arguments (params):
|
365
|
-
|
366
|
-
```
|
367
|
-
Core Struct: 4520294.5 i/s
|
368
|
-
value_struct: 4479181.2 i/s - same-ish: difference falls within error
|
369
|
-
plain Ruby: 4161762.2 i/s - 1.09x slower
|
370
|
-
dry-initializer: 3981426.3 i/s - 1.14x slower
|
371
|
-
concord: 1372696.9 i/s - 3.29x slower
|
372
|
-
values: 637396.9 i/s - 7.09x slower
|
373
|
-
attr_extras: 556342.9 i/s - 8.13x slower
|
374
|
-
```
|
375
|
-
|
376
|
-
Benchmark for instantiation of named arguments (options)
|
377
|
-
|
378
|
-
```
|
379
|
-
dry-initializer: 1020257.3 i/s
|
380
|
-
plain Ruby: 1009705.8 i/s - same-ish: difference falls within error
|
381
|
-
kwattr: 394574.0 i/s - 2.59x slower
|
382
|
-
anima: 377387.8 i/s - 2.70x slower
|
383
|
-
```
|
384
|
-
|
385
|
-
### With Default Values
|
386
|
-
|
387
|
-
Results for [the examples][benchmark_with_defaults]
|
388
|
-
|
389
|
-
```
|
390
|
-
plain Ruby: 3534979.5 i/s
|
391
|
-
dry-initializer: 657308.4 i/s - 5.38x slower
|
392
|
-
kwattr: 581691.0 i/s - 6.08x slower
|
393
|
-
active_attr: 309211.0 i/s - 11.43x slower
|
394
|
-
```
|
395
|
-
|
396
|
-
### With Type Constraints
|
397
|
-
|
398
|
-
Results for [the examples][benchmark_with_types]
|
399
|
-
|
400
|
-
```
|
401
|
-
plain Ruby: 951574.7 i/s
|
402
|
-
dry-initializer: 701676.7 i/s - 1.36x slower
|
403
|
-
fast_attributes: 562646.4 i/s - 1.69x slower
|
404
|
-
virtus: 143113.3 i/s - 6.65x slower
|
405
|
-
```
|
406
|
-
|
407
|
-
### With Default Values and Type Constraints
|
408
|
-
|
409
|
-
Results for [the examples][benchmark_with_types_and_defaults]
|
410
|
-
|
411
|
-
```
|
412
|
-
plain Ruby: 2887933.4 i/s
|
413
|
-
dry-initializer: 532508.0 i/s - 5.42x slower
|
414
|
-
virtus: 183347.1 i/s - 15.75x slower
|
415
|
-
```
|
416
|
-
|
417
|
-
To recap, `dry-initializer` is a fastest DSL for rubies 2.2+ except for cases when core `Struct` is sufficient.
|
340
|
+
The `dry-initializer` is a [fastest DSL][benchmarks] for rubies 2.2+ except for cases when core `Struct` is sufficient.
|
418
341
|
|
419
|
-
[
|
420
|
-
[benchmark_several_defaults]: https://github.com/dryrb/dry-initializer/blob/master/benchmarks/several_defaults.rb
|
421
|
-
[benchmark_without_options]: https://github.com/dryrb/dry-initializer/blob/master/benchmarks/without_options.rb
|
422
|
-
[benchmark_with_defaults]: https://github.com/dryrb/dry-initializer/blob/master/benchmarks/with_defaults.rb
|
423
|
-
[benchmark_with_types]: https://github.com/dryrb/dry-initializer/blob/master/benchmarks/with_types.rb
|
424
|
-
[benchmark_with_types_and_defaults]: https://github.com/dryrb/dry-initializer/blob/master/benchmarks/with_types_and_defaults.rb
|
425
|
-
[benchmark_params]: https://github.com/dryrb/dry-initializer/blob/master/benchmarks/params.rb
|
342
|
+
[benchmarks]: https://github.com/dry-rb/dry-initializer/wiki
|
426
343
|
|
427
344
|
## Compatibility
|
428
345
|
|
@@ -430,8 +347,7 @@ Tested under rubies [compatible to MRI 2.2+](.travis.yml).
|
|
430
347
|
|
431
348
|
## Contributing
|
432
349
|
|
433
|
-
*
|
434
|
-
* [Fork the project](https://github.com/nepalez/query_builder)
|
350
|
+
* [Fork the project](https://github.com/dry-rb/dry-initializer)
|
435
351
|
* Create your feature branch (`git checkout -b my-new-feature`)
|
436
352
|
* Add tests for it
|
437
353
|
* Commit your changes (`git commit -am '[UPDATE] Add some feature'`)
|
data/benchmarks/options.rb
CHANGED
@@ -2,28 +2,28 @@ Bundler.require(:benchmarks)
|
|
2
2
|
|
3
3
|
require "dry-initializer"
|
4
4
|
class NoOptsTest
|
5
|
-
extend Dry::Initializer
|
5
|
+
extend Dry::Initializer::Mixin
|
6
6
|
|
7
7
|
param :foo
|
8
8
|
option :bar
|
9
9
|
end
|
10
10
|
|
11
11
|
class DefaultsTest
|
12
|
-
extend Dry::Initializer
|
12
|
+
extend Dry::Initializer::Mixin
|
13
13
|
|
14
14
|
param :foo, default: proc { "FOO" }
|
15
15
|
option :bar, default: proc { "BAR" }
|
16
16
|
end
|
17
17
|
|
18
18
|
class TypesTest
|
19
|
-
extend Dry::Initializer
|
19
|
+
extend Dry::Initializer::Mixin
|
20
20
|
|
21
21
|
param :foo, type: String
|
22
22
|
option :bar, type: String
|
23
23
|
end
|
24
24
|
|
25
25
|
class DefaultsAndTypesTest
|
26
|
-
extend Dry::Initializer
|
26
|
+
extend Dry::Initializer::Mixin
|
27
27
|
|
28
28
|
param :foo, type: String, default: proc { "FOO" }
|
29
29
|
option :bar, type: String, default: proc { "BAR" }
|
data/benchmarks/params.rb
CHANGED
@@ -3,7 +3,7 @@ Bundler.require(:benchmarks)
|
|
3
3
|
require "dry-initializer"
|
4
4
|
|
5
5
|
class ParamDefaults
|
6
|
-
extend Dry::Initializer
|
6
|
+
extend Dry::Initializer::Mixin
|
7
7
|
|
8
8
|
param :foo, default: proc { "FOO" }
|
9
9
|
param :bar, default: proc { "BAR" }
|
@@ -11,7 +11,7 @@ class ParamDefaults
|
|
11
11
|
end
|
12
12
|
|
13
13
|
class OptionDefaults
|
14
|
-
extend Dry::Initializer
|
14
|
+
extend Dry::Initializer::Mixin
|
15
15
|
|
16
16
|
option :foo, default: proc { "FOO" }
|
17
17
|
option :bar, default: proc { "BAR" }
|
@@ -2,7 +2,7 @@ Bundler.require(:benchmarks)
|
|
2
2
|
|
3
3
|
require "dry-initializer"
|
4
4
|
class WithoutDefaults
|
5
|
-
extend Dry::Initializer
|
5
|
+
extend Dry::Initializer::Mixin
|
6
6
|
|
7
7
|
param :foo
|
8
8
|
param :bar
|
@@ -10,7 +10,7 @@ class WithoutDefaults
|
|
10
10
|
end
|
11
11
|
|
12
12
|
class WithOneDefault
|
13
|
-
extend Dry::Initializer
|
13
|
+
extend Dry::Initializer::Mixin
|
14
14
|
|
15
15
|
param :foo
|
16
16
|
param :bar
|
@@ -18,7 +18,7 @@ class WithOneDefault
|
|
18
18
|
end
|
19
19
|
|
20
20
|
class WithTwoDefaults
|
21
|
-
extend Dry::Initializer
|
21
|
+
extend Dry::Initializer::Mixin
|
22
22
|
|
23
23
|
param :foo
|
24
24
|
param :bar, default: proc { "BAR" }
|
@@ -26,7 +26,7 @@ class WithTwoDefaults
|
|
26
26
|
end
|
27
27
|
|
28
28
|
class WithThreeDefaults
|
29
|
-
extend Dry::Initializer
|
29
|
+
extend Dry::Initializer::Mixin
|
30
30
|
|
31
31
|
param :foo, default: proc { "FOO" }
|
32
32
|
param :bar, default: proc { "BAR" }
|
data/benchmarks/with_defaults.rb
CHANGED
data/benchmarks/with_types.rb
CHANGED
data/dry-initializer.gemspec
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
Gem::Specification.new do |gem|
|
2
2
|
gem.name = "dry-initializer"
|
3
|
-
gem.version = "0.0
|
3
|
+
gem.version = "0.1.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"
|
@@ -13,7 +13,7 @@ Gem::Specification.new do |gem|
|
|
13
13
|
|
14
14
|
gem.required_ruby_version = ">= 2.2"
|
15
15
|
|
16
|
-
gem.add_development_dependency "
|
17
|
-
gem.add_development_dependency "rake", "~> 10.
|
16
|
+
gem.add_development_dependency "rspec", "~> 3.0"
|
17
|
+
gem.add_development_dependency "rake", "~> 10.0"
|
18
18
|
gem.add_development_dependency "dry-types", "~> 0.5.1"
|
19
19
|
end
|
data/lib/dry/initializer.rb
CHANGED
@@ -9,45 +9,13 @@ module Dry
|
|
9
9
|
require_relative "initializer/argument"
|
10
10
|
require_relative "initializer/arguments"
|
11
11
|
require_relative "initializer/builder"
|
12
|
+
require_relative "initializer/mixin"
|
12
13
|
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
# @option options [Object] :default The default value
|
18
|
-
# @option options [#call] :type The type constraings via `dry-types`
|
19
|
-
# @option options [Boolean] :reader (true) Whether to define attr_reader
|
20
|
-
#
|
21
|
-
# @return [self] itself
|
22
|
-
#
|
23
|
-
def param(name, **options)
|
24
|
-
arguments_builder.define_initializer(name, option: false, **options)
|
25
|
-
self
|
26
|
-
end
|
27
|
-
|
28
|
-
# Declares a named argument
|
29
|
-
#
|
30
|
-
# @param (see #param)
|
31
|
-
# @option (see #param)
|
32
|
-
# @return (see #param)
|
33
|
-
#
|
34
|
-
def option(name, **options)
|
35
|
-
arguments_builder.define_initializer(name, option: true, **options)
|
36
|
-
self
|
37
|
-
end
|
38
|
-
|
39
|
-
private
|
40
|
-
|
41
|
-
def arguments_builder
|
42
|
-
@arguments_builder ||= begin
|
43
|
-
builder = Builder.new
|
44
|
-
include builder.mixin
|
45
|
-
builder
|
14
|
+
def self.define(&block)
|
15
|
+
Module.new do |container|
|
16
|
+
container.extend Dry::Initializer::Mixin
|
17
|
+
container.instance_eval(&block)
|
46
18
|
end
|
47
19
|
end
|
48
|
-
|
49
|
-
def inherited(klass)
|
50
|
-
klass.instance_variable_set(:@arguments_builder, arguments_builder)
|
51
|
-
end
|
52
20
|
end
|
53
21
|
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
module Dry::Initializer
|
2
|
+
# Class-level DSL for the initializer
|
3
|
+
module Mixin
|
4
|
+
# Declares a plain argument
|
5
|
+
#
|
6
|
+
# @param [#to_sym] name
|
7
|
+
#
|
8
|
+
# @option options [Object] :default The default value
|
9
|
+
# @option options [#call] :type The type constraings via `dry-types`
|
10
|
+
# @option options [Boolean] :reader (true) Whether to define attr_reader
|
11
|
+
#
|
12
|
+
# @return [self] itself
|
13
|
+
#
|
14
|
+
def param(name, **options)
|
15
|
+
arguments_builder.define_initializer(name, option: false, **options)
|
16
|
+
self
|
17
|
+
end
|
18
|
+
|
19
|
+
# Declares a named argument
|
20
|
+
#
|
21
|
+
# @param (see #param)
|
22
|
+
# @option (see #param)
|
23
|
+
# @return (see #param)
|
24
|
+
#
|
25
|
+
def option(name, **options)
|
26
|
+
arguments_builder.define_initializer(name, option: true, **options)
|
27
|
+
self
|
28
|
+
end
|
29
|
+
|
30
|
+
private
|
31
|
+
|
32
|
+
def arguments_builder
|
33
|
+
@arguments_builder ||= begin
|
34
|
+
builder = Builder.new
|
35
|
+
include builder.mixin
|
36
|
+
builder
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
def inherited(klass)
|
41
|
+
klass.instance_variable_set(:@arguments_builder, arguments_builder)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
data/spec/dry/base_spec.rb
CHANGED
@@ -0,0 +1,21 @@
|
|
1
|
+
describe "base example" do
|
2
|
+
before do
|
3
|
+
class Test::Foo
|
4
|
+
include Dry::Initializer.define {
|
5
|
+
param :foo
|
6
|
+
param :bar
|
7
|
+
option :baz
|
8
|
+
option :qux
|
9
|
+
}
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
it "instantiates attributes" do
|
14
|
+
subject = Test::Foo.new(1, 2, baz: 3, qux: 4)
|
15
|
+
|
16
|
+
expect(subject.foo).to eql 1
|
17
|
+
expect(subject.bar).to eql 2
|
18
|
+
expect(subject.baz).to eql 3
|
19
|
+
expect(subject.qux).to eql 4
|
20
|
+
end
|
21
|
+
end
|
data/spec/dry/dry_type_spec.rb
CHANGED
data/spec/dry/poro_type_spec.rb
CHANGED
data/spec/dry/proc_type_spec.rb
CHANGED
data/spec/dry/reader_spec.rb
CHANGED
@@ -2,7 +2,7 @@ describe "repetitive definitions" do
|
|
2
2
|
context "of params" do
|
3
3
|
subject do
|
4
4
|
class Test::Foo
|
5
|
-
extend Dry::Initializer
|
5
|
+
extend Dry::Initializer::Mixin
|
6
6
|
|
7
7
|
param :foo
|
8
8
|
param :bar
|
@@ -18,7 +18,7 @@ describe "repetitive definitions" do
|
|
18
18
|
context "of options" do
|
19
19
|
subject do
|
20
20
|
class Test::Foo
|
21
|
-
extend Dry::Initializer
|
21
|
+
extend Dry::Initializer::Mixin
|
22
22
|
|
23
23
|
option :foo
|
24
24
|
option :bar
|
@@ -34,7 +34,7 @@ describe "repetitive definitions" do
|
|
34
34
|
context "of param and option" do
|
35
35
|
subject do
|
36
36
|
class Test::Foo
|
37
|
-
extend Dry::Initializer
|
37
|
+
extend Dry::Initializer::Mixin
|
38
38
|
|
39
39
|
param :foo
|
40
40
|
option :bar
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: dry-initializer
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Vladimir Kochnev (marshall-lee)
|
@@ -9,36 +9,36 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2016-04-
|
12
|
+
date: 2016-04-25 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
|
-
name:
|
15
|
+
name: rspec
|
16
16
|
requirement: !ruby/object:Gem::Requirement
|
17
17
|
requirements:
|
18
18
|
- - "~>"
|
19
19
|
- !ruby/object:Gem::Version
|
20
|
-
version: '
|
20
|
+
version: '3.0'
|
21
21
|
type: :development
|
22
22
|
prerelease: false
|
23
23
|
version_requirements: !ruby/object:Gem::Requirement
|
24
24
|
requirements:
|
25
25
|
- - "~>"
|
26
26
|
- !ruby/object:Gem::Version
|
27
|
-
version: '
|
27
|
+
version: '3.0'
|
28
28
|
- !ruby/object:Gem::Dependency
|
29
29
|
name: rake
|
30
30
|
requirement: !ruby/object:Gem::Requirement
|
31
31
|
requirements:
|
32
32
|
- - "~>"
|
33
33
|
- !ruby/object:Gem::Version
|
34
|
-
version: '10.
|
34
|
+
version: '10.0'
|
35
35
|
type: :development
|
36
36
|
prerelease: false
|
37
37
|
version_requirements: !ruby/object:Gem::Requirement
|
38
38
|
requirements:
|
39
39
|
- - "~>"
|
40
40
|
- !ruby/object:Gem::Version
|
41
|
-
version: '10.
|
41
|
+
version: '10.0'
|
42
42
|
- !ruby/object:Gem::Dependency
|
43
43
|
name: dry-types
|
44
44
|
requirement: !ruby/object:Gem::Requirement
|
@@ -95,7 +95,9 @@ files:
|
|
95
95
|
- lib/dry/initializer/errors/key_error.rb
|
96
96
|
- lib/dry/initializer/errors/missed_default_value_error.rb
|
97
97
|
- lib/dry/initializer/errors/type_error.rb
|
98
|
+
- lib/dry/initializer/mixin.rb
|
98
99
|
- spec/dry/base_spec.rb
|
100
|
+
- spec/dry/container_spec.rb
|
99
101
|
- spec/dry/default_nil_spec.rb
|
100
102
|
- spec/dry/default_values_spec.rb
|
101
103
|
- spec/dry/dry_type_spec.rb
|
@@ -128,12 +130,13 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
128
130
|
version: '0'
|
129
131
|
requirements: []
|
130
132
|
rubyforge_project:
|
131
|
-
rubygems_version: 2.
|
133
|
+
rubygems_version: 2.4.8
|
132
134
|
signing_key:
|
133
135
|
specification_version: 4
|
134
136
|
summary: DSL for declaring params and options of the initializer
|
135
137
|
test_files:
|
136
138
|
- spec/dry/base_spec.rb
|
139
|
+
- spec/dry/container_spec.rb
|
137
140
|
- spec/dry/default_nil_spec.rb
|
138
141
|
- spec/dry/default_values_spec.rb
|
139
142
|
- spec/dry/dry_type_spec.rb
|