dry-initializer 2.5.0 → 3.0.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/CHANGELOG.md +367 -239
- data/LICENSE +20 -0
- data/README.md +17 -79
- data/dry-initializer.gemspec +29 -16
- data/lib/dry-initializer.rb +1 -1
- data/lib/dry/initializer.rb +16 -14
- data/lib/dry/initializer/builders.rb +2 -2
- data/lib/dry/initializer/builders/attribute.rb +12 -7
- data/lib/dry/initializer/builders/initializer.rb +9 -13
- data/lib/dry/initializer/builders/reader.rb +3 -1
- data/lib/dry/initializer/builders/signature.rb +3 -3
- data/lib/dry/initializer/config.rb +22 -8
- data/lib/dry/initializer/definition.rb +20 -71
- data/lib/dry/initializer/dispatchers.rb +101 -33
- data/lib/dry/initializer/dispatchers/build_nested_type.rb +59 -0
- data/lib/dry/initializer/dispatchers/check_type.rb +43 -0
- data/lib/dry/initializer/dispatchers/prepare_default.rb +40 -0
- data/lib/dry/initializer/dispatchers/prepare_ivar.rb +12 -0
- data/lib/dry/initializer/dispatchers/prepare_optional.rb +13 -0
- data/lib/dry/initializer/dispatchers/prepare_reader.rb +30 -0
- data/lib/dry/initializer/dispatchers/prepare_source.rb +28 -0
- data/lib/dry/initializer/dispatchers/prepare_target.rb +44 -0
- data/lib/dry/initializer/dispatchers/unwrap_type.rb +22 -0
- data/lib/dry/initializer/dispatchers/wrap_type.rb +28 -0
- data/lib/dry/initializer/mixin.rb +4 -4
- data/lib/dry/initializer/mixin/root.rb +1 -0
- data/lib/dry/initializer/struct.rb +39 -0
- data/lib/dry/initializer/undefined.rb +2 -0
- data/lib/dry/initializer/version.rb +5 -0
- data/lib/tasks/benchmark.rake +13 -13
- data/lib/tasks/profile.rake +16 -16
- metadata +38 -103
- data/.codeclimate.yml +0 -23
- data/.gitignore +0 -10
- data/.rspec +0 -4
- data/.rubocop.yml +0 -51
- data/.travis.yml +0 -24
- data/Gemfile +0 -29
- data/Guardfile +0 -5
- data/LICENSE.txt +0 -21
- data/Rakefile +0 -8
- data/benchmarks/compare_several_defaults.rb +0 -82
- data/benchmarks/plain_options.rb +0 -63
- data/benchmarks/plain_params.rb +0 -84
- data/benchmarks/with_coercion.rb +0 -71
- data/benchmarks/with_defaults.rb +0 -66
- data/benchmarks/with_defaults_and_coercion.rb +0 -59
- data/spec/attributes_spec.rb +0 -38
- data/spec/coercion_of_nil_spec.rb +0 -25
- data/spec/custom_dispatchers_spec.rb +0 -35
- data/spec/custom_initializer_spec.rb +0 -30
- data/spec/default_values_spec.rb +0 -83
- data/spec/definition_spec.rb +0 -107
- data/spec/invalid_default_spec.rb +0 -13
- data/spec/missed_default_spec.rb +0 -14
- data/spec/optional_spec.rb +0 -71
- data/spec/options_tolerance_spec.rb +0 -11
- data/spec/public_attributes_utility_spec.rb +0 -22
- data/spec/reader_spec.rb +0 -87
- data/spec/repetitive_definitions_spec.rb +0 -69
- data/spec/several_assignments_spec.rb +0 -41
- data/spec/spec_helper.rb +0 -21
- data/spec/subclassing_spec.rb +0 -49
- data/spec/type_argument_spec.rb +0 -35
- data/spec/type_constraint_spec.rb +0 -78
- data/spec/value_coercion_via_dry_types_spec.rb +0 -29
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015-2020 dry-rb team
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
6
|
+
this software and associated documentation files (the "Software"), to deal in
|
7
|
+
the Software without restriction, including without limitation the rights to
|
8
|
+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
9
|
+
the Software, and to permit persons to whom the Software is furnished to do so,
|
10
|
+
subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
17
|
+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
18
|
+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
19
|
+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
20
|
+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
CHANGED
@@ -1,91 +1,29 @@
|
|
1
|
-
# dry-initializer [![Join the chat at https://gitter.im/dry-rb/chat](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/dry-rb/chat)
|
2
|
-
|
3
|
-
[![Gem Version](https://badge.fury.io/rb/dry-initializer.svg)][gem]
|
4
|
-
[![Build Status](https://travis-ci.org/dry-rb/dry-initializer.svg?branch=master)][travis]
|
5
|
-
[![Dependency Status](https://gemnasium.com/dry-rb/dry-initializer.svg)][gemnasium]
|
6
|
-
[![Code Climate](https://codeclimate.com/github/dry-rb/dry-initializer/badges/gpa.svg)][codeclimate]
|
7
|
-
[![Test Coverage](https://codeclimate.com/github/dry-rb/dry-initializer/badges/coverage.svg)][coveralls]
|
8
|
-
[![Inline docs](http://inch-ci.org/github/dry-rb/dry-initializer.svg?branch=master)][inchpages]
|
9
|
-
|
10
1
|
[gem]: https://rubygems.org/gems/dry-initializer
|
11
|
-
[
|
12
|
-
[
|
13
|
-
[
|
14
|
-
[coveralls]: https://coveralls.io/r/dry-rb/dry-initializer
|
2
|
+
[actions]: https://github.com/dry-rb/dry-initializer/actions
|
3
|
+
[codacy]: https://www.codacy.com/gh/dry-rb/dry-initializer
|
4
|
+
[chat]: https://dry-rb.zulipchat.com
|
15
5
|
[inchpages]: http://inch-ci.org/github/dry-rb/dry-initializer
|
16
|
-
[docs]: http://dry-rb.org/gems/dry-initializer/
|
17
|
-
[benchmarks]: https://github.com/dry-rb/dry-initializer/wiki
|
18
|
-
[license]: http://opensource.org/licenses/MIT
|
19
|
-
|
20
|
-
DSL for building class initializer with params and options.
|
21
|
-
|
22
|
-
## Installation
|
23
|
-
|
24
|
-
Add this line to your application's Gemfile:
|
25
|
-
|
26
|
-
```ruby
|
27
|
-
gem 'dry-initializer'
|
28
|
-
```
|
29
|
-
|
30
|
-
And then execute:
|
31
|
-
|
32
|
-
```shell
|
33
|
-
$ bundle
|
34
|
-
```
|
35
|
-
|
36
|
-
Or install it yourself as:
|
37
6
|
|
38
|
-
|
39
|
-
$ gem install dry-initializer
|
40
|
-
```
|
7
|
+
# dry-initializer [![Join the chat at https://dry-rb.zulipchat.com](https://img.shields.io/badge/dry--rb-join%20chat-%23346b7a.svg)][chat]
|
41
8
|
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
class User
|
49
|
-
extend Dry::Initializer
|
50
|
-
|
51
|
-
# Params of the initializer along with corresponding readers
|
52
|
-
param :name, proc(&:to_s)
|
53
|
-
param :role, default: -> { 'customer' }
|
54
|
-
# Options of the initializer along with corresponding readers
|
55
|
-
option :admin, default: -> { false }
|
56
|
-
option :vip, optional: true
|
57
|
-
end
|
58
|
-
|
59
|
-
# Defines the initializer with params and options
|
60
|
-
user = User.new 'Vladimir', 'admin', admin: true
|
61
|
-
|
62
|
-
# Defines readers for single attributes
|
63
|
-
user.name # => 'Vladimir'
|
64
|
-
user.role # => 'admin'
|
65
|
-
user.admin # => true
|
66
|
-
user.vip # => Dry::Initializer::UNDEFINED
|
67
|
-
```
|
68
|
-
|
69
|
-
See full documentation on the [Dry project official site][docs]
|
70
|
-
|
71
|
-
## Benchmarks
|
9
|
+
[![Gem Version](https://badge.fury.io/rb/dry-initializer.svg)][gem]
|
10
|
+
[![CI Status](https://github.com/dry-rb/dry-initializer/workflows/ci/badge.svg)][actions]
|
11
|
+
[![Codacy Badge](https://api.codacy.com/project/badge/Grade/03c8923afd734e9fb0f4bfe9cc893edb)][codacy]
|
12
|
+
[![Codacy Badge](https://api.codacy.com/project/badge/Coverage/03c8923afd734e9fb0f4bfe9cc893edb)][codacy]
|
13
|
+
[![Inline docs](http://inch-ci.org/github/dry-rb/dry-initializer.svg?branch=master)][inchpages]
|
72
14
|
|
73
|
-
|
15
|
+
## Links
|
74
16
|
|
75
|
-
|
17
|
+
* [User documentation](http://dry-rb.org/gems/dry-initializer)
|
18
|
+
* [API documentation](http://rubydoc.info/gems/dry-initializer)
|
76
19
|
|
77
|
-
|
20
|
+
## Supported Ruby versions
|
78
21
|
|
79
|
-
|
22
|
+
This library officially supports the following Ruby versions:
|
80
23
|
|
81
|
-
*
|
82
|
-
*
|
83
|
-
* Add tests for it
|
84
|
-
* Commit your changes (`git commit -am '[UPDATE] Add some feature'`)
|
85
|
-
* Push to the branch (`git push origin my-new-feature`)
|
86
|
-
* Create a new Pull Request
|
24
|
+
* MRI >= `2.4`
|
25
|
+
* jruby >= `9.2`
|
87
26
|
|
88
27
|
## License
|
89
28
|
|
90
|
-
|
91
|
-
|
29
|
+
See `LICENSE` file.
|
data/dry-initializer.gemspec
CHANGED
@@ -1,20 +1,33 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
gem.version = "2.5.0"
|
4
|
-
gem.author = ["Vladimir Kochnev (marshall-lee)", "Andrew Kozin (nepalez)"]
|
5
|
-
gem.email = "andrew.kozin@gmail.com"
|
6
|
-
gem.homepage = "https://github.com/dryrb/dry-initializer"
|
7
|
-
gem.summary = "DSL for declaring params and options of the initializer"
|
8
|
-
gem.license = "MIT"
|
1
|
+
# frozen_string_literal: true
|
2
|
+
# this file is managed by dry-rb/devtools project
|
9
3
|
|
10
|
-
|
11
|
-
|
12
|
-
|
4
|
+
lib = File.expand_path('lib', __dir__)
|
5
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
6
|
+
require 'dry/initializer/version'
|
13
7
|
|
14
|
-
|
8
|
+
Gem::Specification.new do |spec|
|
9
|
+
spec.name = 'dry-initializer'
|
10
|
+
spec.authors = ["Vladimir Kochnev (marshall-lee)", "Andrew Kozin (nepalez)"]
|
11
|
+
spec.email = ["andrew.kozin@gmail.com"]
|
12
|
+
spec.license = 'MIT'
|
13
|
+
spec.version = Dry::Initializer::VERSION.dup
|
15
14
|
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
15
|
+
spec.summary = "DSL for declaring params and options of the initializer"
|
16
|
+
spec.description = spec.summary
|
17
|
+
spec.homepage = 'https://dry-rb.org/gems/dry-initializer'
|
18
|
+
spec.files = Dir["CHANGELOG.md", "LICENSE", "README.md", "dry-initializer.gemspec", "lib/**/*"]
|
19
|
+
spec.bindir = 'bin'
|
20
|
+
spec.executables = []
|
21
|
+
spec.require_paths = ['lib']
|
22
|
+
|
23
|
+
spec.metadata['allowed_push_host'] = 'https://rubygems.org'
|
24
|
+
spec.metadata['changelog_uri'] = 'https://github.com/dry-rb/dry-initializer/blob/master/CHANGELOG.md'
|
25
|
+
spec.metadata['source_code_uri'] = 'https://github.com/dry-rb/dry-initializer'
|
26
|
+
spec.metadata['bug_tracker_uri'] = 'https://github.com/dry-rb/dry-initializer/issues'
|
27
|
+
|
28
|
+
spec.required_ruby_version = ">= 2.4.0"
|
29
|
+
|
30
|
+
# to update dependencies edit project.yml
|
31
|
+
spec.add_development_dependency "rake"
|
32
|
+
spec.add_development_dependency "rspec"
|
20
33
|
end
|
data/lib/dry-initializer.rb
CHANGED
@@ -1 +1 @@
|
|
1
|
-
require_relative
|
1
|
+
require_relative 'dry/initializer'
|
data/lib/dry/initializer.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
require
|
1
|
+
require 'set'
|
2
2
|
|
3
3
|
# Namespace for gems in a dry-rb community
|
4
4
|
module Dry
|
@@ -6,15 +6,13 @@ module Dry
|
|
6
6
|
# DSL for declaring params and options of class initializers
|
7
7
|
#
|
8
8
|
module Initializer
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
require_relative
|
13
|
-
require_relative
|
14
|
-
require_relative
|
15
|
-
require_relative
|
16
|
-
require_relative "initializer/mixin"
|
17
|
-
require_relative "initializer/dispatchers"
|
9
|
+
require_relative 'initializer/undefined'
|
10
|
+
require_relative 'initializer/dsl'
|
11
|
+
require_relative 'initializer/definition'
|
12
|
+
require_relative 'initializer/builders'
|
13
|
+
require_relative 'initializer/config'
|
14
|
+
require_relative 'initializer/mixin'
|
15
|
+
require_relative 'initializer/dispatchers'
|
18
16
|
|
19
17
|
# Adds methods [.[]] and [.define]
|
20
18
|
extend DSL
|
@@ -32,18 +30,20 @@ module Dry
|
|
32
30
|
# @option opts [Boolean] :optional
|
33
31
|
# @option opts [Symbol] :as
|
34
32
|
# @option opts [true, false, :protected, :public, :private] :reader
|
33
|
+
# @yield block with nested definition
|
35
34
|
# @return [self] itself
|
36
|
-
def param(name, type = nil, **opts)
|
37
|
-
dry_initializer.param(name, type,
|
35
|
+
def param(name, type = nil, **opts, &block)
|
36
|
+
dry_initializer.param(name, type, **opts, &block)
|
38
37
|
self
|
39
38
|
end
|
40
39
|
|
41
40
|
# Adds or redefines an option of [#dry_initializer]
|
42
41
|
# @param (see #param)
|
43
42
|
# @option (see #param)
|
43
|
+
# @yield (see #param)
|
44
44
|
# @return (see #param)
|
45
|
-
def option(name, type = nil, **opts)
|
46
|
-
dry_initializer.option(name, type,
|
45
|
+
def option(name, type = nil, **opts, &block)
|
46
|
+
dry_initializer.option(name, type, **opts, &block)
|
47
47
|
self
|
48
48
|
end
|
49
49
|
|
@@ -55,5 +55,7 @@ module Dry
|
|
55
55
|
klass.send(:instance_variable_set, :@dry_initializer, config)
|
56
56
|
dry_initializer.children << config
|
57
57
|
end
|
58
|
+
|
59
|
+
require_relative 'initializer/struct'
|
58
60
|
end
|
59
61
|
end
|
@@ -20,17 +20,17 @@ module Dry::Initializer::Builders
|
|
20
20
|
@default = definition.default
|
21
21
|
@source = definition.source
|
22
22
|
@ivar = definition.ivar
|
23
|
-
@null = definition.null ?
|
24
|
-
@opts =
|
25
|
-
@congif =
|
26
|
-
@item =
|
27
|
-
@val = @option ?
|
23
|
+
@null = definition.null ? 'Dry::Initializer::UNDEFINED' : 'nil'
|
24
|
+
@opts = '__dry_initializer_options__'
|
25
|
+
@congif = '__dry_initializer_config__'
|
26
|
+
@item = '__dry_initializer_definition__'
|
27
|
+
@val = @option ? '__dry_initializer_value__' : @source
|
28
28
|
end
|
29
29
|
# rubocop: enable Metrics/MethodLength
|
30
30
|
|
31
31
|
def lines
|
32
32
|
[
|
33
|
-
|
33
|
+
'',
|
34
34
|
definition_line,
|
35
35
|
reader_line,
|
36
36
|
default_line,
|
@@ -41,6 +41,7 @@ module Dry::Initializer::Builders
|
|
41
41
|
|
42
42
|
def reader_line
|
43
43
|
return unless @option
|
44
|
+
|
44
45
|
@optional ? optional_reader : required_reader
|
45
46
|
end
|
46
47
|
|
@@ -55,18 +56,22 @@ module Dry::Initializer::Builders
|
|
55
56
|
|
56
57
|
def definition_line
|
57
58
|
return unless @type || @default
|
59
|
+
|
58
60
|
"#{@item} = __dry_initializer_config__.definitions[:'#{@source}']"
|
59
61
|
end
|
60
62
|
|
61
63
|
def default_line
|
62
64
|
return unless @default
|
65
|
+
|
63
66
|
"#{@val} = instance_exec(&#{@item}.default) if #{@null} == #{@val}"
|
64
67
|
end
|
65
68
|
|
66
69
|
def coercion_line
|
67
70
|
return unless @type
|
71
|
+
|
68
72
|
arity = @type.is_a?(Proc) ? @type.arity : @type.method(:call).arity
|
69
|
-
|
73
|
+
|
74
|
+
if arity.equal?(1) || arity.negative?
|
70
75
|
"#{@val} = #{@item}.type.call(#{@val}) unless #{@null} == #{@val}"
|
71
76
|
else
|
72
77
|
"#{@val} = #{@item}.type.call(#{@val}, self) unless #{@null} == #{@val}"
|
@@ -1,8 +1,8 @@
|
|
1
1
|
module Dry::Initializer::Builders
|
2
2
|
# @private
|
3
3
|
class Initializer
|
4
|
-
require_relative
|
5
|
-
require_relative
|
4
|
+
require_relative 'signature'
|
5
|
+
require_relative 'attribute'
|
6
6
|
|
7
7
|
def self.[](config)
|
8
8
|
new(config).call
|
@@ -30,8 +30,8 @@ module Dry::Initializer::Builders
|
|
30
30
|
end
|
31
31
|
|
32
32
|
def undef_line
|
33
|
-
|
34
|
-
|
33
|
+
'undef :__dry_initializer_initialize__' \
|
34
|
+
' if private_method_defined? :__dry_initializer_initialize__'
|
35
35
|
end
|
36
36
|
|
37
37
|
def define_line
|
@@ -40,22 +40,18 @@ module Dry::Initializer::Builders
|
|
40
40
|
|
41
41
|
def params_lines
|
42
42
|
@definitions.reject(&:option)
|
43
|
-
|
44
|
-
|
43
|
+
.flat_map { |item| Attribute[item] }
|
44
|
+
.map { |line| ' ' << line }
|
45
45
|
end
|
46
46
|
|
47
47
|
def options_lines
|
48
48
|
@definitions.select(&:option)
|
49
|
-
|
50
|
-
|
49
|
+
.flat_map { |item| Attribute[item] }
|
50
|
+
.map { |line| ' ' << line }
|
51
51
|
end
|
52
52
|
|
53
53
|
def end_line
|
54
|
-
|
55
|
-
end
|
56
|
-
|
57
|
-
def private_line
|
58
|
-
"private :__dry_initializer_initialize__"
|
54
|
+
'end'
|
59
55
|
end
|
60
56
|
end
|
61
57
|
end
|
@@ -30,16 +30,18 @@ module Dry::Initializer::Builders
|
|
30
30
|
|
31
31
|
def attribute_line
|
32
32
|
return unless @reader
|
33
|
+
|
33
34
|
"attr_reader :#{@target}" unless @null
|
34
35
|
end
|
35
36
|
|
36
37
|
def method_lines
|
37
38
|
return unless @reader
|
38
39
|
return unless @null
|
40
|
+
|
39
41
|
[
|
40
42
|
"def #{@target}",
|
41
43
|
" #{@ivar} unless Dry::Initializer::UNDEFINED == #{@ivar}",
|
42
|
-
|
44
|
+
'end'
|
43
45
|
]
|
44
46
|
end
|
45
47
|
|
@@ -6,7 +6,7 @@ module Dry::Initializer::Builders
|
|
6
6
|
end
|
7
7
|
|
8
8
|
def call
|
9
|
-
[*required_params, *optional_params,
|
9
|
+
[*required_params, *optional_params, '*', options].compact.join(', ')
|
10
10
|
end
|
11
11
|
|
12
12
|
private
|
@@ -14,7 +14,7 @@ module Dry::Initializer::Builders
|
|
14
14
|
def initialize(config)
|
15
15
|
@config = config
|
16
16
|
@options = config.options.any?
|
17
|
-
@null = config.null ?
|
17
|
+
@null = config.null ? 'Dry::Initializer::UNDEFINED' : 'nil'
|
18
18
|
end
|
19
19
|
|
20
20
|
def required_params
|
@@ -26,7 +26,7 @@ module Dry::Initializer::Builders
|
|
26
26
|
end
|
27
27
|
|
28
28
|
def options
|
29
|
-
|
29
|
+
'**__dry_initializer_options__' if @options
|
30
30
|
end
|
31
31
|
end
|
32
32
|
end
|
@@ -57,8 +57,8 @@ module Dry::Initializer
|
|
57
57
|
# @option opts [Symbol] :as
|
58
58
|
# @option opts [true, false, :protected, :public, :private] :reader
|
59
59
|
# @return [self] itself
|
60
|
-
def param(name, type = nil, **opts)
|
61
|
-
add_definition(false, name, type, opts)
|
60
|
+
def param(name, type = nil, **opts, &block)
|
61
|
+
add_definition(false, name, type, block, **opts)
|
62
62
|
end
|
63
63
|
|
64
64
|
# Adds or redefines an option of [#dry_initializer]
|
@@ -67,8 +67,8 @@ module Dry::Initializer
|
|
67
67
|
# @option (see #param)
|
68
68
|
# @return (see #param)
|
69
69
|
#
|
70
|
-
def option(name, type = nil, **opts)
|
71
|
-
add_definition(true, name, type, opts)
|
70
|
+
def option(name, type = nil, **opts, &block)
|
71
|
+
add_definition(true, name, type, block, **opts)
|
72
72
|
end
|
73
73
|
|
74
74
|
# The hash of public attributes for an instance of the [#extended_class]
|
@@ -78,6 +78,7 @@ module Dry::Initializer
|
|
78
78
|
definitions.values.each_with_object({}) do |item, obj|
|
79
79
|
key = item.target
|
80
80
|
next unless instance.respond_to? key
|
81
|
+
|
81
82
|
val = instance.send(key)
|
82
83
|
obj[key] = val unless null == val
|
83
84
|
end
|
@@ -114,7 +115,7 @@ module Dry::Initializer
|
|
114
115
|
# @return [String]
|
115
116
|
def inch
|
116
117
|
line = Builders::Signature[self]
|
117
|
-
line = line.gsub(
|
118
|
+
line = line.gsub('__dry_initializer_options__', 'options')
|
118
119
|
lines = ["@!method initialize(#{line})"]
|
119
120
|
lines += ["Initializes an instance of #{extended_class}"]
|
120
121
|
lines += definitions.values.map(&:inch)
|
@@ -133,13 +134,25 @@ module Dry::Initializer
|
|
133
134
|
finalize
|
134
135
|
end
|
135
136
|
|
136
|
-
|
137
|
-
|
137
|
+
# rubocop: disable Metrics/MethodLength
|
138
|
+
def add_definition(option, name, type, block, **opts)
|
139
|
+
opts = {
|
140
|
+
parent: extended_class,
|
141
|
+
option: option,
|
142
|
+
null: null,
|
143
|
+
source: name,
|
144
|
+
type: type,
|
145
|
+
block: block,
|
146
|
+
**opts
|
147
|
+
}
|
148
|
+
|
149
|
+
options = Dispatchers.call(**opts)
|
150
|
+
definition = Definition.new(**options)
|
138
151
|
definitions[definition.source] = definition
|
139
152
|
finalize
|
140
|
-
|
141
153
|
mixin.class_eval definition.code
|
142
154
|
end
|
155
|
+
# rubocop: enable Metrics/MethodLength
|
143
156
|
|
144
157
|
def final_definitions
|
145
158
|
parent_definitions = Hash(parent&.definitions&.dup)
|
@@ -151,6 +164,7 @@ module Dry::Initializer
|
|
151
164
|
def check_type(previous, current)
|
152
165
|
return current unless previous
|
153
166
|
return current if previous.option == current.option
|
167
|
+
|
154
168
|
raise SyntaxError,
|
155
169
|
"cannot reload #{previous} of #{extended_class.superclass}" \
|
156
170
|
" by #{current} of its subclass #{extended_class}"
|