Skip to content

Commit 243b7b3

Browse files
committed
Raise UnknownAttribute error on non-bang variants following Rails convention
1 parent 86df884 commit 243b7b3

File tree

4 files changed

+33
-23
lines changed

4 files changed

+33
-23
lines changed

lib/dynamoid/persistence.rb

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,9 @@ def update!(hash_key, range_key_value = nil, attrs)
277277
# +update_fields+ uses the +UpdateItem+ operation so it saves changes and
278278
# loads an updated document back with one HTTP request.
279279
#
280+
# Raises a +Dynamoid::Errors::UnknownAttribute+ exception if any of the
281+
# attributes is not on the model
282+
#
280283
# @param hash_key_value [Scalar value] hash key
281284
# @param range_key_value [Scalar value] range key (optional)
282285
# @param attrs [Hash]
@@ -325,6 +328,9 @@ def update_fields(hash_key_value, range_key_value = nil, attrs = {}, conditions
325328
# +upsert+ uses the +UpdateItem+ operation so it saves changes and loads
326329
# an updated document back with one HTTP request.
327330
#
331+
# Raises a +Dynamoid::Errors::UnknownAttribute+ exception if any of the
332+
# attributes is not on the model
333+
#
328334
# @param hash_key_value [Scalar value] hash key
329335
# @param range_key_value [Scalar value] range key (optional)
330336
# @param attrs [Hash]
@@ -492,14 +498,15 @@ def save(options = {})
492498
#
493499
# user.update_attributes(age: 27, last_name: 'Tylor')
494500
#
501+
# Raises a +Dynamoid::Errors::UnknownAttribute+ exception if any of the
502+
# attributes is not on the model
503+
#
495504
# @param attributes [Hash] a hash of attributes to update
496505
# @return [true|false] Whether updating successful or not
497506
# @since 0.2.0
498507
def update_attributes(attributes)
499508
attributes.each { |attribute, value| write_attribute(attribute, value) }
500509
save
501-
rescue Dynamoid::Errors::UnknownAttribute
502-
false
503510
end
504511

505512
# Update multiple attributes at once, saving the object once the updates
@@ -525,15 +532,16 @@ def update_attributes!(attributes)
525532
#
526533
# user.update_attribute(:last_name, 'Tylor')
527534
#
535+
# Raises a +Dynamoid::Errors::UnknownAttribute+ exception if any of the
536+
# attributes is not on the model
537+
#
528538
# @param attribute [Symbol] attribute name to update
529539
# @param value [Object] the value to assign it
530540
# @return [Dynamoid::Document] self
531541
# @since 0.2.0
532542
def update_attribute(attribute, value)
533543
write_attribute(attribute, value)
534544
save
535-
rescue Dynamoid::Errors::UnknownAttribute
536-
false
537545
end
538546

539547
# Update a model.

lib/dynamoid/persistence/update_fields.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def call
2525

2626
raw_attributes = update_item
2727
@model_class.new(undump_attributes(raw_attributes))
28-
rescue Dynamoid::Errors::ConditionalCheckFailedException, Dynamoid::Errors::UnknownAttribute
28+
rescue Dynamoid::Errors::ConditionalCheckFailedException
2929
end
3030

3131
private

lib/dynamoid/persistence/upsert.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def call
2525

2626
raw_attributes = update_item
2727
@model_class.new(undump_attributes(raw_attributes))
28-
rescue Dynamoid::Errors::ConditionalCheckFailedException, Dynamoid::Errors::UnknownAttribute
28+
rescue Dynamoid::Errors::ConditionalCheckFailedException
2929
end
3030

3131
private

spec/dynamoid/persistence_spec.rb

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1058,7 +1058,7 @@ def log_message
10581058
end.to change { d.reload.name }.to('[Updated]')
10591059
end
10601060

1061-
it 'does not save when adding an attribute that is not on the model' do
1061+
it 'raises an UnknownAttribute error when adding an attribute that is not on the model' do
10621062
klass = new_class do
10631063
field :name
10641064
end
@@ -1067,7 +1067,7 @@ def log_message
10671067

10681068
expect do
10691069
klass.update(obj.id, name: 'New name', age: 26)
1070-
end.not_to change { obj.reload.name }
1070+
end.to raise_error Dynamoid::Errors::UnknownAttribute
10711071
end
10721072

10731073
describe 'timestamps' do
@@ -1302,11 +1302,12 @@ def log_message
13021302
end
13031303
end
13041304

1305-
it 'returns nil if an attribute does not exist in the model' do
1305+
it 'raises an UnknownAttribute error when adding an attribute that is not on the model' do
13061306
obj = document_class.create(title: 'New Document')
13071307

1308-
result = document_class.update_fields(obj.id, { title: 'New title', publisher: 'New publisher' } )
1309-
expect(result).to be_nil
1308+
expect {
1309+
document_class.update_fields(obj.id, { title: 'New title', publisher: 'New publisher' } )
1310+
}.to raise_error Dynamoid::Errors::UnknownAttribute
13101311
end
13111312
end
13121313

@@ -1486,11 +1487,12 @@ def log_message
14861487
end
14871488
end
14881489

1489-
it 'returns nil if an attribute does not exist in the model' do
1490+
it 'raises an UnknownAttribute error when adding an attribute that is not on the model' do
14901491
obj = document_class.create(title: 'New Document')
14911492

1492-
result = document_class.upsert(obj.id, { title: 'New title', publisher: 'New publisher' } )
1493-
expect(result).to be_nil
1493+
expect {
1494+
document_class.upsert(obj.id, { title: 'New title', publisher: 'New publisher' } )
1495+
}.to raise_error Dynamoid::Errors::UnknownAttribute
14941496
end
14951497
end
14961498

@@ -2015,16 +2017,17 @@ def log_message
20152017
end
20162018
end
20172019

2018-
it 'does not save the document if the attribute is not on the model' do
2020+
it 'raises an UnknownAttribute error when adding an attribute that is not on the model' do
20192021
klass = new_class do
20202022
field :age, :integer
20212023
field :name, :string
20222024
end
20232025

20242026
obj = klass.create!(name: 'Alex', age: 26)
20252027

2026-
expect(obj.update_attribute(:city, 'Dublin')).to eq false
2027-
expect(klass.find(obj.id)[:city]).to be_nil
2028+
expect {
2029+
obj.update_attribute(:city, 'Dublin')
2030+
}.to raise_error(Dynamoid::Errors::UnknownAttribute)
20282031
end
20292032
end
20302033

@@ -2118,13 +2121,12 @@ def log_message
21182121
end
21192122
end
21202123

2121-
it 'does not save the document if an attribute is not on the model' do
2124+
it 'raises an UnknownAttribute error when adding an attribute that is not on the model' do
21222125
obj = klass.create!(name: 'Alex', age: 26)
21232126

2124-
expect(obj.update_attributes(city: 'Dublin', age: 27)).to eq false
2125-
saved_object = klass.find(obj.id)
2126-
expect(saved_object[:city]).to be_nil
2127-
expect(saved_object[:age]).to eq 26
2127+
expect {
2128+
obj.update_attributes!(city: 'Dublin', age: 27)
2129+
}.to raise_error(Dynamoid::Errors::UnknownAttribute)
21282130
end
21292131
end
21302132

@@ -2168,7 +2170,7 @@ def log_message
21682170
expect(klass.find(obj.id).age).to eql 26
21692171
end
21702172

2171-
it 'raises UnknownAttributeError if any attribute is not on the model' do
2173+
it 'raises an UnknownAttribute error when adding an attribute that is not on the model' do
21722174
obj = klass.create!(name: 'Alex', age: 26)
21732175

21742176
expect {

0 commit comments

Comments
 (0)