Method: Dynamoid::Fields#write_attribute

Defined in:
lib/dynamoid/fields.rb

#write_attribute(name, value) ⇒ Dynamoid::Document Also known as: []=

Write an attribute on the object.

user.age = 20
user.write_attribute(:age, 21)
user.age # => 21

Also marks the previous value as dirty.

Parameters:

  • name (Symbol)

    the name of the field

  • value (Object)

    the value to assign to that field

Returns:

Since:

  • 0.2.0



291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
# File 'lib/dynamoid/fields.rb', line 291

def write_attribute(name, value)
  name = name.to_sym
  old_value = read_attribute(name)

  unless attribute_is_present_on_model?(name)
    raise Dynamoid::Errors::UnknownAttribute, "Attribute #{name} is not part of the model"
  end

  if association = @associations[name]
    association.reset
  end

  @attributes_before_type_cast[name] = value

  value_casted = TypeCasting.cast_field(value, self.class.attributes[name])
  attribute_will_change!(name) if old_value != value_casted # Dirty API

  attributes[name] = value_casted
  self
end