dry-initializer 3.0.2 → 3.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/ISSUE_TEMPLATE/---bug-report.md +1 -5
- data/.github/workflows/custom_ci.yml +31 -47
- data/.github/workflows/sync_configs.yml +29 -7
- data/.rspec +1 -1
- data/.rubocop.yml +19 -6
- data/CHANGELOG.md +6 -0
- data/Gemfile +27 -29
- data/Gemfile.devtools +16 -0
- data/Guardfile +3 -3
- data/LICENSE +1 -1
- data/README.md +17 -77
- data/Rakefile +4 -4
- data/benchmarks/compare_several_defaults.rb +27 -27
- data/benchmarks/plain_options.rb +14 -14
- data/benchmarks/plain_params.rb +22 -22
- data/benchmarks/with_coercion.rb +14 -14
- data/benchmarks/with_defaults.rb +17 -17
- data/benchmarks/with_defaults_and_coercion.rb +14 -14
- data/bin/.gitkeep +0 -0
- data/dry-initializer.gemspec +13 -13
- data/lib/dry-initializer.rb +1 -1
- data/lib/dry/initializer.rb +9 -9
- 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 +6 -4
- data/lib/dry/initializer/definition.rb +9 -9
- data/lib/dry/initializer/dispatchers.rb +10 -10
- data/lib/dry/initializer/dispatchers/prepare_default.rb +2 -2
- data/lib/dry/initializer/dispatchers/prepare_ivar.rb +1 -1
- data/lib/dry/initializer/dispatchers/prepare_reader.rb +3 -3
- data/lib/dry/initializer/dispatchers/wrap_type.rb +1 -0
- data/lib/dry/initializer/mixin.rb +4 -4
- data/lib/dry/initializer/version.rb +5 -0
- data/lib/tasks/benchmark.rake +13 -13
- data/lib/tasks/profile.rake +16 -16
- data/project.yml +2 -0
- data/spec/attributes_spec.rb +7 -7
- data/spec/coercion_of_nil_spec.rb +3 -3
- data/spec/custom_dispatchers_spec.rb +6 -6
- data/spec/custom_initializer_spec.rb +2 -2
- data/spec/default_values_spec.rb +9 -9
- data/spec/definition_spec.rb +10 -10
- data/spec/invalid_default_spec.rb +2 -2
- data/spec/list_type_spec.rb +8 -8
- data/spec/missed_default_spec.rb +2 -2
- data/spec/nested_type_spec.rb +10 -10
- data/spec/optional_spec.rb +16 -16
- data/spec/options_tolerance_spec.rb +2 -2
- data/spec/public_attributes_utility_spec.rb +5 -5
- data/spec/reader_spec.rb +13 -13
- data/spec/repetitive_definitions_spec.rb +9 -9
- data/spec/several_assignments_spec.rb +9 -9
- data/spec/spec_helper.rb +3 -8
- data/spec/subclassing_spec.rb +5 -5
- data/spec/support/coverage.rb +7 -0
- data/spec/support/warnings.rb +7 -0
- data/spec/type_argument_spec.rb +13 -13
- data/spec/type_constraint_spec.rb +44 -26
- data/spec/value_coercion_via_dry_types_spec.rb +7 -7
- metadata +11 -31
data/Rakefile
CHANGED
@@ -1,8 +1,8 @@
|
|
1
|
-
require
|
1
|
+
require 'bundler/setup'
|
2
2
|
Bundler::GemHelper.install_tasks
|
3
3
|
|
4
|
-
require
|
4
|
+
require 'rspec/core/rake_task'
|
5
5
|
RSpec::Core::RakeTask.new :default
|
6
6
|
|
7
|
-
load
|
8
|
-
load
|
7
|
+
load 'lib/tasks/benchmark.rake'
|
8
|
+
load 'lib/tasks/profile.rake'
|
@@ -1,6 +1,6 @@
|
|
1
1
|
Bundler.require(:benchmarks)
|
2
2
|
|
3
|
-
require
|
3
|
+
require 'dry-initializer'
|
4
4
|
class WithoutDefaults
|
5
5
|
extend Dry::Initializer
|
6
6
|
|
@@ -14,67 +14,67 @@ class WithOneDefault
|
|
14
14
|
|
15
15
|
param :foo
|
16
16
|
param :bar
|
17
|
-
param :baz, default: proc {
|
17
|
+
param :baz, default: proc { 'BAZ' }
|
18
18
|
end
|
19
19
|
|
20
20
|
class WithTwoDefaults
|
21
21
|
extend Dry::Initializer
|
22
22
|
|
23
23
|
param :foo
|
24
|
-
param :bar, default: proc {
|
25
|
-
param :baz, default: proc {
|
24
|
+
param :bar, default: proc { 'BAR' }
|
25
|
+
param :baz, default: proc { 'BAZ' }
|
26
26
|
end
|
27
27
|
|
28
28
|
class WithThreeDefaults
|
29
29
|
extend Dry::Initializer
|
30
30
|
|
31
|
-
param :foo, default: proc {
|
32
|
-
param :bar, default: proc {
|
33
|
-
param :baz, default: proc {
|
31
|
+
param :foo, default: proc { 'FOO' }
|
32
|
+
param :bar, default: proc { 'BAR' }
|
33
|
+
param :baz, default: proc { 'BAZ' }
|
34
34
|
end
|
35
35
|
|
36
|
-
puts
|
36
|
+
puts 'Benchmark for various options'
|
37
37
|
|
38
38
|
Benchmark.ips do |x|
|
39
39
|
x.config time: 15, warmup: 10
|
40
40
|
|
41
|
-
x.report(
|
42
|
-
WithoutDefaults.new
|
41
|
+
x.report('without defaults') do
|
42
|
+
WithoutDefaults.new 'FOO', 'BAR', 'BAZ'
|
43
43
|
end
|
44
44
|
|
45
|
-
x.report(
|
46
|
-
WithOneDefault.new
|
45
|
+
x.report('with 0 of 1 default used') do
|
46
|
+
WithOneDefault.new 'FOO', 'BAR', 'BAZ'
|
47
47
|
end
|
48
48
|
|
49
|
-
x.report(
|
50
|
-
WithOneDefault.new
|
49
|
+
x.report('with 1 of 1 default used') do
|
50
|
+
WithOneDefault.new 'FOO', 'BAR'
|
51
51
|
end
|
52
52
|
|
53
|
-
x.report(
|
54
|
-
WithTwoDefaults.new
|
53
|
+
x.report('with 0 of 2 defaults used') do
|
54
|
+
WithTwoDefaults.new 'FOO', 'BAR', 'BAZ'
|
55
55
|
end
|
56
56
|
|
57
|
-
x.report(
|
58
|
-
WithTwoDefaults.new
|
57
|
+
x.report('with 1 of 2 defaults used') do
|
58
|
+
WithTwoDefaults.new 'FOO', 'BAR'
|
59
59
|
end
|
60
60
|
|
61
|
-
x.report(
|
62
|
-
WithTwoDefaults.new
|
61
|
+
x.report('with 2 of 2 defaults used') do
|
62
|
+
WithTwoDefaults.new 'FOO'
|
63
63
|
end
|
64
64
|
|
65
|
-
x.report(
|
66
|
-
WithThreeDefaults.new
|
65
|
+
x.report('with 0 of 3 defaults used') do
|
66
|
+
WithThreeDefaults.new 'FOO', 'BAR', 'BAZ'
|
67
67
|
end
|
68
68
|
|
69
|
-
x.report(
|
70
|
-
WithThreeDefaults.new
|
69
|
+
x.report('with 1 of 3 defaults used') do
|
70
|
+
WithThreeDefaults.new 'FOO', 'BAR'
|
71
71
|
end
|
72
72
|
|
73
|
-
x.report(
|
74
|
-
WithThreeDefaults.new
|
73
|
+
x.report('with 2 of 3 defaults used') do
|
74
|
+
WithThreeDefaults.new 'FOO'
|
75
75
|
end
|
76
76
|
|
77
|
-
x.report(
|
77
|
+
x.report('with 3 of 3 defaults used') do
|
78
78
|
WithThreeDefaults.new
|
79
79
|
end
|
80
80
|
|
data/benchmarks/plain_options.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
Bundler.require(:benchmarks)
|
2
2
|
|
3
|
-
require
|
3
|
+
require 'dry-initializer'
|
4
4
|
class DryTest
|
5
5
|
extend Dry::Initializer[undefined: false]
|
6
6
|
|
@@ -24,39 +24,39 @@ class PlainRubyTest
|
|
24
24
|
end
|
25
25
|
end
|
26
26
|
|
27
|
-
require
|
27
|
+
require 'anima'
|
28
28
|
class AnimaTest
|
29
29
|
include Anima.new(:foo, :bar)
|
30
30
|
end
|
31
31
|
|
32
|
-
require
|
32
|
+
require 'kwattr'
|
33
33
|
class KwattrTest
|
34
34
|
kwattr :foo, :bar
|
35
35
|
end
|
36
36
|
|
37
|
-
puts
|
37
|
+
puts 'Benchmark for instantiation with plain options'
|
38
38
|
|
39
39
|
Benchmark.ips do |x|
|
40
40
|
x.config time: 15, warmup: 10
|
41
41
|
|
42
|
-
x.report(
|
43
|
-
PlainRubyTest.new foo:
|
42
|
+
x.report('plain Ruby') do
|
43
|
+
PlainRubyTest.new foo: 'FOO', bar: 'BAR'
|
44
44
|
end
|
45
45
|
|
46
|
-
x.report(
|
47
|
-
DryTest.new foo:
|
46
|
+
x.report('dry-initializer') do
|
47
|
+
DryTest.new foo: 'FOO', bar: 'BAR'
|
48
48
|
end
|
49
49
|
|
50
|
-
x.report(
|
51
|
-
DryTestUndefined.new foo:
|
50
|
+
x.report('dry-initializer (with UNDEFINED)') do
|
51
|
+
DryTestUndefined.new foo: 'FOO', bar: 'BAR'
|
52
52
|
end
|
53
53
|
|
54
|
-
x.report(
|
55
|
-
AnimaTest.new foo:
|
54
|
+
x.report('anima') do
|
55
|
+
AnimaTest.new foo: 'FOO', bar: 'BAR'
|
56
56
|
end
|
57
57
|
|
58
|
-
x.report(
|
59
|
-
KwattrTest.new foo:
|
58
|
+
x.report('kwattr') do
|
59
|
+
KwattrTest.new foo: 'FOO', bar: 'BAR'
|
60
60
|
end
|
61
61
|
|
62
62
|
x.compare!
|
data/benchmarks/plain_params.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
Bundler.require(:benchmarks)
|
2
2
|
|
3
|
-
require
|
3
|
+
require 'dry-initializer'
|
4
4
|
class DryTest
|
5
5
|
extend Dry::Initializer[undefined: false]
|
6
6
|
|
@@ -26,58 +26,58 @@ end
|
|
26
26
|
|
27
27
|
StructTest = Struct.new(:foo, :bar)
|
28
28
|
|
29
|
-
require
|
29
|
+
require 'concord'
|
30
30
|
class ConcordTest
|
31
31
|
include Concord.new(:foo, :bar)
|
32
32
|
end
|
33
33
|
|
34
|
-
require
|
34
|
+
require 'values'
|
35
35
|
ValueTest = Value.new(:foo, :bar)
|
36
36
|
|
37
|
-
require
|
37
|
+
require 'value_struct'
|
38
38
|
ValueStructTest = ValueStruct.new(:foo, :bar)
|
39
39
|
|
40
|
-
require
|
40
|
+
require 'attr_extras'
|
41
41
|
class AttrExtrasText
|
42
42
|
attr_initialize :foo, :bar
|
43
43
|
attr_reader :foo, :bar
|
44
44
|
end
|
45
45
|
|
46
|
-
puts
|
46
|
+
puts 'Benchmark for instantiation with plain params'
|
47
47
|
|
48
48
|
Benchmark.ips do |x|
|
49
49
|
x.config time: 15, warmup: 10
|
50
50
|
|
51
|
-
x.report(
|
52
|
-
PlainRubyTest.new
|
51
|
+
x.report('plain Ruby') do
|
52
|
+
PlainRubyTest.new 'FOO', 'BAR'
|
53
53
|
end
|
54
54
|
|
55
|
-
x.report(
|
56
|
-
StructTest.new
|
55
|
+
x.report('Core Struct') do
|
56
|
+
StructTest.new 'FOO', 'BAR'
|
57
57
|
end
|
58
58
|
|
59
|
-
x.report(
|
60
|
-
ValueTest.new
|
59
|
+
x.report('values') do
|
60
|
+
ValueTest.new 'FOO', 'BAR'
|
61
61
|
end
|
62
62
|
|
63
|
-
x.report(
|
64
|
-
ValueStructTest.new
|
63
|
+
x.report('value_struct') do
|
64
|
+
ValueStructTest.new 'FOO', 'BAR'
|
65
65
|
end
|
66
66
|
|
67
|
-
x.report(
|
68
|
-
DryTest.new
|
67
|
+
x.report('dry-initializer') do
|
68
|
+
DryTest.new 'FOO', 'BAR'
|
69
69
|
end
|
70
70
|
|
71
|
-
x.report(
|
72
|
-
DryTestUndefined.new
|
71
|
+
x.report('dry-initializer (with UNDEFINED)') do
|
72
|
+
DryTestUndefined.new 'FOO', 'BAR'
|
73
73
|
end
|
74
74
|
|
75
|
-
x.report(
|
76
|
-
ConcordTest.new
|
75
|
+
x.report('concord') do
|
76
|
+
ConcordTest.new 'FOO', 'BAR'
|
77
77
|
end
|
78
78
|
|
79
|
-
x.report(
|
80
|
-
AttrExtrasText.new
|
79
|
+
x.report('attr_extras') do
|
80
|
+
AttrExtrasText.new 'FOO', 'BAR'
|
81
81
|
end
|
82
82
|
|
83
83
|
x.compare!
|
data/benchmarks/with_coercion.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
Bundler.require(:benchmarks)
|
2
2
|
|
3
|
-
require
|
3
|
+
require 'dry-initializer'
|
4
4
|
class DryTest
|
5
5
|
extend Dry::Initializer[undefined: false]
|
6
6
|
|
@@ -24,7 +24,7 @@ class PlainRubyTest
|
|
24
24
|
end
|
25
25
|
end
|
26
26
|
|
27
|
-
require
|
27
|
+
require 'virtus'
|
28
28
|
class VirtusTest
|
29
29
|
include Virtus.model
|
30
30
|
|
@@ -32,7 +32,7 @@ class VirtusTest
|
|
32
32
|
attribute :bar, String
|
33
33
|
end
|
34
34
|
|
35
|
-
require
|
35
|
+
require 'fast_attributes'
|
36
36
|
class FastAttributesTest
|
37
37
|
extend FastAttributes
|
38
38
|
|
@@ -42,29 +42,29 @@ class FastAttributesTest
|
|
42
42
|
end
|
43
43
|
end
|
44
44
|
|
45
|
-
puts
|
45
|
+
puts 'Benchmark for instantiation with coercion'
|
46
46
|
|
47
47
|
Benchmark.ips do |x|
|
48
48
|
x.config time: 15, warmup: 10
|
49
49
|
|
50
|
-
x.report(
|
51
|
-
PlainRubyTest.new foo:
|
50
|
+
x.report('plain Ruby') do
|
51
|
+
PlainRubyTest.new foo: 'FOO', bar: 'BAR'
|
52
52
|
end
|
53
53
|
|
54
|
-
x.report(
|
55
|
-
DryTest.new foo:
|
54
|
+
x.report('dry-initializer') do
|
55
|
+
DryTest.new foo: 'FOO', bar: 'BAR'
|
56
56
|
end
|
57
57
|
|
58
|
-
x.report(
|
59
|
-
DryTestUndefined.new foo:
|
58
|
+
x.report('dry-initializer (with UNDEFINED)') do
|
59
|
+
DryTestUndefined.new foo: 'FOO', bar: 'BAR'
|
60
60
|
end
|
61
61
|
|
62
|
-
x.report(
|
63
|
-
VirtusTest.new foo:
|
62
|
+
x.report('virtus') do
|
63
|
+
VirtusTest.new foo: 'FOO', bar: 'BAR'
|
64
64
|
end
|
65
65
|
|
66
|
-
x.report(
|
67
|
-
FastAttributesTest.new foo:
|
66
|
+
x.report('fast_attributes') do
|
67
|
+
FastAttributesTest.new foo: 'FOO', bar: 'BAR'
|
68
68
|
end
|
69
69
|
|
70
70
|
x.compare!
|
data/benchmarks/with_defaults.rb
CHANGED
@@ -1,64 +1,64 @@
|
|
1
1
|
Bundler.require(:benchmarks)
|
2
2
|
|
3
|
-
require
|
3
|
+
require 'dry-initializer'
|
4
4
|
class DryTest
|
5
5
|
extend Dry::Initializer[undefined: false]
|
6
6
|
|
7
|
-
option :foo, default: -> {
|
8
|
-
option :bar, default: -> {
|
7
|
+
option :foo, default: -> { 'FOO' }
|
8
|
+
option :bar, default: -> { 'BAR' }
|
9
9
|
end
|
10
10
|
|
11
11
|
class DryTestUndefined
|
12
12
|
extend Dry::Initializer
|
13
13
|
|
14
|
-
option :foo, default: -> {
|
15
|
-
option :bar, default: -> {
|
14
|
+
option :foo, default: -> { 'FOO' }
|
15
|
+
option :bar, default: -> { 'BAR' }
|
16
16
|
end
|
17
17
|
|
18
18
|
class PlainRubyTest
|
19
19
|
attr_reader :foo, :bar
|
20
20
|
|
21
|
-
def initialize(foo:
|
21
|
+
def initialize(foo: 'FOO', bar: 'BAR')
|
22
22
|
@foo = foo
|
23
23
|
@bar = bar
|
24
24
|
end
|
25
25
|
end
|
26
26
|
|
27
|
-
require
|
27
|
+
require 'kwattr'
|
28
28
|
class KwattrTest
|
29
|
-
kwattr foo:
|
29
|
+
kwattr foo: 'FOO', bar: 'BAR'
|
30
30
|
end
|
31
31
|
|
32
|
-
require
|
32
|
+
require 'active_attr'
|
33
33
|
class ActiveAttrTest
|
34
34
|
include ActiveAttr::AttributeDefaults
|
35
35
|
|
36
|
-
attribute :foo, default:
|
37
|
-
attribute :bar, default:
|
36
|
+
attribute :foo, default: 'FOO'
|
37
|
+
attribute :bar, default: 'BAR'
|
38
38
|
end
|
39
39
|
|
40
|
-
puts
|
40
|
+
puts 'Benchmark for instantiation with default values'
|
41
41
|
|
42
42
|
Benchmark.ips do |x|
|
43
43
|
x.config time: 15, warmup: 10
|
44
44
|
|
45
|
-
x.report(
|
45
|
+
x.report('plain Ruby') do
|
46
46
|
PlainRubyTest.new
|
47
47
|
end
|
48
48
|
|
49
|
-
x.report(
|
49
|
+
x.report('dry-initializer') do
|
50
50
|
DryTest.new
|
51
51
|
end
|
52
52
|
|
53
|
-
x.report(
|
53
|
+
x.report('dry-initializer (with UNDEFINED)') do
|
54
54
|
DryTestUndefined.new
|
55
55
|
end
|
56
56
|
|
57
|
-
x.report(
|
57
|
+
x.report('kwattr') do
|
58
58
|
KwattrTest.new
|
59
59
|
end
|
60
60
|
|
61
|
-
x.report(
|
61
|
+
x.report('active_attr') do
|
62
62
|
ActiveAttrTest.new
|
63
63
|
end
|
64
64
|
|
@@ -1,24 +1,24 @@
|
|
1
1
|
Bundler.require(:benchmarks)
|
2
2
|
|
3
|
-
require
|
3
|
+
require 'dry-initializer'
|
4
4
|
class DryTest
|
5
5
|
extend Dry::Initializer[undefined: false]
|
6
6
|
|
7
|
-
option :foo, proc(&:to_s), default: -> {
|
8
|
-
option :bar, proc(&:to_s), default: -> {
|
7
|
+
option :foo, proc(&:to_s), default: -> { 'FOO' }
|
8
|
+
option :bar, proc(&:to_s), default: -> { 'BAR' }
|
9
9
|
end
|
10
10
|
|
11
11
|
class DryTestUndefined
|
12
12
|
extend Dry::Initializer
|
13
13
|
|
14
|
-
option :foo, proc(&:to_s), default: -> {
|
15
|
-
option :bar, proc(&:to_s), default: -> {
|
14
|
+
option :foo, proc(&:to_s), default: -> { 'FOO' }
|
15
|
+
option :bar, proc(&:to_s), default: -> { 'BAR' }
|
16
16
|
end
|
17
17
|
|
18
18
|
class PlainRubyTest
|
19
19
|
attr_reader :foo, :bar
|
20
20
|
|
21
|
-
def initialize(foo:
|
21
|
+
def initialize(foo: 'FOO', bar: 'BAR')
|
22
22
|
@foo = foo
|
23
23
|
@bar = bar
|
24
24
|
raise TypeError unless String === @foo
|
@@ -26,32 +26,32 @@ class PlainRubyTest
|
|
26
26
|
end
|
27
27
|
end
|
28
28
|
|
29
|
-
require
|
29
|
+
require 'virtus'
|
30
30
|
class VirtusTest
|
31
31
|
include Virtus.model
|
32
32
|
|
33
|
-
attribute :foo, String, default:
|
34
|
-
attribute :bar, String, default:
|
33
|
+
attribute :foo, String, default: 'FOO'
|
34
|
+
attribute :bar, String, default: 'BAR'
|
35
35
|
end
|
36
36
|
|
37
|
-
puts
|
37
|
+
puts 'Benchmark for instantiation with type constraints and default values'
|
38
38
|
|
39
39
|
Benchmark.ips do |x|
|
40
40
|
x.config time: 15, warmup: 10
|
41
41
|
|
42
|
-
x.report(
|
42
|
+
x.report('plain Ruby') do
|
43
43
|
PlainRubyTest.new
|
44
44
|
end
|
45
45
|
|
46
|
-
x.report(
|
46
|
+
x.report('dry-initializer') do
|
47
47
|
DryTest.new
|
48
48
|
end
|
49
49
|
|
50
|
-
x.report(
|
50
|
+
x.report('dry-initializer (with UNDEFINED)') do
|
51
51
|
DryTest.new
|
52
52
|
end
|
53
53
|
|
54
|
-
x.report(
|
54
|
+
x.report('virtus') do
|
55
55
|
VirtusTest.new
|
56
56
|
end
|
57
57
|
|