dry-initializer 3.0.1 → 3.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +259 -242
- data/LICENSE +20 -0
- data/README.md +18 -78
- data/dry-initializer.gemspec +34 -19
- data/lib/dry/initializer/builders/attribute.rb +84 -69
- data/lib/dry/initializer/builders/initializer.rb +56 -58
- data/lib/dry/initializer/builders/reader.rb +55 -47
- data/lib/dry/initializer/builders/signature.rb +29 -23
- data/lib/dry/initializer/builders.rb +9 -5
- data/lib/dry/initializer/config.rb +162 -158
- data/lib/dry/initializer/definition.rb +58 -54
- data/lib/dry/initializer/dispatchers/build_nested_type.rb +54 -40
- data/lib/dry/initializer/dispatchers/check_type.rb +45 -39
- data/lib/dry/initializer/dispatchers/prepare_default.rb +32 -25
- data/lib/dry/initializer/dispatchers/prepare_ivar.rb +13 -6
- data/lib/dry/initializer/dispatchers/prepare_optional.rb +14 -7
- data/lib/dry/initializer/dispatchers/prepare_reader.rb +29 -22
- data/lib/dry/initializer/dispatchers/prepare_source.rb +12 -5
- data/lib/dry/initializer/dispatchers/prepare_target.rb +44 -37
- data/lib/dry/initializer/dispatchers/unwrap_type.rb +21 -10
- data/lib/dry/initializer/dispatchers/wrap_type.rb +25 -17
- data/lib/dry/initializer/dispatchers.rb +48 -43
- data/lib/dry/initializer/dsl.rb +42 -34
- data/lib/dry/initializer/errors.rb +22 -0
- data/lib/dry/initializer/mixin/local.rb +19 -13
- data/lib/dry/initializer/mixin/root.rb +12 -6
- data/lib/dry/initializer/mixin.rb +17 -12
- data/lib/dry/initializer/struct.rb +34 -30
- data/lib/dry/initializer/undefined.rb +7 -1
- data/lib/dry/initializer/version.rb +7 -0
- data/lib/dry/initializer.rb +3 -0
- data/lib/dry-initializer.rb +2 -0
- data/lib/tasks/benchmark.rake +2 -0
- data/lib/tasks/profile.rake +4 -0
- metadata +27 -106
- data/.codeclimate.yml +0 -23
- data/.gitignore +0 -10
- data/.rspec +0 -4
- data/.rubocop.yml +0 -51
- data/.travis.yml +0 -28
- 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 -111
- data/spec/invalid_default_spec.rb +0 -13
- data/spec/list_type_spec.rb +0 -32
- data/spec/missed_default_spec.rb +0 -14
- data/spec/nested_type_spec.rb +0 -44
- 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 -22
- 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-2021 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,90 +1,30 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
[][gem]
|
|
4
|
-
[][travis]
|
|
5
|
-
[][codeclimate]
|
|
6
|
-
[][coveralls]
|
|
7
|
-
[][inchpages]
|
|
8
|
-
|
|
1
|
+
<!--- this file is synced from dry-rb/template-gem project -->
|
|
9
2
|
[gem]: https://rubygems.org/gems/dry-initializer
|
|
10
|
-
[
|
|
11
|
-
[
|
|
12
|
-
[
|
|
13
|
-
[coveralls]: https://coveralls.io/r/dry-rb/dry-initializer
|
|
3
|
+
[actions]: https://github.com/dry-rb/dry-initializer/actions
|
|
4
|
+
[codacy]: https://www.codacy.com/gh/dry-rb/dry-initializer
|
|
5
|
+
[chat]: https://dry-rb.zulipchat.com
|
|
14
6
|
[inchpages]: http://inch-ci.org/github/dry-rb/dry-initializer
|
|
15
|
-
[docs]: http://dry-rb.org/gems/dry-initializer/
|
|
16
|
-
[benchmarks]: https://github.com/dry-rb/dry-initializer/wiki
|
|
17
|
-
[license]: http://opensource.org/licenses/MIT
|
|
18
|
-
|
|
19
|
-
DSL for building class initializer with params and options.
|
|
20
|
-
|
|
21
|
-
## Installation
|
|
22
|
-
|
|
23
|
-
Add this line to your application's Gemfile:
|
|
24
|
-
|
|
25
|
-
```ruby
|
|
26
|
-
gem 'dry-initializer'
|
|
27
|
-
```
|
|
28
|
-
|
|
29
|
-
And then execute:
|
|
30
|
-
|
|
31
|
-
```shell
|
|
32
|
-
$ bundle
|
|
33
|
-
```
|
|
34
|
-
|
|
35
|
-
Or install it yourself as:
|
|
36
7
|
|
|
37
|
-
|
|
38
|
-
$ gem install dry-initializer
|
|
39
|
-
```
|
|
8
|
+
# dry-initializer [][chat]
|
|
40
9
|
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
class User
|
|
48
|
-
extend Dry::Initializer
|
|
49
|
-
|
|
50
|
-
# Params of the initializer along with corresponding readers
|
|
51
|
-
param :name, proc(&:to_s)
|
|
52
|
-
param :role, default: -> { 'customer' }
|
|
53
|
-
# Options of the initializer along with corresponding readers
|
|
54
|
-
option :admin, default: -> { false }
|
|
55
|
-
option :vip, optional: true
|
|
56
|
-
end
|
|
57
|
-
|
|
58
|
-
# Defines the initializer with params and options
|
|
59
|
-
user = User.new 'Vladimir', 'admin', admin: true
|
|
60
|
-
|
|
61
|
-
# Defines readers for single attributes
|
|
62
|
-
user.name # => 'Vladimir'
|
|
63
|
-
user.role # => 'admin'
|
|
64
|
-
user.admin # => true
|
|
65
|
-
user.vip # => Dry::Initializer::UNDEFINED
|
|
66
|
-
```
|
|
67
|
-
|
|
68
|
-
See full documentation on the [Dry project official site][docs]
|
|
69
|
-
|
|
70
|
-
## Benchmarks
|
|
10
|
+
[][gem]
|
|
11
|
+
[][actions]
|
|
12
|
+
[][codacy]
|
|
13
|
+
[][codacy]
|
|
14
|
+
[][inchpages]
|
|
71
15
|
|
|
72
|
-
|
|
16
|
+
## Links
|
|
73
17
|
|
|
74
|
-
|
|
18
|
+
* [User documentation](https://dry-rb.org/gems/dry-initializer)
|
|
19
|
+
* [API documentation](http://rubydoc.info/gems/dry-initializer)
|
|
75
20
|
|
|
76
|
-
|
|
21
|
+
## Supported Ruby versions
|
|
77
22
|
|
|
78
|
-
|
|
23
|
+
This library officially supports the following Ruby versions:
|
|
79
24
|
|
|
80
|
-
*
|
|
81
|
-
*
|
|
82
|
-
* Add tests for it
|
|
83
|
-
* Commit your changes (`git commit -am '[UPDATE] Add some feature'`)
|
|
84
|
-
* Push to the branch (`git push origin my-new-feature`)
|
|
85
|
-
* Create a new Pull Request
|
|
25
|
+
* MRI `>= 2.7.0`
|
|
26
|
+
* jruby `>= 9.3` (postponed until 2.7 is supported)
|
|
86
27
|
|
|
87
28
|
## License
|
|
88
29
|
|
|
89
|
-
|
|
90
|
-
|
|
30
|
+
See `LICENSE` file.
|
data/dry-initializer.gemspec
CHANGED
|
@@ -1,20 +1,35 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# this file is synced from dry-rb/template-gem project
|
|
4
|
+
|
|
5
|
+
lib = File.expand_path("lib", __dir__)
|
|
6
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
7
|
+
require "dry/initializer/version"
|
|
8
|
+
|
|
9
|
+
Gem::Specification.new do |spec|
|
|
10
|
+
spec.name = "dry-initializer"
|
|
11
|
+
spec.authors = ["Vladimir Kochnev (marshall-lee)", "Andrew Kozin (nepalez)"]
|
|
12
|
+
spec.email = ["andrew.kozin@gmail.com"]
|
|
13
|
+
spec.license = "MIT"
|
|
14
|
+
spec.version = Dry::Initializer::VERSION.dup
|
|
15
|
+
|
|
16
|
+
spec.summary = "DSL for declaring params and options of the initializer"
|
|
17
|
+
spec.description = spec.summary
|
|
18
|
+
spec.homepage = "https://dry-rb.org/gems/dry-initializer"
|
|
19
|
+
spec.files = Dir["CHANGELOG.md", "LICENSE", "README.md", "dry-initializer.gemspec", "lib/**/*"]
|
|
20
|
+
spec.bindir = "bin"
|
|
21
|
+
spec.executables = []
|
|
22
|
+
spec.require_paths = ["lib"]
|
|
23
|
+
|
|
24
|
+
spec.metadata["allowed_push_host"] = "https://rubygems.org"
|
|
25
|
+
spec.metadata["changelog_uri"] = "https://github.com/dry-rb/dry-initializer/blob/master/CHANGELOG.md"
|
|
26
|
+
spec.metadata["source_code_uri"] = "https://github.com/dry-rb/dry-initializer"
|
|
27
|
+
spec.metadata["bug_tracker_uri"] = "https://github.com/dry-rb/dry-initializer/issues"
|
|
28
|
+
|
|
29
|
+
spec.required_ruby_version = ">= 2.7.0"
|
|
30
|
+
|
|
31
|
+
# to update dependencies edit project.yml
|
|
32
|
+
|
|
33
|
+
spec.add_development_dependency "rake"
|
|
34
|
+
spec.add_development_dependency "rspec"
|
|
20
35
|
end
|
|
@@ -1,81 +1,96 @@
|
|
|
1
|
-
|
|
2
|
-
# @private
|
|
3
|
-
class Attribute
|
|
4
|
-
def self.[](definition)
|
|
5
|
-
new(definition).call
|
|
6
|
-
end
|
|
1
|
+
# frozen_string_literal: true
|
|
7
2
|
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
3
|
+
module Dry
|
|
4
|
+
module Initializer
|
|
5
|
+
module Builders
|
|
6
|
+
# @private
|
|
7
|
+
class Attribute
|
|
8
|
+
def self.[](definition)
|
|
9
|
+
new(definition).call
|
|
10
|
+
end
|
|
11
11
|
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
def initialize(definition)
|
|
16
|
-
@definition = definition
|
|
17
|
-
@option = definition.option
|
|
18
|
-
@type = definition.type
|
|
19
|
-
@optional = definition.optional
|
|
20
|
-
@default = definition.default
|
|
21
|
-
@source = definition.source
|
|
22
|
-
@ivar = definition.ivar
|
|
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
|
-
end
|
|
29
|
-
# rubocop: enable Metrics/MethodLength
|
|
30
|
-
|
|
31
|
-
def lines
|
|
32
|
-
[
|
|
33
|
-
"",
|
|
34
|
-
definition_line,
|
|
35
|
-
reader_line,
|
|
36
|
-
default_line,
|
|
37
|
-
coercion_line,
|
|
38
|
-
assignment_line
|
|
39
|
-
]
|
|
40
|
-
end
|
|
12
|
+
def call
|
|
13
|
+
lines.compact
|
|
14
|
+
end
|
|
41
15
|
|
|
42
|
-
|
|
43
|
-
return unless @option
|
|
44
|
-
@optional ? optional_reader : required_reader
|
|
45
|
-
end
|
|
16
|
+
private
|
|
46
17
|
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
18
|
+
def initialize(definition)
|
|
19
|
+
@definition = definition
|
|
20
|
+
@option = definition.option
|
|
21
|
+
@type = definition.type
|
|
22
|
+
@optional = definition.optional
|
|
23
|
+
@default = definition.default
|
|
24
|
+
@source = definition.source
|
|
25
|
+
@ivar = definition.ivar
|
|
26
|
+
@null = definition.null ? "Dry::Initializer::UNDEFINED" : "nil"
|
|
27
|
+
@opts = "__dry_initializer_options__"
|
|
28
|
+
@congif = "__dry_initializer_config__"
|
|
29
|
+
@item = "__dry_initializer_definition__"
|
|
30
|
+
@val = @option ? "__dry_initializer_value__" : @source
|
|
31
|
+
end
|
|
50
32
|
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
33
|
+
def lines
|
|
34
|
+
[
|
|
35
|
+
"",
|
|
36
|
+
definition_line,
|
|
37
|
+
reader_line,
|
|
38
|
+
default_line,
|
|
39
|
+
coercion_line,
|
|
40
|
+
assignment_line
|
|
41
|
+
]
|
|
42
|
+
end
|
|
55
43
|
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
"#{@item} = __dry_initializer_config__.definitions[:'#{@source}']"
|
|
59
|
-
end
|
|
44
|
+
def reader_line
|
|
45
|
+
return unless @option
|
|
60
46
|
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
"#{@val} = instance_exec(&#{@item}.default) if #{@null} == #{@val}"
|
|
64
|
-
end
|
|
47
|
+
@optional ? optional_reader : required_reader
|
|
48
|
+
end
|
|
65
49
|
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
50
|
+
def optional_reader
|
|
51
|
+
"#{@val} = #{@opts}.fetch(:'#{@source}', #{@null})"
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
def required_reader
|
|
55
|
+
"#{@val} = #{@opts}.fetch(:'#{@source}')" \
|
|
56
|
+
" { raise KeyError, \"\#{self.class}: #{@definition} is required\" }"
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
def definition_line
|
|
60
|
+
return unless @type || @default
|
|
61
|
+
|
|
62
|
+
"#{@item} = __dry_initializer_config__.definitions[:'#{@source}']"
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
def default_line
|
|
66
|
+
return unless @default
|
|
75
67
|
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
68
|
+
"#{@val} = instance_exec(&#{@item}.default) if #{@null} == #{@val}"
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
def coercion_line
|
|
72
|
+
return unless @type
|
|
73
|
+
|
|
74
|
+
arity = @type.is_a?(Proc) ? @type.arity : @type.method(:call).arity
|
|
75
|
+
type_call_params = \
|
|
76
|
+
arity.equal?(1) || arity.negative? ? @val : "#{@val}, self"
|
|
77
|
+
|
|
78
|
+
<<-COERCE
|
|
79
|
+
begin
|
|
80
|
+
unless #{@null} == #{@val}
|
|
81
|
+
#{@val} = #{@item}.type.call(#{type_call_params})
|
|
82
|
+
end
|
|
83
|
+
rescue Dry::Types::ConstraintError => e
|
|
84
|
+
raise Dry::Initializer::CoercionError.new(e, '#{@source}')
|
|
85
|
+
end
|
|
86
|
+
COERCE
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
def assignment_line
|
|
90
|
+
"#{@ivar} = #{@val}" \
|
|
91
|
+
" unless #{@null} == #{@val} && instance_variable_defined?(:#{@ivar})"
|
|
92
|
+
end
|
|
93
|
+
end
|
|
79
94
|
end
|
|
80
95
|
end
|
|
81
96
|
end
|
|
@@ -1,61 +1,59 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
def private_line
|
|
58
|
-
"private :__dry_initializer_initialize__"
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Dry
|
|
4
|
+
module Initializer
|
|
5
|
+
module Builders
|
|
6
|
+
# @private
|
|
7
|
+
class Initializer
|
|
8
|
+
require_relative "signature"
|
|
9
|
+
require_relative "attribute"
|
|
10
|
+
|
|
11
|
+
def self.[](config)
|
|
12
|
+
new(config).call
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def call
|
|
16
|
+
lines.flatten.compact.join("\n")
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
private
|
|
20
|
+
|
|
21
|
+
def initialize(config)
|
|
22
|
+
@config = config
|
|
23
|
+
@definitions = config.definitions.values
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def lines
|
|
27
|
+
[
|
|
28
|
+
undef_line,
|
|
29
|
+
define_line,
|
|
30
|
+
params_lines,
|
|
31
|
+
options_lines,
|
|
32
|
+
end_line
|
|
33
|
+
]
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def undef_line
|
|
37
|
+
"undef :__dry_initializer_initialize__" \
|
|
38
|
+
" if private_method_defined? :__dry_initializer_initialize__"
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def define_line
|
|
42
|
+
"private def __dry_initializer_initialize__(#{Signature[@config]})"
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def params_lines
|
|
46
|
+
@definitions.reject(&:option).flat_map { Attribute[_1] }.map { " #{_1}" }
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def options_lines
|
|
50
|
+
@definitions.select(&:option).flat_map { Attribute[_1] }.map { " #{_1}" }
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def end_line
|
|
54
|
+
"end"
|
|
55
|
+
end
|
|
56
|
+
end
|
|
59
57
|
end
|
|
60
58
|
end
|
|
61
59
|
end
|
|
@@ -1,50 +1,58 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Dry
|
|
4
|
+
module Initializer
|
|
5
|
+
module Builders
|
|
6
|
+
# @private
|
|
7
|
+
class Reader
|
|
8
|
+
def self.[](definition)
|
|
9
|
+
new(definition).call
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
def call
|
|
13
|
+
lines.flatten.compact.join("\n")
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
private
|
|
17
|
+
|
|
18
|
+
def initialize(definition)
|
|
19
|
+
@target = definition.target
|
|
20
|
+
@ivar = definition.ivar
|
|
21
|
+
@null = definition.null
|
|
22
|
+
@reader = definition.reader
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def lines
|
|
26
|
+
[undef_line, attribute_line, method_lines, type_line]
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def undef_line
|
|
30
|
+
"undef :#{@target} if method_defined?(:#{@target})" \
|
|
31
|
+
" || private_method_defined?(:#{@target})" \
|
|
32
|
+
" || protected_method_defined?(:#{@target})"
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def attribute_line
|
|
36
|
+
return unless @reader
|
|
37
|
+
|
|
38
|
+
"attr_reader :#{@target}" unless @null
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def method_lines
|
|
42
|
+
return unless @reader
|
|
43
|
+
return unless @null
|
|
44
|
+
|
|
45
|
+
[
|
|
46
|
+
"def #{@target}",
|
|
47
|
+
" #{@ivar} unless Dry::Initializer::UNDEFINED == #{@ivar}",
|
|
48
|
+
"end"
|
|
49
|
+
]
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def type_line
|
|
53
|
+
"#{@reader} :#{@target}" if %i[private protected].include? @reader
|
|
54
|
+
end
|
|
55
|
+
end
|
|
48
56
|
end
|
|
49
57
|
end
|
|
50
58
|
end
|
|
@@ -1,32 +1,38 @@
|
|
|
1
|
-
|
|
2
|
-
# @private
|
|
3
|
-
class Signature
|
|
4
|
-
def self.[](config)
|
|
5
|
-
new(config).call
|
|
6
|
-
end
|
|
1
|
+
# frozen_string_literal: true
|
|
7
2
|
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
3
|
+
module Dry
|
|
4
|
+
module Initializer
|
|
5
|
+
module Builders
|
|
6
|
+
# @private
|
|
7
|
+
class Signature
|
|
8
|
+
def self.[](config)
|
|
9
|
+
new(config).call
|
|
10
|
+
end
|
|
11
11
|
|
|
12
|
-
|
|
12
|
+
def call
|
|
13
|
+
[*required_params, *optional_params, "*", options].compact.join(", ")
|
|
14
|
+
end
|
|
13
15
|
|
|
14
|
-
|
|
15
|
-
@config = config
|
|
16
|
-
@options = config.options.any?
|
|
17
|
-
@null = config.null ? "Dry::Initializer::UNDEFINED" : "nil"
|
|
18
|
-
end
|
|
16
|
+
private
|
|
19
17
|
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
18
|
+
def initialize(config)
|
|
19
|
+
@config = config
|
|
20
|
+
@options = config.options.any?
|
|
21
|
+
@null = config.null ? "Dry::Initializer::UNDEFINED" : "nil"
|
|
22
|
+
end
|
|
23
23
|
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
24
|
+
def required_params
|
|
25
|
+
@config.params.reject(&:optional).map(&:source)
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def optional_params
|
|
29
|
+
@config.params.select(&:optional).map { |rec| "#{rec.source} = #{@null}" }
|
|
30
|
+
end
|
|
27
31
|
|
|
28
|
-
|
|
29
|
-
|
|
32
|
+
def options
|
|
33
|
+
"**__dry_initializer_options__" if @options
|
|
34
|
+
end
|
|
35
|
+
end
|
|
30
36
|
end
|
|
31
37
|
end
|
|
32
38
|
end
|
|
@@ -1,7 +1,11 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Dry
|
|
4
|
+
module Initializer
|
|
5
|
+
# @private
|
|
6
|
+
module Builders
|
|
7
|
+
require_relative "builders/reader"
|
|
8
|
+
require_relative "builders/initializer"
|
|
9
|
+
end
|
|
6
10
|
end
|
|
7
11
|
end
|