Skip to content

Commit b57d637

Browse files
authored
Merge pull request #21 from danielvidal/bugfix-custom-type-changed
Fix bug for Custom Types when the type changes to Hash after call the `attributes` method
2 parents 754d238 + d8e6440 commit b57d637

File tree

3 files changed

+32
-16
lines changed

3 files changed

+32
-16
lines changed

lib/shallow_attributes/instance_methods.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ def attributes
5858
hash = {}
5959
@attributes.map do |key, value|
6060
hash[key] =
61-
value.is_a?(Array) ? value.map!(&TO_H_PROC) : TO_H_PROC.call(value)
61+
value.is_a?(Array) ? value.map(&TO_H_PROC) : TO_H_PROC.call(value)
6262
end
6363
hash
6464
end

test/custom_types_test.rb

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,30 +25,36 @@ class Person
2525

2626
describe ShallowAttributes do
2727
describe 'with custom types' do
28-
it 'allows embedded values' do
29-
person = Person.new(address: {
28+
let(:person) do
29+
Person.new(address: {
3030
street: 'Street 1/2', city: {
3131
name: 'NYC'
3232
}
3333
})
34+
end
3435

35-
person.address.zipcode.must_equal "111111"
36-
person.address.street.must_equal "Street 1/2"
36+
it 'allows embedded values' do
37+
person.address.zipcode.must_equal '111111'
38+
person.address.street.must_equal 'Street 1/2'
3739
person.address.city.size.must_equal 9000
38-
person.address.city.name.must_equal "NYC"
40+
person.address.city.name.must_equal 'NYC'
3941
end
4042

4143
it 'returns normal hash' do
42-
person = Person.new(address: {
43-
street: 'Street 1/2', city: {
44-
name: 'NYC'
45-
}
46-
})
47-
48-
person.address.zipcode.must_equal "111111"
49-
person.address.street.must_equal "Street 1/2"
44+
person.address.zipcode.must_equal '111111'
45+
person.address.street.must_equal 'Street 1/2'
5046
person.address.city.size.must_equal 9000
51-
person.address.city.name.must_equal "NYC"
47+
person.address.city.name.must_equal 'NYC'
48+
end
49+
50+
it 'builds the custom types with the correct class' do
51+
person.address.must_be_instance_of Address
52+
person.address.city.must_be_instance_of City
53+
end
54+
55+
it 'does not change the attribute type after call the method #attributes' do
56+
person.attributes
57+
person.address.must_be_instance_of Address
5258
end
5359

5460
describe 'when one of attribute is array' do
@@ -89,6 +95,16 @@ class Person
8995
person.addresses[1].city.name.must_equal 'Spb'
9096
end
9197

98+
it 'builds the custom types with the correct class' do
99+
person.addresses[0].must_be_instance_of Address
100+
person.addresses[0].city.must_be_instance_of City
101+
end
102+
103+
it 'does not change the attribute type after call the method #attributes' do
104+
person.attributes
105+
person.addresses[0].must_be_instance_of Address
106+
end
107+
92108
describe '#attributes' do
93109
it 'returns attributes hash' do
94110
hash = person.attributes

test/dry_types_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class MainDryUser
1313
include ShallowAttributes
1414

1515
attribute :name, Types::Coercible::String
16-
attribute :age, Types::Coercible::Int
16+
attribute :age, Types::Coercible::Integer
1717
attribute :birthday, DateTime
1818
end
1919

0 commit comments

Comments
 (0)