dry-initializer 1.4.1 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +8 -8
- data/.travis.yml +0 -3
- data/CHANGELOG.md +339 -196
- data/LICENSE.txt +1 -1
- data/README.md +3 -3
- data/Rakefile +2 -47
- data/benchmarks/{several_defaults.rb → compare_several_defaults.rb} +4 -4
- data/benchmarks/{without_options.rb → plain_options.rb} +20 -9
- data/benchmarks/{params.rb → plain_params.rb} +20 -9
- data/benchmarks/{with_types.rb → with_coercion.rb} +20 -9
- data/benchmarks/with_defaults.rb +19 -8
- data/benchmarks/{with_types_and_defaults.rb → with_defaults_and_coercion.rb} +21 -10
- data/dry-initializer.gemspec +3 -3
- data/lib/dry/initializer/builders/attribute.rb +76 -0
- data/lib/dry/initializer/builders/initializer.rb +61 -0
- data/lib/dry/initializer/builders/reader.rb +50 -0
- data/lib/dry/initializer/builders/signature.rb +32 -0
- data/lib/dry/initializer/builders.rb +7 -0
- data/lib/dry/initializer/config.rb +161 -0
- data/lib/dry/initializer/definition.rb +93 -0
- data/lib/dry/initializer/dsl.rb +43 -0
- data/lib/dry/initializer/mixin/local.rb +19 -0
- data/lib/dry/initializer/mixin/root.rb +10 -0
- data/lib/dry/initializer/mixin.rb +15 -0
- data/lib/dry/initializer.rb +45 -41
- data/lib/tasks/benchmark.rake +41 -0
- data/lib/tasks/profile.rake +78 -0
- data/spec/{options_var_spec.rb → attributes_spec.rb} +9 -9
- data/spec/custom_initializer_spec.rb +1 -1
- data/spec/default_values_spec.rb +6 -6
- data/spec/definition_spec.rb +21 -14
- data/spec/invalid_default_spec.rb +2 -2
- data/spec/missed_default_spec.rb +2 -2
- data/spec/optional_spec.rb +2 -2
- data/spec/options_tolerance_spec.rb +1 -1
- data/spec/public_attributes_utility_spec.rb +22 -0
- data/spec/reader_spec.rb +11 -11
- data/spec/repetitive_definitions_spec.rb +5 -5
- data/spec/several_assignments_spec.rb +1 -1
- data/spec/spec_helper.rb +5 -0
- data/spec/subclassing_spec.rb +7 -3
- data/spec/type_argument_spec.rb +1 -1
- data/spec/type_constraint_spec.rb +2 -2
- data/spec/value_coercion_via_dry_types_spec.rb +1 -1
- metadata +27 -27
- data/benchmarks/options.rb +0 -54
- data/benchmarks/params_vs_options.rb +0 -35
- data/benchmarks/profiler.rb +0 -28
- data/lib/dry/initializer/attribute.rb +0 -123
- data/lib/dry/initializer/builder.rb +0 -127
- data/lib/dry/initializer/class_dsl.rb +0 -37
- data/lib/dry/initializer/exceptions/default_value_error.rb +0 -8
- data/lib/dry/initializer/exceptions/params_order_error.rb +0 -8
- data/lib/dry/initializer/exceptions/type_constraint_error.rb +0 -7
- data/lib/dry/initializer/instance_dsl.rb +0 -15
- data/lib/dry/initializer/option.rb +0 -61
- data/lib/dry/initializer/param.rb +0 -52
- data/spec/gem_enhancement_spec.rb +0 -18
data/spec/definition_spec.rb
CHANGED
@@ -18,10 +18,10 @@ describe "definition" do
|
|
18
18
|
end
|
19
19
|
end
|
20
20
|
|
21
|
-
it_behaves_like :initializer, "extend Dry::Initializer
|
21
|
+
it_behaves_like :initializer, "extend Dry::Initializer" do
|
22
22
|
before do
|
23
23
|
class Test::Foo
|
24
|
-
extend Dry::Initializer
|
24
|
+
extend Dry::Initializer
|
25
25
|
param :foo
|
26
26
|
option :bar
|
27
27
|
end
|
@@ -41,10 +41,12 @@ describe "definition" do
|
|
41
41
|
it_behaves_like :initializer, "include Dry::Initializer with block" do
|
42
42
|
before do
|
43
43
|
class Test::Foo
|
44
|
-
include
|
45
|
-
|
46
|
-
|
47
|
-
|
44
|
+
include(
|
45
|
+
Dry::Initializer.define do
|
46
|
+
param :foo
|
47
|
+
option :bar
|
48
|
+
end
|
49
|
+
)
|
48
50
|
end
|
49
51
|
end
|
50
52
|
end
|
@@ -63,21 +65,26 @@ describe "definition" do
|
|
63
65
|
it_behaves_like :initializer, "include Dry::Initializer[undefined: false]" do
|
64
66
|
before do
|
65
67
|
class Test::Foo
|
66
|
-
include
|
67
|
-
|
68
|
-
|
69
|
-
|
68
|
+
include(
|
69
|
+
Dry::Initializer[undefined: false].define do
|
70
|
+
param :foo
|
71
|
+
option :bar
|
72
|
+
end
|
73
|
+
)
|
70
74
|
end
|
71
75
|
end
|
72
76
|
end
|
73
77
|
|
78
|
+
# @deprecated
|
74
79
|
it_behaves_like :initializer, "include Dry::Initializer::Mixin" do
|
75
80
|
before do
|
76
81
|
class Test::Foo
|
77
|
-
include
|
78
|
-
|
79
|
-
|
80
|
-
|
82
|
+
include(
|
83
|
+
Dry::Initializer::Mixin.define do
|
84
|
+
param :foo
|
85
|
+
option :bar
|
86
|
+
end
|
87
|
+
)
|
81
88
|
end
|
82
89
|
end
|
83
90
|
end
|
@@ -1,13 +1,13 @@
|
|
1
1
|
describe "invalid default value assignment" do
|
2
2
|
subject do
|
3
3
|
class Test::Foo
|
4
|
-
extend Dry::Initializer
|
4
|
+
extend Dry::Initializer
|
5
5
|
|
6
6
|
param :foo, default: 1
|
7
7
|
end
|
8
8
|
end
|
9
9
|
|
10
10
|
it "raises TypeError" do
|
11
|
-
expect { subject }.to raise_error TypeError
|
11
|
+
expect { subject }.to raise_error TypeError
|
12
12
|
end
|
13
13
|
end
|
data/spec/missed_default_spec.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
describe "missed default values" do
|
2
2
|
subject do
|
3
3
|
class Test::Foo
|
4
|
-
extend Dry::Initializer
|
4
|
+
extend Dry::Initializer
|
5
5
|
|
6
6
|
param :foo, default: proc { :FOO }
|
7
7
|
param :bar, required: true
|
@@ -9,6 +9,6 @@ describe "missed default values" do
|
|
9
9
|
end
|
10
10
|
|
11
11
|
it "raises SyntaxError" do
|
12
|
-
expect { subject }.to raise_error
|
12
|
+
expect { subject }.to raise_error SyntaxError, /bar/
|
13
13
|
end
|
14
14
|
end
|
data/spec/optional_spec.rb
CHANGED
@@ -2,7 +2,7 @@ describe "optional value" do
|
|
2
2
|
context "when has no default value" do
|
3
3
|
before do
|
4
4
|
class Test::Foo
|
5
|
-
extend Dry::Initializer
|
5
|
+
extend Dry::Initializer
|
6
6
|
|
7
7
|
param :foo
|
8
8
|
param :bar, optional: true
|
@@ -49,7 +49,7 @@ describe "optional value" do
|
|
49
49
|
context "when has a default value" do
|
50
50
|
before do
|
51
51
|
class Test::Foo
|
52
|
-
extend Dry::Initializer
|
52
|
+
extend Dry::Initializer
|
53
53
|
|
54
54
|
param :foo
|
55
55
|
param :bar, optional: true, default: proc { "baz" }
|
@@ -0,0 +1,22 @@
|
|
1
|
+
describe Dry::Initializer, ".dry_initializer.public_attributes" do
|
2
|
+
subject { instance.class.dry_initializer.public_attributes(instance) }
|
3
|
+
|
4
|
+
context "when class has params" do
|
5
|
+
before do
|
6
|
+
class Test::Foo
|
7
|
+
extend Dry::Initializer
|
8
|
+
param :foo, proc(&:to_s)
|
9
|
+
option :moo, optional: true
|
10
|
+
option :bar, default: proc { 1 }, reader: false
|
11
|
+
option :baz, optional: true, reader: :protected
|
12
|
+
option :qux, proc(&:to_s), as: :quxx, reader: :private
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
let(:instance) { Test::Foo.new(:FOO, bar: :BAR, baz: :BAZ, qux: :QUX) }
|
17
|
+
|
18
|
+
it "collects public options only" do
|
19
|
+
expect(subject).to eq({ foo: "FOO", moo: nil })
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
data/spec/reader_spec.rb
CHANGED
@@ -9,7 +9,7 @@ describe "reader" do
|
|
9
9
|
context "with reader: :public or no reader: option" do
|
10
10
|
subject do
|
11
11
|
class Test::Foo
|
12
|
-
extend Dry::Initializer
|
12
|
+
extend Dry::Initializer
|
13
13
|
|
14
14
|
param :foo
|
15
15
|
param :foo2, reader: :public
|
@@ -28,17 +28,17 @@ describe "reader" do
|
|
28
28
|
end
|
29
29
|
|
30
30
|
context "with reader: false" do
|
31
|
-
|
31
|
+
before do
|
32
32
|
class Test::Foo
|
33
|
-
extend Dry::Initializer
|
33
|
+
extend Dry::Initializer
|
34
34
|
|
35
35
|
param :foo, reader: false
|
36
36
|
option :bar, reader: false
|
37
37
|
end
|
38
|
-
|
39
|
-
Test::Foo.new 1, bar: 2
|
40
38
|
end
|
41
39
|
|
40
|
+
subject { Test::Foo.new 1, bar: 2 }
|
41
|
+
|
42
42
|
it_behaves_like "it has no public attr_reader"
|
43
43
|
|
44
44
|
it "keeps assigning variables" do
|
@@ -48,17 +48,17 @@ describe "reader" do
|
|
48
48
|
end
|
49
49
|
|
50
50
|
context "with reader: :private" do
|
51
|
-
|
51
|
+
before do
|
52
52
|
class Test::Foo
|
53
|
-
extend Dry::Initializer
|
53
|
+
extend Dry::Initializer
|
54
54
|
|
55
55
|
param :foo, reader: :private
|
56
56
|
option :bar, reader: :private
|
57
57
|
end
|
58
|
-
|
59
|
-
Test::Foo.new 1, bar: 2
|
60
58
|
end
|
61
59
|
|
60
|
+
subject { Test::Foo.new 1, bar: 2 }
|
61
|
+
|
62
62
|
it_behaves_like "it has no public attr_reader"
|
63
63
|
|
64
64
|
it "adds a private attr_reader" do
|
@@ -70,7 +70,7 @@ describe "reader" do
|
|
70
70
|
context "with reader: :protected" do
|
71
71
|
subject do
|
72
72
|
class Test::Foo
|
73
|
-
extend Dry::Initializer
|
73
|
+
extend Dry::Initializer
|
74
74
|
|
75
75
|
param :foo, reader: :protected
|
76
76
|
option :bar, reader: :protected
|
@@ -81,7 +81,7 @@ describe "reader" do
|
|
81
81
|
|
82
82
|
it "adds a protected attr_reader" do
|
83
83
|
protected_instance_methods = subject.class.protected_instance_methods
|
84
|
-
expect(protected_instance_methods).to match_array([
|
84
|
+
expect(protected_instance_methods).to match_array(%i[foo bar])
|
85
85
|
end
|
86
86
|
end
|
87
87
|
end
|
@@ -4,7 +4,7 @@ describe "repetitive definitions" do
|
|
4
4
|
context "of params" do
|
5
5
|
before do
|
6
6
|
class Test::Foo
|
7
|
-
extend Dry::Initializer
|
7
|
+
extend Dry::Initializer
|
8
8
|
|
9
9
|
param :foo, default: proc { 0 }
|
10
10
|
param :bar, default: proc { 1 }
|
@@ -20,7 +20,7 @@ describe "repetitive definitions" do
|
|
20
20
|
context "of options" do
|
21
21
|
before do
|
22
22
|
class Test::Foo
|
23
|
-
extend Dry::Initializer
|
23
|
+
extend Dry::Initializer
|
24
24
|
|
25
25
|
option :foo, default: proc { 0 }
|
26
26
|
option :bar, default: proc { 1 }
|
@@ -36,7 +36,7 @@ describe "repetitive definitions" do
|
|
36
36
|
context "of param and option" do
|
37
37
|
before do
|
38
38
|
class Test::Foo
|
39
|
-
extend Dry::Initializer
|
39
|
+
extend Dry::Initializer
|
40
40
|
|
41
41
|
param :foo, default: proc { 0 }
|
42
42
|
option :bar, default: proc { 1 }
|
@@ -52,9 +52,9 @@ describe "repetitive definitions" do
|
|
52
52
|
context "of optional param and option" do
|
53
53
|
before do
|
54
54
|
class Test::Foo
|
55
|
-
extend Dry::Initializer
|
55
|
+
extend Dry::Initializer
|
56
56
|
|
57
|
-
param :
|
57
|
+
param :baz, optional: true, as: :foo
|
58
58
|
option :bar, optional: true
|
59
59
|
option :foo, optional: true
|
60
60
|
end
|
data/spec/spec_helper.rb
CHANGED
data/spec/subclassing_spec.rb
CHANGED
@@ -1,8 +1,7 @@
|
|
1
1
|
describe "subclassing" do
|
2
2
|
before do
|
3
3
|
class Test::Foo
|
4
|
-
extend Dry::Initializer
|
5
|
-
|
4
|
+
extend Dry::Initializer[undefined: false]
|
6
5
|
param :foo
|
7
6
|
option :bar
|
8
7
|
end
|
@@ -21,6 +20,11 @@ describe "subclassing" do
|
|
21
20
|
Test::Bar.new 1, 2, bar: 3, qux: 4
|
22
21
|
end
|
23
22
|
|
23
|
+
it "preserves null definition" do
|
24
|
+
expect(Test::Foo.dry_initializer.null).to be_nil
|
25
|
+
expect(Test::Bar.dry_initializer.null).to be_nil
|
26
|
+
end
|
27
|
+
|
24
28
|
it "preserves definitions made in the superclass" do
|
25
29
|
expect(instance_of_subclass.foo).to eql 1
|
26
30
|
expect(instance_of_subclass.baz).to eql 2
|
@@ -37,7 +41,7 @@ describe "subclassing" do
|
|
37
41
|
called = false
|
38
42
|
mixin = Module.new { define_method(:inherited) { |_| called = true } }
|
39
43
|
|
40
|
-
base = Class.new { extend mixin; extend Dry::Initializer
|
44
|
+
base = Class.new { extend mixin; extend Dry::Initializer }
|
41
45
|
Class.new(base)
|
42
46
|
|
43
47
|
expect(called).to be true
|
data/spec/type_argument_spec.rb
CHANGED
@@ -4,7 +4,7 @@ describe "type constraint" do
|
|
4
4
|
context "by dry-type" do
|
5
5
|
before do
|
6
6
|
class Test::Foo
|
7
|
-
extend Dry::Initializer
|
7
|
+
extend Dry::Initializer
|
8
8
|
param :foo, Dry::Types["strict.string"], optional: true
|
9
9
|
end
|
10
10
|
end
|
@@ -39,7 +39,7 @@ describe "type constraint" do
|
|
39
39
|
it "raises TypeError" do
|
40
40
|
expect do
|
41
41
|
class Test::Foo
|
42
|
-
extend Dry::Initializer
|
42
|
+
extend Dry::Initializer
|
43
43
|
param :foo, type: String
|
44
44
|
end
|
45
45
|
end.to raise_error(TypeError)
|
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:
|
4
|
+
version: 2.0.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-
|
12
|
+
date: 2017-08-28 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rspec
|
@@ -68,9 +68,7 @@ dependencies:
|
|
68
68
|
- !ruby/object:Gem::Version
|
69
69
|
version: '0.42'
|
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,36 +86,37 @@ files:
|
|
88
86
|
- LICENSE.txt
|
89
87
|
- README.md
|
90
88
|
- Rakefile
|
91
|
-
- benchmarks/
|
92
|
-
- benchmarks/
|
93
|
-
- benchmarks/
|
94
|
-
- benchmarks/
|
95
|
-
- 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
|
96
93
|
- benchmarks/with_defaults.rb
|
97
|
-
- benchmarks/
|
98
|
-
- benchmarks/with_types_and_defaults.rb
|
99
|
-
- benchmarks/without_options.rb
|
94
|
+
- benchmarks/with_defaults_and_coercion.rb
|
100
95
|
- dry-initializer.gemspec
|
101
96
|
- lib/dry-initializer.rb
|
102
97
|
- lib/dry/initializer.rb
|
103
|
-
- lib/dry/initializer/
|
104
|
-
- lib/dry/initializer/
|
105
|
-
- lib/dry/initializer/
|
106
|
-
- lib/dry/initializer/
|
107
|
-
- lib/dry/initializer/
|
108
|
-
- lib/dry/initializer/
|
109
|
-
- lib/dry/initializer/
|
110
|
-
- lib/dry/initializer/
|
111
|
-
- lib/dry/initializer/
|
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/dsl.rb
|
106
|
+
- lib/dry/initializer/mixin.rb
|
107
|
+
- lib/dry/initializer/mixin/local.rb
|
108
|
+
- lib/dry/initializer/mixin/root.rb
|
109
|
+
- lib/tasks/benchmark.rake
|
110
|
+
- lib/tasks/profile.rake
|
111
|
+
- spec/attributes_spec.rb
|
112
112
|
- spec/custom_initializer_spec.rb
|
113
113
|
- spec/default_values_spec.rb
|
114
114
|
- spec/definition_spec.rb
|
115
|
-
- spec/gem_enhancement_spec.rb
|
116
115
|
- spec/invalid_default_spec.rb
|
117
116
|
- spec/missed_default_spec.rb
|
118
117
|
- spec/optional_spec.rb
|
119
118
|
- spec/options_tolerance_spec.rb
|
120
|
-
- spec/
|
119
|
+
- spec/public_attributes_utility_spec.rb
|
121
120
|
- spec/reader_spec.rb
|
122
121
|
- spec/repetitive_definitions_spec.rb
|
123
122
|
- spec/several_assignments_spec.rb
|
@@ -138,7 +137,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
138
137
|
requirements:
|
139
138
|
- - ">="
|
140
139
|
- !ruby/object:Gem::Version
|
141
|
-
version: '2.
|
140
|
+
version: '2.3'
|
142
141
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
143
142
|
requirements:
|
144
143
|
- - ">="
|
@@ -151,15 +150,15 @@ signing_key:
|
|
151
150
|
specification_version: 4
|
152
151
|
summary: DSL for declaring params and options of the initializer
|
153
152
|
test_files:
|
153
|
+
- spec/attributes_spec.rb
|
154
154
|
- spec/custom_initializer_spec.rb
|
155
155
|
- spec/default_values_spec.rb
|
156
156
|
- spec/definition_spec.rb
|
157
|
-
- spec/gem_enhancement_spec.rb
|
158
157
|
- spec/invalid_default_spec.rb
|
159
158
|
- spec/missed_default_spec.rb
|
160
159
|
- spec/optional_spec.rb
|
161
160
|
- spec/options_tolerance_spec.rb
|
162
|
-
- spec/
|
161
|
+
- spec/public_attributes_utility_spec.rb
|
163
162
|
- spec/reader_spec.rb
|
164
163
|
- spec/repetitive_definitions_spec.rb
|
165
164
|
- spec/several_assignments_spec.rb
|
@@ -168,3 +167,4 @@ test_files:
|
|
168
167
|
- spec/type_argument_spec.rb
|
169
168
|
- spec/type_constraint_spec.rb
|
170
169
|
- spec/value_coercion_via_dry_types_spec.rb
|
170
|
+
has_rdoc:
|
data/benchmarks/options.rb
DELETED
@@ -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, proc(&:to_s)
|
22
|
-
option :bar, proc(&:to_s)
|
23
|
-
end
|
24
|
-
|
25
|
-
class DefaultsAndTypesTest
|
26
|
-
extend Dry::Initializer::Mixin
|
27
|
-
|
28
|
-
param :foo, proc(&:to_s), default: proc { "FOO" }
|
29
|
-
option :bar, proc(&:to_s), 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
|
data/benchmarks/profiler.rb
DELETED
@@ -1,28 +0,0 @@
|
|
1
|
-
require "dry-initializer"
|
2
|
-
require "ruby-prof"
|
3
|
-
require "fileutils"
|
4
|
-
|
5
|
-
class User
|
6
|
-
extend Dry::Initializer
|
7
|
-
|
8
|
-
param :first_name, proc(&:to_s), default: proc { "Unknown" }
|
9
|
-
param :second_name, proc(&:to_s), default: proc { "Unknown" }
|
10
|
-
option :email, proc(&:to_s), optional: true
|
11
|
-
option :phone, proc(&:to_s), optional: true
|
12
|
-
end
|
13
|
-
|
14
|
-
result = RubyProf.profile do
|
15
|
-
1_000.times { User.new :Andy, email: :"andy@example.com" }
|
16
|
-
end
|
17
|
-
|
18
|
-
FileUtils.mkdir_p "./tmp"
|
19
|
-
|
20
|
-
FileUtils.touch "./tmp/profile.dot"
|
21
|
-
File.open("./tmp/profile.dot", "w+") do |output|
|
22
|
-
RubyProf::DotPrinter.new(result).print(output, min_percent: 0)
|
23
|
-
end
|
24
|
-
|
25
|
-
FileUtils.touch "./tmp/profile.html"
|
26
|
-
File.open("./tmp/profile.html", "w+") do |output|
|
27
|
-
RubyProf::CallStackPrinter.new(result).print(output, min_percent: 0)
|
28
|
-
end
|