Skip to content

Commit

Permalink
Fix coercion for array type: do not coerce array value if type is the…
Browse files Browse the repository at this point in the history
… same as specified
  • Loading branch information
evheny0 committed Nov 7, 2019
1 parent 51f5c1b commit f299bd0
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
3 changes: 2 additions & 1 deletion lib/shallow_attributes/type/array.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ def coerce(values, options = {})
raise ShallowAttributes::Type::InvalidValueError, %(Invalid value "#{values}" for type "Array")
end
values.map! do |value|
ShallowAttributes::Type.coerce(item_klass(options[:of]), value)
klass_const = item_klass(options[:of])
value.is_a?(klass_const) ? value : ShallowAttributes::Type.coerce(klass_const, value)
end
end

Expand Down
11 changes: 10 additions & 1 deletion test/array_type_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,16 @@
end
end

describe 'when value is Array' do
describe 'when value is Array of specified types' do
let(:arbitrary_class) { Class.new }
let(:arbitrary_value) { arbitrary_class.new }

it 'returns array of non-coerced values' do
type.coerce([arbitrary_value], of: arbitrary_class).must_equal [arbitrary_value]
end
end

describe 'when value is Array of non-specified types' do
it 'returns array of specific type' do
type.coerce([], of: Integer).must_equal []
type.coerce(['1', '2'], of: Integer).must_equal [1, 2]
Expand Down

0 comments on commit f299bd0

Please sign in to comment.