dry-initializer 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (47) hide show
  1. checksums.yaml +7 -0
  2. data/.codeclimate.yml +15 -0
  3. data/.gitignore +9 -0
  4. data/.rspec +3 -0
  5. data/.rubocop.yml +91 -0
  6. data/.travis.yml +16 -0
  7. data/CHANGELOG.md +3 -0
  8. data/Gemfile +25 -0
  9. data/Guardfile +5 -0
  10. data/LICENSE.txt +21 -0
  11. data/README.md +444 -0
  12. data/Rakefile +47 -0
  13. data/benchmarks/options.rb +54 -0
  14. data/benchmarks/params.rb +73 -0
  15. data/benchmarks/params_vs_options.rb +35 -0
  16. data/benchmarks/several_defaults.rb +82 -0
  17. data/benchmarks/with_defaults.rb +55 -0
  18. data/benchmarks/with_types.rb +62 -0
  19. data/benchmarks/with_types_and_defaults.rb +48 -0
  20. data/benchmarks/without_options.rb +52 -0
  21. data/dry-initializer.gemspec +19 -0
  22. data/lib/dry-initializer.rb +1 -0
  23. data/lib/dry/initializer.rb +53 -0
  24. data/lib/dry/initializer/argument.rb +96 -0
  25. data/lib/dry/initializer/arguments.rb +85 -0
  26. data/lib/dry/initializer/builder.rb +33 -0
  27. data/lib/dry/initializer/errors.rb +13 -0
  28. data/lib/dry/initializer/errors/existing_argument_error.rb +5 -0
  29. data/lib/dry/initializer/errors/invalid_default_value_error.rb +6 -0
  30. data/lib/dry/initializer/errors/invalid_type_error.rb +6 -0
  31. data/lib/dry/initializer/errors/key_error.rb +5 -0
  32. data/lib/dry/initializer/errors/missed_default_value_error.rb +5 -0
  33. data/lib/dry/initializer/errors/type_error.rb +5 -0
  34. data/spec/dry/base_spec.rb +21 -0
  35. data/spec/dry/default_nil_spec.rb +17 -0
  36. data/spec/dry/default_values_spec.rb +39 -0
  37. data/spec/dry/dry_type_spec.rb +25 -0
  38. data/spec/dry/invalid_default_spec.rb +13 -0
  39. data/spec/dry/invalid_type_spec.rb +13 -0
  40. data/spec/dry/missed_default_spec.rb +14 -0
  41. data/spec/dry/poro_type_spec.rb +33 -0
  42. data/spec/dry/proc_type_spec.rb +25 -0
  43. data/spec/dry/reader_spec.rb +22 -0
  44. data/spec/dry/repetitive_definitions_spec.rb +49 -0
  45. data/spec/dry/subclassing_spec.rb +24 -0
  46. data/spec/spec_helper.rb +15 -0
  47. metadata +149 -0
data/Rakefile ADDED
@@ -0,0 +1,47 @@
1
+ require "bundler/setup"
2
+ Bundler::GemHelper.install_tasks
3
+
4
+ require "rspec/core/rake_task"
5
+ RSpec::Core::RakeTask.new :default
6
+
7
+ namespace :benchmark do
8
+ desc "Runs benchmarks without options"
9
+ task :without_options do
10
+ system "ruby benchmarks/without_options.rb"
11
+ end
12
+
13
+ desc "Runs benchmarks for several defaults"
14
+ task :several_defaults do
15
+ system "ruby benchmarks/several_defaults.rb"
16
+ end
17
+
18
+ desc "Runs benchmarks for defaults of params vs. options"
19
+ task :params_vs_options do
20
+ system "ruby benchmarks/params_vs_options.rb"
21
+ end
22
+
23
+ desc "Runs benchmarks with types"
24
+ task :with_types do
25
+ system "ruby benchmarks/with_types.rb"
26
+ end
27
+
28
+ desc "Runs benchmarks with defaults"
29
+ task :with_defaults do
30
+ system "ruby benchmarks/with_defaults.rb"
31
+ end
32
+
33
+ desc "Runs benchmarks with types and defaults"
34
+ task :with_types_and_defaults do
35
+ system "ruby benchmarks/with_types_and_defaults.rb"
36
+ end
37
+
38
+ desc "Runs benchmarks for plain params"
39
+ task :params do
40
+ system "ruby benchmarks/params.rb"
41
+ end
42
+
43
+ desc "Runs benchmarks various opts"
44
+ task :options do
45
+ system "ruby benchmarks/options.rb"
46
+ end
47
+ end
@@ -0,0 +1,54 @@
1
+ Bundler.require(:benchmarks)
2
+
3
+ require "dry-initializer"
4
+ class NoOptsTest
5
+ extend Dry::Initializer
6
+
7
+ param :foo
8
+ option :bar
9
+ end
10
+
11
+ class DefaultsTest
12
+ extend Dry::Initializer
13
+
14
+ param :foo, default: proc { "FOO" }
15
+ option :bar, default: proc { "BAR" }
16
+ end
17
+
18
+ class TypesTest
19
+ extend Dry::Initializer
20
+
21
+ param :foo, type: String
22
+ option :bar, type: String
23
+ end
24
+
25
+ class DefaultsAndTypesTest
26
+ extend Dry::Initializer
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
@@ -0,0 +1,73 @@
1
+ Bundler.require(:benchmarks)
2
+
3
+ class PlainRubyTest
4
+ attr_reader :foo, :bar
5
+
6
+ def initialize(foo, bar)
7
+ @foo = foo
8
+ @bar = bar
9
+ end
10
+ end
11
+
12
+ StructTest = Struct.new(:foo, :bar)
13
+
14
+ require "dry-initializer"
15
+ class DryTest
16
+ extend Dry::Initializer
17
+
18
+ param :foo
19
+ param :bar
20
+ end
21
+
22
+ require "concord"
23
+ class ConcordTest
24
+ include Concord.new(:foo, :bar)
25
+ end
26
+
27
+ require "values"
28
+ ValueTest = Value.new(:foo, :bar)
29
+
30
+ require "value_struct"
31
+ ValueStructTest = ValueStruct.new(:foo, :bar)
32
+
33
+ require "attr_extras"
34
+ class AttrExtrasText
35
+ attr_initialize :foo, :bar
36
+ attr_reader :foo, :bar
37
+ end
38
+
39
+ puts "Benchmark for instantiation of plain params"
40
+
41
+ Benchmark.ips do |x|
42
+ x.config time: 15, warmup: 10
43
+
44
+ x.report("plain Ruby") do
45
+ PlainRubyTest.new "FOO", "BAR"
46
+ end
47
+
48
+ x.report("Core Struct") do
49
+ StructTest.new "FOO", "BAR"
50
+ end
51
+
52
+ x.report("values") do
53
+ ValueTest.new "FOO", "BAR"
54
+ end
55
+
56
+ x.report("value_struct") do
57
+ ValueStructTest.new "FOO", "BAR"
58
+ end
59
+
60
+ x.report("dry-initializer") do
61
+ DryTest.new "FOO", "BAR"
62
+ end
63
+
64
+ x.report("concord") do
65
+ ConcordTest.new "FOO", "BAR"
66
+ end
67
+
68
+ x.report("attr_extras") do
69
+ AttrExtrasText.new "FOO", "BAR"
70
+ end
71
+
72
+ x.compare!
73
+ end
@@ -0,0 +1,35 @@
1
+ Bundler.require(:benchmarks)
2
+
3
+ require "dry-initializer"
4
+
5
+ class ParamDefaults
6
+ extend Dry::Initializer
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
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
@@ -0,0 +1,82 @@
1
+ Bundler.require(:benchmarks)
2
+
3
+ require "dry-initializer"
4
+ class WithoutDefaults
5
+ extend Dry::Initializer
6
+
7
+ param :foo
8
+ param :bar
9
+ param :baz
10
+ end
11
+
12
+ class WithOneDefault
13
+ extend Dry::Initializer
14
+
15
+ param :foo
16
+ param :bar
17
+ param :baz, default: proc { "BAZ" }
18
+ end
19
+
20
+ class WithTwoDefaults
21
+ extend Dry::Initializer
22
+
23
+ param :foo
24
+ param :bar, default: proc { "BAR" }
25
+ param :baz, default: proc { "BAZ" }
26
+ end
27
+
28
+ class WithThreeDefaults
29
+ extend Dry::Initializer
30
+
31
+ param :foo, default: proc { "FOO" }
32
+ param :bar, default: proc { "BAR" }
33
+ param :baz, default: proc { "BAZ" }
34
+ end
35
+
36
+ puts "Benchmark for various options"
37
+
38
+ Benchmark.ips do |x|
39
+ x.config time: 15, warmup: 10
40
+
41
+ x.report("without defaults") do
42
+ WithoutDefaults.new "FOO", "BAR", "BAZ"
43
+ end
44
+
45
+ x.report("with 0 of 1 default used") do
46
+ WithOneDefault.new "FOO", "BAR", "BAZ"
47
+ end
48
+
49
+ x.report("with 1 of 1 default used") do
50
+ WithOneDefault.new "FOO", "BAR"
51
+ end
52
+
53
+ x.report("with 0 of 2 defaults used") do
54
+ WithTwoDefaults.new "FOO", "BAR", "BAZ"
55
+ end
56
+
57
+ x.report("with 1 of 2 defaults used") do
58
+ WithTwoDefaults.new "FOO", "BAR"
59
+ end
60
+
61
+ x.report("with 2 of 2 defaults used") do
62
+ WithTwoDefaults.new "FOO"
63
+ end
64
+
65
+ x.report("with 0 of 3 defaults used") do
66
+ WithThreeDefaults.new "FOO", "BAR", "BAZ"
67
+ end
68
+
69
+ x.report("with 1 of 3 defaults used") do
70
+ WithThreeDefaults.new "FOO", "BAR"
71
+ end
72
+
73
+ x.report("with 2 of 3 defaults used") do
74
+ WithThreeDefaults.new "FOO"
75
+ end
76
+
77
+ x.report("with 3 of 3 defaults used") do
78
+ WithThreeDefaults.new
79
+ end
80
+
81
+ x.compare!
82
+ end
@@ -0,0 +1,55 @@
1
+ Bundler.require(:benchmarks)
2
+
3
+ class PlainRubyTest
4
+ attr_reader :foo, :bar
5
+
6
+ def initialize(foo: "FOO", bar: "BAR")
7
+ @foo = foo
8
+ @bar = bar
9
+ end
10
+ end
11
+
12
+ require "dry-initializer"
13
+ class DryTest
14
+ extend Dry::Initializer
15
+
16
+ option :foo, default: proc { "FOO" }
17
+ option :bar, default: proc { "BAR" }
18
+ end
19
+
20
+ require "kwattr"
21
+ class KwattrTest
22
+ kwattr foo: "FOO", bar: "BAR"
23
+ end
24
+
25
+ require "active_attr"
26
+ class ActiveAttrTest
27
+ include ActiveAttr::AttributeDefaults
28
+
29
+ attribute :foo, default: "FOO"
30
+ attribute :bar, default: "BAR"
31
+ end
32
+
33
+ puts "Benchmark for instantiation with default values"
34
+
35
+ Benchmark.ips do |x|
36
+ x.config time: 15, warmup: 10
37
+
38
+ x.report("plain Ruby") do
39
+ PlainRubyTest.new
40
+ end
41
+
42
+ x.report("dry-initializer") do
43
+ DryTest.new
44
+ end
45
+
46
+ x.report("kwattr") do
47
+ KwattrTest.new
48
+ end
49
+
50
+ x.report("active_attr") do
51
+ ActiveAttrTest.new
52
+ end
53
+
54
+ x.compare!
55
+ end
@@ -0,0 +1,62 @@
1
+ Bundler.require(:benchmarks)
2
+
3
+ class PlainRubyTest
4
+ attr_reader :foo, :bar
5
+
6
+ def initialize(foo:, bar:)
7
+ @foo = foo
8
+ @bar = bar
9
+ fail TypeError unless String === @foo
10
+ fail TypeError unless String === @bar
11
+ end
12
+ end
13
+
14
+ require "dry-initializer"
15
+ class DryTest
16
+ extend Dry::Initializer
17
+
18
+ option :foo, type: String
19
+ option :bar, type: String
20
+ end
21
+
22
+ require "virtus"
23
+ class VirtusTest
24
+ include Virtus.model
25
+
26
+ attribute :foo, String
27
+ attribute :bar, String
28
+ end
29
+
30
+ require "fast_attributes"
31
+ class FastAttributesTest
32
+ extend FastAttributes
33
+
34
+ define_attributes initialize: true do
35
+ attribute :foo, String
36
+ attribute :bar, String
37
+ end
38
+ end
39
+
40
+ puts "Benchmark for instantiation with type constraints"
41
+
42
+ Benchmark.ips do |x|
43
+ x.config time: 15, warmup: 10
44
+
45
+ x.report("plain Ruby") do
46
+ PlainRubyTest.new foo: "FOO", bar: "BAR"
47
+ end
48
+
49
+ x.report("dry-initializer") do
50
+ DryTest.new foo: "FOO", bar: "BAR"
51
+ end
52
+
53
+ x.report("virtus") do
54
+ VirtusTest.new foo: "FOO", bar: "BAR"
55
+ end
56
+
57
+ x.report("fast_attributes") do
58
+ FastAttributesTest.new foo: "FOO", bar: "BAR"
59
+ end
60
+
61
+ x.compare!
62
+ end
@@ -0,0 +1,48 @@
1
+ Bundler.require(:benchmarks)
2
+
3
+ class PlainRubyTest
4
+ attr_reader :foo, :bar
5
+
6
+ def initialize(foo: "FOO", bar: "BAR")
7
+ @foo = foo
8
+ @bar = bar
9
+ fail TypeError unless String === @foo
10
+ fail TypeError unless String === @bar
11
+ end
12
+ end
13
+
14
+ require "dry-initializer"
15
+ class DryTest
16
+ extend Dry::Initializer
17
+
18
+ option :foo, type: String, default: proc { "FOO" }
19
+ option :bar, type: String, default: proc { "BAR" }
20
+ end
21
+
22
+ require "virtus"
23
+ class VirtusTest
24
+ include Virtus.model
25
+
26
+ attribute :foo, String, default: "FOO"
27
+ attribute :bar, String, default: "BAR"
28
+ end
29
+
30
+ puts "Benchmark for instantiation with type constraints and default values"
31
+
32
+ Benchmark.ips do |x|
33
+ x.config time: 15, warmup: 10
34
+
35
+ x.report("plain Ruby") do
36
+ PlainRubyTest.new
37
+ end
38
+
39
+ x.report("dry-initializer") do
40
+ DryTest.new
41
+ end
42
+
43
+ x.report("virtus") do
44
+ VirtusTest.new
45
+ end
46
+
47
+ x.compare!
48
+ end