dry-initializer 0.0.1

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.
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
@@ -0,0 +1,39 @@
1
+ describe "default values" do
2
+ before do
3
+ class Test::Foo
4
+ extend Dry::Initializer
5
+
6
+ param :foo, default: proc { :FOO }
7
+ param :bar, default: proc { :BAR }
8
+ option :baz, default: proc { :BAZ }
9
+ option :qux, default: proc { foo }
10
+ end
11
+ end
12
+
13
+ it "instantiate arguments" do
14
+ subject = Test::Foo.new(1, 2, baz: 3, qux: 4)
15
+
16
+ expect(subject.foo).to eql 1
17
+ expect(subject.bar).to eql 2
18
+ expect(subject.baz).to eql 3
19
+ expect(subject.qux).to eql 4
20
+ end
21
+
22
+ it "applies default values" do
23
+ subject = Test::Foo.new
24
+
25
+ expect(subject.foo).to eql :FOO
26
+ expect(subject.bar).to eql :BAR
27
+ expect(subject.baz).to eql :BAZ
28
+ expect(subject.qux).to eql :FOO
29
+ end
30
+
31
+ it "applies default values partially" do
32
+ subject = Test::Foo.new 1, baz: 3
33
+
34
+ expect(subject.foo).to eql 1
35
+ expect(subject.bar).to eql :BAR
36
+ expect(subject.baz).to eql 3
37
+ expect(subject.qux).to eql 1
38
+ end
39
+ end
@@ -0,0 +1,25 @@
1
+ describe "Dry type" do
2
+ before do
3
+ class Test::Foo
4
+ extend Dry::Initializer
5
+
6
+ param :foo, type: Dry::Types::Coercible::String
7
+ end
8
+ end
9
+
10
+ pending context "in case of mismatch" do
11
+ subject { Test::Foo.new :foo }
12
+
13
+ it "raises TypeError" do
14
+ expect { subject }.to raise_error TypeError, /:foo/
15
+ end
16
+ end
17
+
18
+ pending context "in case of match" do
19
+ subject { Test::Foo.new "foo" }
20
+
21
+ it "completes the initialization" do
22
+ expect { subject }.not_to raise_error
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,13 @@
1
+ describe "invalid default value assignment" do
2
+ subject do
3
+ class Test::Foo
4
+ extend Dry::Initializer
5
+
6
+ param :foo, default: 1
7
+ end
8
+ end
9
+
10
+ it "raises TypeError" do
11
+ expect { subject }.to raise_error SyntaxError, /1/
12
+ end
13
+ end
@@ -0,0 +1,13 @@
1
+ describe "invalid type declaration" do
2
+ subject do
3
+ class Test::Foo
4
+ extend Dry::Initializer
5
+
6
+ param :foo, type: 1
7
+ end
8
+ end
9
+
10
+ it "raises TypeError" do
11
+ expect { subject }.to raise_error TypeError, /1/
12
+ end
13
+ end
@@ -0,0 +1,14 @@
1
+ describe "missed default values" do
2
+ subject do
3
+ class Test::Foo
4
+ extend Dry::Initializer
5
+
6
+ param :foo, default: proc { :FOO }
7
+ param :bar
8
+ end
9
+ end
10
+
11
+ it "raises SyntaxError" do
12
+ expect { subject }.to raise_error SyntaxError, /bar/
13
+ end
14
+ end
@@ -0,0 +1,33 @@
1
+ describe "PORO type" do
2
+ before do
3
+ class Test::Foo
4
+ extend Dry::Initializer
5
+
6
+ param :foo, type: String
7
+ end
8
+ end
9
+
10
+ context "in case of mismatch" do
11
+ subject { Test::Foo.new :foo }
12
+
13
+ it "raises TypeError" do
14
+ expect { subject }.to raise_error TypeError, /:foo/
15
+ end
16
+ end
17
+
18
+ context "in case of match" do
19
+ subject { Test::Foo.new "foo" }
20
+
21
+ it "completes the initialization" do
22
+ expect { subject }.not_to raise_error
23
+ end
24
+ end
25
+
26
+ context "in case of soft match" do
27
+ subject { Test::Foo.new Class.new(String).new "foo" }
28
+
29
+ it "completes the initialization" do
30
+ expect { subject }.not_to raise_error
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,25 @@
1
+ describe "proc type" do
2
+ before do
3
+ class Test::Foo
4
+ extend Dry::Initializer
5
+
6
+ param :foo, type: proc { |val| fail(TypeError) unless String === val }
7
+ end
8
+ end
9
+
10
+ context "in case of mismatch" do
11
+ subject { Test::Foo.new :foo }
12
+
13
+ it "raises TypeError" do
14
+ expect { subject }.to raise_error TypeError
15
+ end
16
+ end
17
+
18
+ context "in case of match" do
19
+ subject { Test::Foo.new "foo" }
20
+
21
+ it "completes the initialization" do
22
+ expect { subject }.not_to raise_error
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,22 @@
1
+ describe "reader" do
2
+ subject do
3
+ class Test::Foo
4
+ extend Dry::Initializer
5
+
6
+ param :foo, reader: false
7
+ option :bar, reader: false
8
+ end
9
+
10
+ Test::Foo.new 1, bar: 2
11
+ end
12
+
13
+ it "skips attr_reader" do
14
+ expect(subject).not_to respond_to :foo
15
+ expect(subject).not_to respond_to :bar
16
+ end
17
+
18
+ it "keeps assigning variables" do
19
+ expect(subject.instance_variable_get(:@foo)).to eql 1
20
+ expect(subject.instance_variable_get(:@bar)).to eql 2
21
+ end
22
+ end
@@ -0,0 +1,49 @@
1
+ describe "repetitive definitions" do
2
+ context "of params" do
3
+ subject do
4
+ class Test::Foo
5
+ extend Dry::Initializer
6
+
7
+ param :foo
8
+ param :bar
9
+ param :foo
10
+ end
11
+ end
12
+
13
+ it "raise SyntaxError" do
14
+ expect { subject }.to raise_error SyntaxError, /foo/
15
+ end
16
+ end
17
+
18
+ context "of options" do
19
+ subject do
20
+ class Test::Foo
21
+ extend Dry::Initializer
22
+
23
+ option :foo
24
+ option :bar
25
+ option :foo
26
+ end
27
+ end
28
+
29
+ it "raise SyntaxError" do
30
+ expect { subject }.to raise_error SyntaxError, /foo/
31
+ end
32
+ end
33
+
34
+ context "of param and option" do
35
+ subject do
36
+ class Test::Foo
37
+ extend Dry::Initializer
38
+
39
+ param :foo
40
+ option :bar
41
+ option :foo
42
+ end
43
+ end
44
+
45
+ it "raise SyntaxError" do
46
+ expect { subject }.to raise_error SyntaxError, /foo/
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,24 @@
1
+ describe "subclassing" do
2
+ subject do
3
+ class Test::Foo
4
+ extend Dry::Initializer
5
+
6
+ param :foo
7
+ option :bar
8
+ end
9
+
10
+ class Test::Bar < Test::Foo
11
+ param :baz
12
+ option :qux
13
+ end
14
+
15
+ Test::Bar.new 1, 2, bar: 3, qux: 4
16
+ end
17
+
18
+ it "preserves definitions made in the superclass" do
19
+ expect(subject.foo).to eql 1
20
+ expect(subject.baz).to eql 2
21
+ expect(subject.bar).to eql 3
22
+ expect(subject.qux).to eql 4
23
+ end
24
+ end
@@ -0,0 +1,15 @@
1
+ require "pry"
2
+ require "dry/initializer"
3
+
4
+ RSpec.configure do |config|
5
+ config.order = :random
6
+ config.filter_run focus: true
7
+ config.run_all_when_everything_filtered = true
8
+
9
+ # Prepare the Test namespace for constants defined in specs
10
+ config.around(:each) do |example|
11
+ Test = Class.new(Module)
12
+ example.run
13
+ Object.send :remove_const, :Test
14
+ end
15
+ end
metadata ADDED
@@ -0,0 +1,149 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dry-initializer
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Vladimir Kochnev (marshall-lee)
8
+ - Andrew Kozin (nepalez)
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2016-04-13 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: guard-rspec
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - "~>"
19
+ - !ruby/object:Gem::Version
20
+ version: '4.0'
21
+ type: :development
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - "~>"
26
+ - !ruby/object:Gem::Version
27
+ version: '4.0'
28
+ - !ruby/object:Gem::Dependency
29
+ name: rake
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - "~>"
33
+ - !ruby/object:Gem::Version
34
+ version: '10.5'
35
+ type: :development
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - "~>"
40
+ - !ruby/object:Gem::Version
41
+ version: '10.5'
42
+ - !ruby/object:Gem::Dependency
43
+ name: dry-types
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - "~>"
47
+ - !ruby/object:Gem::Version
48
+ version: 0.5.1
49
+ type: :development
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - "~>"
54
+ - !ruby/object:Gem::Version
55
+ version: 0.5.1
56
+ description:
57
+ email:
58
+ - hashtable@yandex.ru
59
+ - andrew.kozin@gmail.com
60
+ executables: []
61
+ extensions: []
62
+ extra_rdoc_files:
63
+ - README.md
64
+ - CHANGELOG.md
65
+ files:
66
+ - ".codeclimate.yml"
67
+ - ".gitignore"
68
+ - ".rspec"
69
+ - ".rubocop.yml"
70
+ - ".travis.yml"
71
+ - CHANGELOG.md
72
+ - Gemfile
73
+ - Guardfile
74
+ - LICENSE.txt
75
+ - README.md
76
+ - Rakefile
77
+ - benchmarks/options.rb
78
+ - benchmarks/params.rb
79
+ - benchmarks/params_vs_options.rb
80
+ - benchmarks/several_defaults.rb
81
+ - benchmarks/with_defaults.rb
82
+ - benchmarks/with_types.rb
83
+ - benchmarks/with_types_and_defaults.rb
84
+ - benchmarks/without_options.rb
85
+ - dry-initializer.gemspec
86
+ - lib/dry-initializer.rb
87
+ - lib/dry/initializer.rb
88
+ - lib/dry/initializer/argument.rb
89
+ - lib/dry/initializer/arguments.rb
90
+ - lib/dry/initializer/builder.rb
91
+ - lib/dry/initializer/errors.rb
92
+ - lib/dry/initializer/errors/existing_argument_error.rb
93
+ - lib/dry/initializer/errors/invalid_default_value_error.rb
94
+ - lib/dry/initializer/errors/invalid_type_error.rb
95
+ - lib/dry/initializer/errors/key_error.rb
96
+ - lib/dry/initializer/errors/missed_default_value_error.rb
97
+ - lib/dry/initializer/errors/type_error.rb
98
+ - spec/dry/base_spec.rb
99
+ - spec/dry/default_nil_spec.rb
100
+ - spec/dry/default_values_spec.rb
101
+ - spec/dry/dry_type_spec.rb
102
+ - spec/dry/invalid_default_spec.rb
103
+ - spec/dry/invalid_type_spec.rb
104
+ - spec/dry/missed_default_spec.rb
105
+ - spec/dry/poro_type_spec.rb
106
+ - spec/dry/proc_type_spec.rb
107
+ - spec/dry/reader_spec.rb
108
+ - spec/dry/repetitive_definitions_spec.rb
109
+ - spec/dry/subclassing_spec.rb
110
+ - spec/spec_helper.rb
111
+ homepage: https://github.com/dryrb/dry-initializer
112
+ licenses:
113
+ - MIT
114
+ metadata: {}
115
+ post_install_message:
116
+ rdoc_options: []
117
+ require_paths:
118
+ - lib
119
+ required_ruby_version: !ruby/object:Gem::Requirement
120
+ requirements:
121
+ - - ">="
122
+ - !ruby/object:Gem::Version
123
+ version: '2.2'
124
+ required_rubygems_version: !ruby/object:Gem::Requirement
125
+ requirements:
126
+ - - ">="
127
+ - !ruby/object:Gem::Version
128
+ version: '0'
129
+ requirements: []
130
+ rubyforge_project:
131
+ rubygems_version: 2.5.1
132
+ signing_key:
133
+ specification_version: 4
134
+ summary: DSL for declaring params and options of the initializer
135
+ test_files:
136
+ - spec/dry/base_spec.rb
137
+ - spec/dry/default_nil_spec.rb
138
+ - spec/dry/default_values_spec.rb
139
+ - spec/dry/dry_type_spec.rb
140
+ - spec/dry/invalid_default_spec.rb
141
+ - spec/dry/invalid_type_spec.rb
142
+ - spec/dry/missed_default_spec.rb
143
+ - spec/dry/poro_type_spec.rb
144
+ - spec/dry/proc_type_spec.rb
145
+ - spec/dry/reader_spec.rb
146
+ - spec/dry/repetitive_definitions_spec.rb
147
+ - spec/dry/subclassing_spec.rb
148
+ - spec/spec_helper.rb
149
+ has_rdoc: