Skip to content

Commit 42263a0

Browse files
authored
Merge pull request #27 from morr/nil-strings
allow_nil for strings and inverse logic of allow_nil
2 parents 37fe194 + 2885c3a commit 42263a0

File tree

7 files changed

+15
-8
lines changed

7 files changed

+15
-8
lines changed

lib/shallow_attributes/type/float.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class Float
2525
# @since 0.1.0
2626
def coerce(value, options = {})
2727
case value
28-
when nil then options[:allow_nil] ? 0.0 : nil
28+
when nil then options[:allow_nil] ? nil : 0.0
2929
when ::TrueClass then 1.0
3030
when ::FalseClass then 0.0
3131
else

lib/shallow_attributes/type/integer.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class Integer
2525
# @since 0.1.0
2626
def coerce(value, options = {})
2727
case value
28-
when nil then options[:allow_nil] ? 0 : nil
28+
when nil then options[:allow_nil] ? nil : 0
2929
when ::TrueClass then 1
3030
when ::FalseClass then 0
3131
else

lib/shallow_attributes/type/string.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@ class String
2020
# @return [Sting]
2121
#
2222
# @since 0.1.0
23-
def coerce(value, _options = {})
23+
def coerce(value, options = {})
2424
case value
25+
when nil then options[:allow_nil] ? nil : ''
2526
when ::Array then value.join
2627
when ::Hash, ::Class then error(value)
2728
else

test/float_type_test.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@
2121

2222
describe 'when value is Nil' do
2323
it 'returns nil' do
24-
assert_nil type.coerce(nil)
24+
type.coerce(nil).must_equal 0
2525
end
2626
end
2727

2828
describe 'when allow_nil is true' do
2929
it 'returns float' do
30-
type.coerce(nil, allow_nil: true).must_equal 0.0
30+
assert_nil type.coerce(nil, allow_nil: true)
3131
end
3232
end
3333

test/integer_type_test.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@
2020

2121
describe 'when value is Nil' do
2222
it 'returns nil' do
23-
assert_nil type.coerce(nil)
23+
type.coerce(nil).must_equal 0
2424
end
2525
end
2626

2727
describe 'when allow_nil is true' do
2828
it 'returns integer' do
29-
type.coerce(nil, allow_nil: true).must_equal 0
29+
assert_nil type.coerce(nil, allow_nil: true)
3030
end
3131
end
3232

test/shallow_attributes_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ def default_color
140140
user.name = nil
141141
user.age = nil
142142
user.admin = nil
143-
user.attributes.must_equal(name: '', age: nil, last_name: "Affleck", full_name: "Anton Affleck", color: "Pink", friends_count: 0, sizes: [], admin: false)
143+
user.attributes.must_equal(name: '', age: 0, last_name: "Affleck", full_name: "Anton Affleck", color: "Pink", friends_count: 0, sizes: [], admin: false)
144144
end
145145
end
146146
end

test/string_type_test.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@
3030
end
3131
end
3232

33+
describe 'when allow_nil is true' do
34+
it 'returns integer' do
35+
assert_nil type.coerce(nil, allow_nil: true)
36+
end
37+
end
38+
3339
describe 'when value is TrueClass' do
3440
it 'returns string' do
3541
type.coerce(true).must_equal 'true'

0 commit comments

Comments
 (0)