File tree Expand file tree Collapse file tree 2 files changed +31
-7
lines changed
lib/dry/initializer/builders Expand file tree Collapse file tree 2 files changed +31
-7
lines changed Original file line number Diff line number Diff line change @@ -20,7 +20,8 @@ def initialize(definition)
20
20
@default = definition . default
21
21
@source = definition . source
22
22
@ivar = definition . ivar
23
- @null = definition . null ? "Dry::Initializer::UNDEFINED" : "nil"
23
+ @default_null = "Dry::Initializer::UNDEFINED"
24
+ @null = definition . null ? @default_null : "nil"
24
25
@opts = "__dry_initializer_options__"
25
26
@congif = "__dry_initializer_config__"
26
27
@item = "__dry_initializer_definition__"
@@ -67,9 +68,11 @@ def coercion_line
67
68
return unless @type
68
69
arity = @type . is_a? ( Proc ) ? @type . arity : @type . method ( :call ) . arity
69
70
if arity . abs == 1
70
- "#{ @val } = #{ @item } .type.call(#{ @val } ) unless #{ @null } == #{ @val } "
71
+ "#{ @val } = #{ @item } .type.call(#{ @val } )" \
72
+ " unless #{ @default_null } == #{ @val } "
71
73
else
72
- "#{ @val } = #{ @item } .type.call(#{ @val } , self) unless #{ @null } == #{ @val } "
74
+ "#{ @val } = #{ @item } .type.call(#{ @val } , self)" \
75
+ " unless #{ @default_null } == #{ @val } "
73
76
end
74
77
end
75
78
Original file line number Diff line number Diff line change @@ -15,11 +15,32 @@ class Test::Baz
15
15
let ( :foo ) { Test ::Foo . new ( nil ) }
16
16
let ( :baz ) { Test ::Baz . new ( nil ) }
17
17
18
- it "works with extend syntax" do
19
- expect ( foo . bar ) . to eq 0
18
+ context "default @null" do
19
+ it "works with extend syntax" do
20
+ expect ( foo . bar ) . to eq 0
21
+ end
22
+
23
+ it "works with include syntax" do
24
+ expect ( baz . qux ) . to eq 0
25
+ end
20
26
end
21
27
22
- it "works with include syntax" do
23
- expect ( baz . qux ) . to eq 0
28
+ context "@null is nil" do
29
+ before ( :context ) do
30
+ @default_null = Dry ::Initializer . instance_variable_get :@null
31
+ Dry ::Initializer . instance_variable_set :@null , nil
32
+ end
33
+
34
+ after ( :context ) do
35
+ Dry ::Initializer . instance_variable_set :@null , @default_null
36
+ end
37
+
38
+ it "works with extend syntax" do
39
+ expect ( foo . bar ) . to eq 0
40
+ end
41
+
42
+ it "works with include syntax" do
43
+ expect ( baz . qux ) . to eq 0
44
+ end
24
45
end
25
46
end
You can’t perform that action at this time.
0 commit comments