Skip to content

Commit 04929eb

Browse files
committed
Keep in #attributes Hash only explicitly specified values in constructor
1 parent 966206f commit 04929eb

File tree

3 files changed

+12
-56
lines changed

3 files changed

+12
-56
lines changed

lib/dynamoid/document.rb

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -138,13 +138,14 @@ def initialize(attrs = {})
138138
@associations ||= {}
139139
@attributes_before_type_cast ||= {}
140140

141-
attrs_with_defaults = {}
142-
self.class.attributes.each do |attribute, options|
143-
attrs_with_defaults[attribute] = if attrs.key?(attribute)
144-
attrs[attribute]
145-
elsif options.key?(:default)
146-
evaluate_default_value(options[:default])
147-
end
141+
attrs_with_defaults = self.class.attributes.reduce({}) do |res, (attribute, options)|
142+
if attrs.key?(attribute)
143+
res.merge(attribute => attrs[attribute])
144+
elsif options.key?(:default)
145+
res.merge(attribute => evaluate_default_value(options[:default]))
146+
else
147+
res
148+
end
148149
end
149150

150151
attrs_virtual = attrs.slice(*(attrs.keys - self.class.attributes.keys))

spec/dynamoid/before_type_cast_spec.rb

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,7 @@
1414
obj = klass.new(admin: 0)
1515

1616
expect(obj.attributes_before_type_cast).to eql(
17-
id: nil,
1817
admin: 0,
19-
created_at: nil,
20-
updated_at: nil
2118
)
2219
end
2320

@@ -30,12 +27,7 @@
3027
obj = klass_with_many_fields.new(first_name: 'John')
3128

3229
expect(obj.attributes_before_type_cast).to eql(
33-
id: nil,
3430
first_name: 'John',
35-
last_name: nil,
36-
email: nil,
37-
created_at: nil,
38-
updated_at: nil
3931
)
4032
end
4133

@@ -46,22 +38,14 @@
4638
obj = klass_with_default_value.new
4739

4840
expect(obj.attributes_before_type_cast).to eql(
49-
id: nil,
5041
activated_on: '2018-09-27',
51-
created_at: nil,
52-
updated_at: nil
5342
)
5443
end
5544

5645
it 'returns nil if field does not have default value' do
5746
obj = klass.new
5847

59-
expect(obj.attributes_before_type_cast).to eql(
60-
id: nil,
61-
admin: nil,
62-
created_at: nil,
63-
updated_at: nil
64-
)
48+
expect(obj.attributes_before_type_cast).to eql({})
6549
end
6650

6751
it 'returns values loaded from the storage before type casting' do
@@ -71,8 +55,6 @@
7155
expect(obj2.attributes_before_type_cast).to eql(
7256
id: obj.id,
7357
admin: false,
74-
created_at: nil,
75-
updated_at: nil
7658
)
7759
end
7860
end

spec/dynamoid/document_spec.rb

Lines changed: 3 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,7 @@
77
address = Address.new
88

99
expect(address.new_record).to be_truthy
10-
expect(address.attributes).to eq(id: nil,
11-
created_at: nil,
12-
updated_at: nil,
13-
city: nil,
14-
options: nil,
15-
deliverable: nil,
16-
latitude: nil,
17-
config: nil,
18-
registered_on: nil,
19-
lock_version: nil)
10+
expect(address.attributes).to eq({})
2011
end
2112

2213
it 'responds to will_change! methods for all fields' do
@@ -32,33 +23,15 @@
3223

3324
expect(address.new_record).to be_truthy
3425

35-
expect(address.attributes).to eq(id: nil,
36-
created_at: nil,
37-
updated_at: nil,
38-
city: 'Chicago',
39-
options: nil,
40-
deliverable: nil,
41-
latitude: nil,
42-
config: nil,
43-
registered_on: nil,
44-
lock_version: nil)
26+
expect(address.attributes).to eq(city: 'Chicago')
4527
end
4628

4729
it 'initializes a new document with a virtual attribute' do
4830
address = Address.new(zip_code: '12345')
4931

5032
expect(address.new_record).to be_truthy
5133

52-
expect(address.attributes).to eq(id: nil,
53-
created_at: nil,
54-
updated_at: nil,
55-
city: 'Chicago',
56-
options: nil,
57-
deliverable: nil,
58-
latitude: nil,
59-
config: nil,
60-
registered_on: nil,
61-
lock_version: nil)
34+
expect(address.attributes).to eq(city: 'Chicago')
6235
end
6336

6437
it 'allows interception of write_attribute on load' do

0 commit comments

Comments
 (0)