diff --git a/lib/shallow_attributes/type/array.rb b/lib/shallow_attributes/type/array.rb index 28d4083..274d924 100644 --- a/lib/shallow_attributes/type/array.rb +++ b/lib/shallow_attributes/type/array.rb @@ -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 diff --git a/test/array_type_test.rb b/test/array_type_test.rb index 6c94590..8eb8501 100644 --- a/test/array_type_test.rb +++ b/test/array_type_test.rb @@ -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]