dry-initializer 2.5.0 → 3.0.4

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 (67) hide show
  1. checksums.yaml +5 -5
  2. data/CHANGELOG.md +367 -239
  3. data/LICENSE +20 -0
  4. data/README.md +17 -79
  5. data/dry-initializer.gemspec +29 -16
  6. data/lib/dry-initializer.rb +1 -1
  7. data/lib/dry/initializer.rb +16 -14
  8. data/lib/dry/initializer/builders.rb +2 -2
  9. data/lib/dry/initializer/builders/attribute.rb +12 -7
  10. data/lib/dry/initializer/builders/initializer.rb +9 -13
  11. data/lib/dry/initializer/builders/reader.rb +3 -1
  12. data/lib/dry/initializer/builders/signature.rb +3 -3
  13. data/lib/dry/initializer/config.rb +22 -8
  14. data/lib/dry/initializer/definition.rb +20 -71
  15. data/lib/dry/initializer/dispatchers.rb +101 -33
  16. data/lib/dry/initializer/dispatchers/build_nested_type.rb +59 -0
  17. data/lib/dry/initializer/dispatchers/check_type.rb +43 -0
  18. data/lib/dry/initializer/dispatchers/prepare_default.rb +40 -0
  19. data/lib/dry/initializer/dispatchers/prepare_ivar.rb +12 -0
  20. data/lib/dry/initializer/dispatchers/prepare_optional.rb +13 -0
  21. data/lib/dry/initializer/dispatchers/prepare_reader.rb +30 -0
  22. data/lib/dry/initializer/dispatchers/prepare_source.rb +28 -0
  23. data/lib/dry/initializer/dispatchers/prepare_target.rb +44 -0
  24. data/lib/dry/initializer/dispatchers/unwrap_type.rb +22 -0
  25. data/lib/dry/initializer/dispatchers/wrap_type.rb +28 -0
  26. data/lib/dry/initializer/mixin.rb +4 -4
  27. data/lib/dry/initializer/mixin/root.rb +1 -0
  28. data/lib/dry/initializer/struct.rb +39 -0
  29. data/lib/dry/initializer/undefined.rb +2 -0
  30. data/lib/dry/initializer/version.rb +5 -0
  31. data/lib/tasks/benchmark.rake +13 -13
  32. data/lib/tasks/profile.rake +16 -16
  33. metadata +38 -103
  34. data/.codeclimate.yml +0 -23
  35. data/.gitignore +0 -10
  36. data/.rspec +0 -4
  37. data/.rubocop.yml +0 -51
  38. data/.travis.yml +0 -24
  39. data/Gemfile +0 -29
  40. data/Guardfile +0 -5
  41. data/LICENSE.txt +0 -21
  42. data/Rakefile +0 -8
  43. data/benchmarks/compare_several_defaults.rb +0 -82
  44. data/benchmarks/plain_options.rb +0 -63
  45. data/benchmarks/plain_params.rb +0 -84
  46. data/benchmarks/with_coercion.rb +0 -71
  47. data/benchmarks/with_defaults.rb +0 -66
  48. data/benchmarks/with_defaults_and_coercion.rb +0 -59
  49. data/spec/attributes_spec.rb +0 -38
  50. data/spec/coercion_of_nil_spec.rb +0 -25
  51. data/spec/custom_dispatchers_spec.rb +0 -35
  52. data/spec/custom_initializer_spec.rb +0 -30
  53. data/spec/default_values_spec.rb +0 -83
  54. data/spec/definition_spec.rb +0 -107
  55. data/spec/invalid_default_spec.rb +0 -13
  56. data/spec/missed_default_spec.rb +0 -14
  57. data/spec/optional_spec.rb +0 -71
  58. data/spec/options_tolerance_spec.rb +0 -11
  59. data/spec/public_attributes_utility_spec.rb +0 -22
  60. data/spec/reader_spec.rb +0 -87
  61. data/spec/repetitive_definitions_spec.rb +0 -69
  62. data/spec/several_assignments_spec.rb +0 -41
  63. data/spec/spec_helper.rb +0 -21
  64. data/spec/subclassing_spec.rb +0 -49
  65. data/spec/type_argument_spec.rb +0 -35
  66. data/spec/type_constraint_spec.rb +0 -78
  67. 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