dry-initializer 2.5.0 → 3.0.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/CHANGELOG.md +367 -239
- data/LICENSE +20 -0
- data/README.md +17 -79
- data/dry-initializer.gemspec +29 -16
- data/lib/dry-initializer.rb +1 -1
- data/lib/dry/initializer.rb +16 -14
- 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 +22 -8
- data/lib/dry/initializer/definition.rb +20 -71
- data/lib/dry/initializer/dispatchers.rb +101 -33
- data/lib/dry/initializer/dispatchers/build_nested_type.rb +59 -0
- data/lib/dry/initializer/dispatchers/check_type.rb +43 -0
- data/lib/dry/initializer/dispatchers/prepare_default.rb +40 -0
- data/lib/dry/initializer/dispatchers/prepare_ivar.rb +12 -0
- data/lib/dry/initializer/dispatchers/prepare_optional.rb +13 -0
- data/lib/dry/initializer/dispatchers/prepare_reader.rb +30 -0
- data/lib/dry/initializer/dispatchers/prepare_source.rb +28 -0
- data/lib/dry/initializer/dispatchers/prepare_target.rb +44 -0
- data/lib/dry/initializer/dispatchers/unwrap_type.rb +22 -0
- data/lib/dry/initializer/dispatchers/wrap_type.rb +28 -0
- data/lib/dry/initializer/mixin.rb +4 -4
- data/lib/dry/initializer/mixin/root.rb +1 -0
- data/lib/dry/initializer/struct.rb +39 -0
- data/lib/dry/initializer/undefined.rb +2 -0
- data/lib/dry/initializer/version.rb +5 -0
- data/lib/tasks/benchmark.rake +13 -13
- data/lib/tasks/profile.rake +16 -16
- metadata +38 -103
- data/.codeclimate.yml +0 -23
- data/.gitignore +0 -10
- data/.rspec +0 -4
- data/.rubocop.yml +0 -51
- data/.travis.yml +0 -24
- data/Gemfile +0 -29
- data/Guardfile +0 -5
- data/LICENSE.txt +0 -21
- data/Rakefile +0 -8
- data/benchmarks/compare_several_defaults.rb +0 -82
- data/benchmarks/plain_options.rb +0 -63
- data/benchmarks/plain_params.rb +0 -84
- data/benchmarks/with_coercion.rb +0 -71
- data/benchmarks/with_defaults.rb +0 -66
- data/benchmarks/with_defaults_and_coercion.rb +0 -59
- data/spec/attributes_spec.rb +0 -38
- data/spec/coercion_of_nil_spec.rb +0 -25
- data/spec/custom_dispatchers_spec.rb +0 -35
- data/spec/custom_initializer_spec.rb +0 -30
- data/spec/default_values_spec.rb +0 -83
- data/spec/definition_spec.rb +0 -107
- data/spec/invalid_default_spec.rb +0 -13
- data/spec/missed_default_spec.rb +0 -14
- data/spec/optional_spec.rb +0 -71
- data/spec/options_tolerance_spec.rb +0 -11
- data/spec/public_attributes_utility_spec.rb +0 -22
- data/spec/reader_spec.rb +0 -87
- data/spec/repetitive_definitions_spec.rb +0 -69
- data/spec/several_assignments_spec.rb +0 -41
- data/spec/spec_helper.rb +0 -21
- data/spec/subclassing_spec.rb +0 -49
- data/spec/type_argument_spec.rb +0 -35
- data/spec/type_constraint_spec.rb +0 -78
- data/spec/value_coercion_via_dry_types_spec.rb +0 -29
@@ -1,78 +0,0 @@
|
|
1
|
-
require "dry-types"
|
2
|
-
|
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
|
-
|
34
|
-
context "by dry-type" do
|
35
|
-
before do
|
36
|
-
class Test::Foo
|
37
|
-
extend Dry::Initializer
|
38
|
-
param :foo, Dry::Types["strict.string"], optional: true
|
39
|
-
end
|
40
|
-
end
|
41
|
-
|
42
|
-
context "in case of mismatch" do
|
43
|
-
subject { Test::Foo.new 1 }
|
44
|
-
|
45
|
-
it "raises TypeError" do
|
46
|
-
expect { subject }.to raise_error TypeError, /1/
|
47
|
-
end
|
48
|
-
end
|
49
|
-
|
50
|
-
context "in case of match" do
|
51
|
-
subject { Test::Foo.new "foo" }
|
52
|
-
|
53
|
-
it "completes the initialization" do
|
54
|
-
expect { subject }.not_to raise_error
|
55
|
-
end
|
56
|
-
end
|
57
|
-
|
58
|
-
context "if optional value not set" do
|
59
|
-
subject { Test::Foo.new }
|
60
|
-
|
61
|
-
it "not applicable to Dry::Initializer::UNDEFINED" do
|
62
|
-
expect(subject.instance_variable_get(:@foo))
|
63
|
-
.to eq Dry::Initializer::UNDEFINED
|
64
|
-
end
|
65
|
-
end
|
66
|
-
end
|
67
|
-
|
68
|
-
context "by invalid constraint" do
|
69
|
-
it "raises TypeError" do
|
70
|
-
expect do
|
71
|
-
class Test::Foo
|
72
|
-
extend Dry::Initializer
|
73
|
-
param :foo, type: String
|
74
|
-
end
|
75
|
-
end.to raise_error(TypeError)
|
76
|
-
end
|
77
|
-
end
|
78
|
-
end
|
@@ -1,29 +0,0 @@
|
|
1
|
-
require "dry-types"
|
2
|
-
|
3
|
-
describe "value coercion via dry-types" do
|
4
|
-
before do
|
5
|
-
module Test::Types
|
6
|
-
include Dry::Types.module
|
7
|
-
end
|
8
|
-
|
9
|
-
class Test::Foo
|
10
|
-
extend Dry::Initializer
|
11
|
-
|
12
|
-
param :foo, type: Test::Types::Coercible::String
|
13
|
-
option :bar, proc(&:to_i), default: proc { "16" }
|
14
|
-
end
|
15
|
-
end
|
16
|
-
|
17
|
-
it "coerces assigned values" do
|
18
|
-
subject = Test::Foo.new :foo, bar: "13"
|
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
|
28
|
-
end
|
29
|
-
end
|