dry-initializer 0.11.0 → 2.5.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (74) hide show
  1. checksums.yaml +4 -4
  2. data/.codeclimate.yml +8 -0
  3. data/.gitignore +1 -0
  4. data/.rspec +1 -0
  5. data/.rubocop.yml +14 -62
  6. data/.travis.yml +15 -11
  7. data/CHANGELOG.md +538 -158
  8. data/Gemfile +2 -2
  9. data/LICENSE.txt +1 -1
  10. data/README.md +6 -6
  11. data/Rakefile +2 -41
  12. data/benchmarks/{several_defaults.rb → compare_several_defaults.rb} +4 -4
  13. data/benchmarks/{without_options.rb → plain_options.rb} +21 -10
  14. data/benchmarks/{params.rb → plain_params.rb} +20 -9
  15. data/benchmarks/{with_types.rb → with_coercion.rb} +23 -16
  16. data/benchmarks/with_defaults.rb +19 -8
  17. data/benchmarks/{with_types_and_defaults.rb → with_defaults_and_coercion.rb} +21 -12
  18. data/dry-initializer.gemspec +4 -4
  19. data/lib/dry/initializer/builders/attribute.rb +81 -0
  20. data/lib/dry/initializer/builders/initializer.rb +61 -0
  21. data/lib/dry/initializer/builders/reader.rb +50 -0
  22. data/lib/dry/initializer/builders/signature.rb +32 -0
  23. data/lib/dry/initializer/builders.rb +7 -0
  24. data/lib/dry/initializer/config.rb +172 -0
  25. data/lib/dry/initializer/definition.rb +116 -0
  26. data/lib/dry/initializer/dispatchers.rb +44 -0
  27. data/lib/dry/initializer/dsl.rb +43 -0
  28. data/lib/dry/initializer/mixin/local.rb +19 -0
  29. data/lib/dry/initializer/mixin/root.rb +10 -0
  30. data/lib/dry/initializer/mixin.rb +8 -70
  31. data/lib/dry/initializer.rb +49 -12
  32. data/lib/tasks/benchmark.rake +41 -0
  33. data/lib/tasks/profile.rake +78 -0
  34. data/spec/attributes_spec.rb +38 -0
  35. data/spec/coercion_of_nil_spec.rb +25 -0
  36. data/spec/custom_dispatchers_spec.rb +35 -0
  37. data/spec/custom_initializer_spec.rb +1 -1
  38. data/spec/default_values_spec.rb +8 -8
  39. data/spec/definition_spec.rb +107 -0
  40. data/spec/invalid_default_spec.rb +2 -2
  41. data/spec/missed_default_spec.rb +3 -3
  42. data/spec/optional_spec.rb +35 -8
  43. data/spec/options_tolerance_spec.rb +1 -1
  44. data/spec/public_attributes_utility_spec.rb +22 -0
  45. data/spec/reader_spec.rb +11 -11
  46. data/spec/repetitive_definitions_spec.rb +41 -21
  47. data/spec/several_assignments_spec.rb +41 -0
  48. data/spec/spec_helper.rb +7 -0
  49. data/spec/subclassing_spec.rb +7 -3
  50. data/spec/type_argument_spec.rb +1 -1
  51. data/spec/type_constraint_spec.rb +38 -7
  52. data/spec/value_coercion_via_dry_types_spec.rb +12 -4
  53. metadata +37 -40
  54. data/benchmarks/options.rb +0 -54
  55. data/benchmarks/params_vs_options.rb +0 -35
  56. data/lib/dry/initializer/builder.rb +0 -100
  57. data/lib/dry/initializer/errors/default_value_error.rb +0 -6
  58. data/lib/dry/initializer/errors/order_error.rb +0 -7
  59. data/lib/dry/initializer/errors/plugin_error.rb +0 -6
  60. data/lib/dry/initializer/errors/redefinition_error.rb +0 -5
  61. data/lib/dry/initializer/errors/type_constraint_error.rb +0 -5
  62. data/lib/dry/initializer/errors.rb +0 -10
  63. data/lib/dry/initializer/plugins/base.rb +0 -47
  64. data/lib/dry/initializer/plugins/default_proc.rb +0 -28
  65. data/lib/dry/initializer/plugins/signature.rb +0 -28
  66. data/lib/dry/initializer/plugins/type_constraint.rb +0 -21
  67. data/lib/dry/initializer/plugins/variable_setter.rb +0 -30
  68. data/lib/dry/initializer/plugins.rb +0 -10
  69. data/lib/dry/initializer/signature.rb +0 -61
  70. data/spec/base_spec.rb +0 -21
  71. data/spec/container_spec.rb +0 -45
  72. data/spec/default_nil_spec.rb +0 -17
  73. data/spec/plugin_registry_spec.rb +0 -45
  74. data/spec/renaming_options_spec.rb +0 -20
@@ -1,11 +1,41 @@
1
1
  require "dry-types"
2
2
 
3
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
+
4
34
  context "by dry-type" do
5
35
  before do
6
36
  class Test::Foo
7
- extend Dry::Initializer::Mixin
8
- param :foo, type: Dry::Types["strict.string"], optional: true
37
+ extend Dry::Initializer
38
+ param :foo, Dry::Types["strict.string"], optional: true
9
39
  end
10
40
  end
11
41
 
@@ -28,20 +58,21 @@ describe "type constraint" do
28
58
  context "if optional value not set" do
29
59
  subject { Test::Foo.new }
30
60
 
31
- it "completes the initialization" do
32
- expect { subject }.not_to raise_error
61
+ it "not applicable to Dry::Initializer::UNDEFINED" do
62
+ expect(subject.instance_variable_get(:@foo))
63
+ .to eq Dry::Initializer::UNDEFINED
33
64
  end
34
65
  end
35
66
  end
36
67
 
37
68
  context "by invalid constraint" do
38
69
  it "raises TypeError" do
39
- expect {
70
+ expect do
40
71
  class Test::Foo
41
- extend Dry::Initializer::Mixin
72
+ extend Dry::Initializer
42
73
  param :foo, type: String
43
74
  end
44
- }.to raise_error(TypeError)
75
+ end.to raise_error(TypeError)
45
76
  end
46
77
  end
47
78
  end
@@ -7,15 +7,23 @@ describe "value coercion via dry-types" do
7
7
  end
8
8
 
9
9
  class Test::Foo
10
- extend Dry::Initializer::Mixin
10
+ extend Dry::Initializer
11
11
 
12
- param :foo, type: Test::Types::Coercible::String
12
+ param :foo, type: Test::Types::Coercible::String
13
+ option :bar, proc(&:to_i), default: proc { "16" }
13
14
  end
14
15
  end
15
16
 
16
- subject { Test::Foo.new :foo }
17
+ it "coerces assigned values" do
18
+ subject = Test::Foo.new :foo, bar: "13"
17
19
 
18
- it "coerces values" do
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
20
28
  end
21
29
  end
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.11.0
4
+ version: 2.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Vladimir Kochnev (marshall-lee)
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2017-01-02 00:00:00.000000000 Z
12
+ date: 2018-08-16 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
@@ -59,18 +59,16 @@ dependencies:
59
59
  requirements:
60
60
  - - "~>"
61
61
  - !ruby/object:Gem::Version
62
- version: '0.42'
62
+ version: 0.49.0
63
63
  type: :development
64
64
  prerelease: false
65
65
  version_requirements: !ruby/object:Gem::Requirement
66
66
  requirements:
67
67
  - - "~>"
68
68
  - !ruby/object:Gem::Version
69
- version: '0.42'
69
+ version: 0.49.0
70
70
  description:
71
- email:
72
- - hashtable@yandex.ru
73
- - andrew.kozin@gmail.com
71
+ email: andrew.kozin@gmail.com
74
72
  executables: []
75
73
  extensions: []
76
74
  extra_rdoc_files:
@@ -88,45 +86,43 @@ files:
88
86
  - LICENSE.txt
89
87
  - README.md
90
88
  - Rakefile
91
- - benchmarks/options.rb
92
- - benchmarks/params.rb
93
- - benchmarks/params_vs_options.rb
94
- - benchmarks/several_defaults.rb
89
+ - benchmarks/compare_several_defaults.rb
90
+ - benchmarks/plain_options.rb
91
+ - benchmarks/plain_params.rb
92
+ - benchmarks/with_coercion.rb
95
93
  - benchmarks/with_defaults.rb
96
- - benchmarks/with_types.rb
97
- - benchmarks/with_types_and_defaults.rb
98
- - benchmarks/without_options.rb
94
+ - benchmarks/with_defaults_and_coercion.rb
99
95
  - dry-initializer.gemspec
100
96
  - lib/dry-initializer.rb
101
97
  - lib/dry/initializer.rb
102
- - lib/dry/initializer/builder.rb
103
- - lib/dry/initializer/errors.rb
104
- - lib/dry/initializer/errors/default_value_error.rb
105
- - lib/dry/initializer/errors/order_error.rb
106
- - lib/dry/initializer/errors/plugin_error.rb
107
- - lib/dry/initializer/errors/redefinition_error.rb
108
- - lib/dry/initializer/errors/type_constraint_error.rb
98
+ - lib/dry/initializer/builders.rb
99
+ - lib/dry/initializer/builders/attribute.rb
100
+ - lib/dry/initializer/builders/initializer.rb
101
+ - lib/dry/initializer/builders/reader.rb
102
+ - lib/dry/initializer/builders/signature.rb
103
+ - lib/dry/initializer/config.rb
104
+ - lib/dry/initializer/definition.rb
105
+ - lib/dry/initializer/dispatchers.rb
106
+ - lib/dry/initializer/dsl.rb
109
107
  - lib/dry/initializer/mixin.rb
110
- - lib/dry/initializer/plugins.rb
111
- - lib/dry/initializer/plugins/base.rb
112
- - lib/dry/initializer/plugins/default_proc.rb
113
- - lib/dry/initializer/plugins/signature.rb
114
- - lib/dry/initializer/plugins/type_constraint.rb
115
- - lib/dry/initializer/plugins/variable_setter.rb
116
- - lib/dry/initializer/signature.rb
117
- - spec/base_spec.rb
118
- - spec/container_spec.rb
108
+ - lib/dry/initializer/mixin/local.rb
109
+ - lib/dry/initializer/mixin/root.rb
110
+ - lib/tasks/benchmark.rake
111
+ - lib/tasks/profile.rake
112
+ - spec/attributes_spec.rb
113
+ - spec/coercion_of_nil_spec.rb
114
+ - spec/custom_dispatchers_spec.rb
119
115
  - spec/custom_initializer_spec.rb
120
- - spec/default_nil_spec.rb
121
116
  - spec/default_values_spec.rb
117
+ - spec/definition_spec.rb
122
118
  - spec/invalid_default_spec.rb
123
119
  - spec/missed_default_spec.rb
124
120
  - spec/optional_spec.rb
125
121
  - spec/options_tolerance_spec.rb
126
- - spec/plugin_registry_spec.rb
122
+ - spec/public_attributes_utility_spec.rb
127
123
  - spec/reader_spec.rb
128
- - spec/renaming_options_spec.rb
129
124
  - spec/repetitive_definitions_spec.rb
125
+ - spec/several_assignments_spec.rb
130
126
  - spec/spec_helper.rb
131
127
  - spec/subclassing_spec.rb
132
128
  - spec/type_argument_spec.rb
@@ -144,7 +140,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
144
140
  requirements:
145
141
  - - ">="
146
142
  - !ruby/object:Gem::Version
147
- version: '2.2'
143
+ version: '2.3'
148
144
  required_rubygems_version: !ruby/object:Gem::Requirement
149
145
  requirements:
150
146
  - - ">="
@@ -152,24 +148,25 @@ required_rubygems_version: !ruby/object:Gem::Requirement
152
148
  version: '0'
153
149
  requirements: []
154
150
  rubyforge_project:
155
- rubygems_version: 2.5.2
151
+ rubygems_version: 2.6.14
156
152
  signing_key:
157
153
  specification_version: 4
158
154
  summary: DSL for declaring params and options of the initializer
159
155
  test_files:
160
- - spec/base_spec.rb
161
- - spec/container_spec.rb
156
+ - spec/attributes_spec.rb
157
+ - spec/coercion_of_nil_spec.rb
158
+ - spec/custom_dispatchers_spec.rb
162
159
  - spec/custom_initializer_spec.rb
163
- - spec/default_nil_spec.rb
164
160
  - spec/default_values_spec.rb
161
+ - spec/definition_spec.rb
165
162
  - spec/invalid_default_spec.rb
166
163
  - spec/missed_default_spec.rb
167
164
  - spec/optional_spec.rb
168
165
  - spec/options_tolerance_spec.rb
169
- - spec/plugin_registry_spec.rb
166
+ - spec/public_attributes_utility_spec.rb
170
167
  - spec/reader_spec.rb
171
- - spec/renaming_options_spec.rb
172
168
  - spec/repetitive_definitions_spec.rb
169
+ - spec/several_assignments_spec.rb
173
170
  - spec/spec_helper.rb
174
171
  - spec/subclassing_spec.rb
175
172
  - spec/type_argument_spec.rb
@@ -1,54 +0,0 @@
1
- Bundler.require(:benchmarks)
2
-
3
- require "dry-initializer"
4
- class NoOptsTest
5
- extend Dry::Initializer::Mixin
6
-
7
- param :foo
8
- option :bar
9
- end
10
-
11
- class DefaultsTest
12
- extend Dry::Initializer::Mixin
13
-
14
- param :foo, default: proc { "FOO" }
15
- option :bar, default: proc { "BAR" }
16
- end
17
-
18
- class TypesTest
19
- extend Dry::Initializer::Mixin
20
-
21
- param :foo, type: String
22
- option :bar, type: String
23
- end
24
-
25
- class DefaultsAndTypesTest
26
- extend Dry::Initializer::Mixin
27
-
28
- param :foo, type: String, default: proc { "FOO" }
29
- option :bar, type: String, default: proc { "BAR" }
30
- end
31
-
32
- puts "Benchmark for various options"
33
-
34
- Benchmark.ips do |x|
35
- x.config time: 15, warmup: 10
36
-
37
- x.report("no opts") do
38
- NoOptsTest.new "foo", bar: "bar"
39
- end
40
-
41
- x.report("with defaults") do
42
- DefaultsTest.new
43
- end
44
-
45
- x.report("with types") do
46
- TypesTest.new "foo", bar: "bar"
47
- end
48
-
49
- x.report("with defaults and types") do
50
- DefaultsAndTypesTest.new
51
- end
52
-
53
- x.compare!
54
- end
@@ -1,35 +0,0 @@
1
- Bundler.require(:benchmarks)
2
-
3
- require "dry-initializer"
4
-
5
- class ParamDefaults
6
- extend Dry::Initializer::Mixin
7
-
8
- param :foo, default: proc { "FOO" }
9
- param :bar, default: proc { "BAR" }
10
- param :baz, default: proc { "BAZ" }
11
- end
12
-
13
- class OptionDefaults
14
- extend Dry::Initializer::Mixin
15
-
16
- option :foo, default: proc { "FOO" }
17
- option :bar, default: proc { "BAR" }
18
- option :baz, default: proc { "BAZ" }
19
- end
20
-
21
- puts "Benchmark for param's vs. option's defaults"
22
-
23
- Benchmark.ips do |x|
24
- x.config time: 15, warmup: 10
25
-
26
- x.report("param's defaults") do
27
- ParamDefaults.new
28
- end
29
-
30
- x.report("option's defaults") do
31
- OptionDefaults.new
32
- end
33
-
34
- x.compare!
35
- end
@@ -1,100 +0,0 @@
1
- require "set"
2
- module Dry::Initializer
3
- # Rebuilds the initializer every time a new argument defined
4
- #
5
- # @api private
6
- #
7
- class Builder
8
- include Plugins
9
-
10
- def initialize
11
- @signature = Signature.new
12
- @plugins = Set.new [VariableSetter, TypeConstraint, DefaultProc]
13
- @parts = []
14
- end
15
-
16
- # Register new plugin to be applied as a chunk of code, or a proc
17
- # to be evaluated in the instance's scope
18
- #
19
- # @param [Dry::Initializer::Plugin]
20
- #
21
- # @return [Dry::Initializer::Builder]
22
- #
23
- def register(plugin)
24
- plugins = @plugins + [plugin]
25
- copy { @plugins = plugins }
26
- end
27
-
28
- # Defines new agrument and reloads mixin definitions
29
- #
30
- # @param [#to_sym] name
31
- # @param [Hash<Symbol, Object>] settings
32
- #
33
- # @return [Dry::Initializer::Builder]
34
- #
35
- def define(name, settings)
36
- signature = @signature.add(name, settings)
37
- parts = @parts + @plugins.map { |p| p.call(name, settings) }.compact
38
-
39
- copy do
40
- @signature = signature
41
- @parts = parts
42
- end
43
- end
44
-
45
- # Redeclares initializer and readers in the mixin module
46
- #
47
- # @param [Module] mixin
48
- #
49
- def call(mixin)
50
- define_readers(mixin)
51
- reload_initializer(mixin)
52
- reload_callback(mixin)
53
- mixin
54
- end
55
-
56
- private
57
-
58
- def copy(&block)
59
- dup.tap { |instance| instance.instance_eval(&block) }
60
- end
61
-
62
- def define_readers(mixin)
63
- define_reader(
64
- mixin,
65
- :attr_reader,
66
- ->(item) { item.settings[:reader] != false }
67
- )
68
- define_reader mixin, :private
69
- define_reader mixin, :protected
70
- end
71
-
72
- def define_reader(mixin, method, filter_lambda = nil)
73
- filter_lambda ||= ->(item) { item.settings[:reader] == method }
74
- readers = @signature.select(&filter_lambda).map(&:rename)
75
- mixin.send method, *readers if readers.any?
76
- end
77
-
78
- def reload_initializer(mixin)
79
- mixin.class_eval <<-RUBY, __FILE__, __LINE__ + 1
80
- def __initialize__(#{@signature.call})
81
- @__options__ = __options__
82
- #{@parts.select { |part| String === part }.join("\n")}
83
- __after_initialize__
84
- end
85
- RUBY
86
-
87
- mixin.send :private, :__initialize__
88
- end
89
-
90
- def reload_callback(mixin)
91
- blocks = @parts.select { |part| Proc === part }
92
-
93
- mixin.send :define_method, :__after_initialize__ do
94
- blocks.each { |block| instance_eval(&block) }
95
- end
96
-
97
- mixin.send :private, :__after_initialize__
98
- end
99
- end
100
- end
@@ -1,6 +0,0 @@
1
- class Dry::Initializer::Errors::DefaultValueError < TypeError
2
- def initialize(name, value)
3
- super "Cannot set #{value.inspect} directly as a default value" \
4
- " of the argument '#{name}'. Wrap it to either proc or lambda."
5
- end
6
- end
@@ -1,7 +0,0 @@
1
- class Dry::Initializer::Errors::OrderError < SyntaxError
2
- def initialize(name)
3
- super "Cannot define the required param '#{name}' after optional ones." \
4
- " Either provide a default value for the '#{name}', or declare it" \
5
- " before params with default values."
6
- end
7
- end
@@ -1,6 +0,0 @@
1
- class Dry::Initializer::Errors::PluginError < TypeError
2
- def initialize(plugin)
3
- super "#{plugin} is not a valid plugin." \
4
- " Use a subclass of Dry::Initialier::Plugins::Base."
5
- end
6
- end
@@ -1,5 +0,0 @@
1
- class Dry::Initializer::Errors::RedefinitionError < SyntaxError
2
- def initialize(name)
3
- super "The argument '#{name}' is already defined."
4
- end
5
- end
@@ -1,5 +0,0 @@
1
- class Dry::Initializer::Errors::TypeConstraintError < TypeError
2
- def initialize(name, type)
3
- super "#{type} used as constraint for argument '#{name}' is not a dry-type."
4
- end
5
- end
@@ -1,10 +0,0 @@
1
- module Dry::Initializer
2
- # Collection of gem-specific exceptions
3
- module Errors
4
- require_relative "errors/default_value_error"
5
- require_relative "errors/order_error"
6
- require_relative "errors/plugin_error"
7
- require_relative "errors/redefinition_error"
8
- require_relative "errors/type_constraint_error"
9
- end
10
- end
@@ -1,47 +0,0 @@
1
- module Dry::Initializer::Plugins
2
- # Base class for plugins
3
- #
4
- # A plugin should has class method [.call] that takes argument name and
5
- # settings and return a chunk of code for the #initialize method body.
6
- #
7
- class Base
8
- include Dry::Initializer::Errors
9
-
10
- # Builds the proc for the `__after_initializer__` callback
11
- #
12
- # @param [#to_s] name
13
- # @param [Hash<Symbol, Object>] settings
14
- #
15
- # @return [String, Proc, nil]
16
- #
17
- def self.call(name, settings)
18
- new(name, settings).call
19
- end
20
-
21
- # @private
22
- attr_reader :name, :settings
23
-
24
- # Initializes a builder with argument name and settings
25
- # @param (see .call)
26
- def initialize(name, settings)
27
- @name = name
28
- @settings = settings
29
- end
30
-
31
- # Checks equality to another instance by name
32
- # @return [Boolean]
33
- def ==(other)
34
- other.instance_of?(self.class) && (other.name == name)
35
- end
36
-
37
- # Builds a chunk of code
38
- # @return (see .call)
39
- def call; end
40
-
41
- # Returns the name for the attribute
42
- # @return (see .name)
43
- def rename
44
- @rename ||= settings[:option] ? (settings[:as] || name) : name
45
- end
46
- end
47
- end
@@ -1,28 +0,0 @@
1
- module Dry::Initializer::Plugins
2
- # Builds a block to be evaluated by initializer (__after_initialize__)
3
- # to assign a default value to the argument
4
- class DefaultProc < Base
5
- def call
6
- return unless default
7
-
8
- ivar = :"@#{rename}"
9
- default_proc = default
10
-
11
- proc do
12
- if instance_variable_get(ivar) == Dry::Initializer::UNDEFINED
13
- instance_variable_set ivar, instance_eval(&default_proc)
14
- end
15
- end
16
- end
17
-
18
- private
19
-
20
- def default
21
- return unless settings.key? :default
22
-
23
- @default ||= settings[:default].tap do |value|
24
- fail DefaultValueError.new(name, value) unless Proc === value
25
- end
26
- end
27
- end
28
- end
@@ -1,28 +0,0 @@
1
- module Dry::Initializer::Plugins
2
- # Plugin builds a chunk of code for the initializer's signature:
3
- #
4
- # @example
5
- # Signature.call(:user)
6
- # # => "user"
7
- #
8
- # Signature.call(:user, default: -> { nil })
9
- # # => "user = Dry::Initializer::UNDEFINED"
10
- #
11
- # Signature.call(:user, option: true)
12
- # # => nil
13
- #
14
- class Signature < Base
15
- def param?
16
- settings[:option] != true
17
- end
18
-
19
- def required?
20
- !settings.key?(:default) && !settings[:optional]
21
- end
22
-
23
- def call
24
- return unless param?
25
- required? ? name.to_s : "#{name} = Dry::Initializer::UNDEFINED"
26
- end
27
- end
28
- end
@@ -1,21 +0,0 @@
1
- module Dry::Initializer::Plugins
2
- # Plugin builds either chunk of code for the #initializer,
3
- # or a proc for the ##__after_initialize__ callback.
4
- class TypeConstraint < Base
5
- def call
6
- return unless settings.key? :type
7
-
8
- type = settings[:type]
9
- fail TypeConstraintError.new(rename, type) unless type.respond_to? :call
10
-
11
- ivar = :"@#{rename}"
12
- lambda do |*|
13
- value = instance_variable_get(ivar)
14
-
15
- if value != Dry::Initializer::UNDEFINED
16
- instance_variable_set ivar, type.call(value)
17
- end
18
- end
19
- end
20
- end
21
- end
@@ -1,30 +0,0 @@
1
- module Dry::Initializer::Plugins
2
- # Plugin builds a code for variable setter:
3
- #
4
- # @example
5
- # VariableSetter.call(:user, option: false)
6
- # # => "@user = user"
7
- #
8
- # VariableSetter.call(:user, option: true)
9
- # # => "@user = __options__.fetch(:user)"
10
- #
11
- # VariableSetter.call(:user, option: true, optional: true)
12
- # # => "@user = __options__.fetch(:user, Dry::Initializer::UNDEFINED)"
13
- #
14
- class VariableSetter < Base
15
- def param?
16
- settings[:option] != true
17
- end
18
-
19
- def required?
20
- !settings.key?(:default) && !settings[:optional]
21
- end
22
-
23
- def call
24
- return "@#{name} = #{name}" if param?
25
- key = ":\"#{name}\""
26
- return "@#{rename} = __options__.fetch(#{key})" if required?
27
- "@#{rename} = __options__.fetch(#{key}, Dry::Initializer::UNDEFINED)"
28
- end
29
- end
30
- end
@@ -1,10 +0,0 @@
1
- module Dry::Initializer
2
- # Namespace for code plugins builders
3
- module Plugins
4
- require_relative "plugins/base"
5
- require_relative "plugins/default_proc"
6
- require_relative "plugins/signature"
7
- require_relative "plugins/type_constraint"
8
- require_relative "plugins/variable_setter"
9
- end
10
- end