Module: Dynamoid::Fields

Extended by:
ActiveSupport::Concern
Included in:
Components
Defined in:
lib/dynamoid/fields.rb,
lib/dynamoid/fields/declare.rb

Overview

All fields on a Dynamoid::Document must be explicitly defined – if you have fields in the database that are not specified with field, then they will be ignored.

Defined Under Namespace

Modules: ClassMethods Classes: Declare

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#attributesObject Also known as: raw_attributes

You can access the attributes of an object directly on its attributes method, which is by default an empty hash.



273
274
275
# File 'lib/dynamoid/fields.rb', line 273

def attributes
  @attributes
end

Instance Method Details

#attributes_before_type_castHash

Return attributes values before type casting.

user = User.new
user.age = '21'
user.age # => 21

user.attributes_before_type_cast # => { age: '21' }

Returns:

  • (Hash)

    original attribute values



333
334
335
# File 'lib/dynamoid/fields.rb', line 333

def attributes_before_type_cast
  @attributes_before_type_cast
end

#read_attribute(name) ⇒ Object Also known as: []

Read an attribute from an object.

user.age = 20
user.read_attribute(:age) # => 20

Parameters:

  • name (Symbol)

    the name of the field

Returns:

  • attribute value

Since:

  • 0.2.0



319
320
321
# File 'lib/dynamoid/fields.rb', line 319

def read_attribute(name)
  attributes[name.to_sym]
end

#read_attribute_before_type_cast(name) ⇒ Object

Return the value of the attribute identified by name before type casting.

user = User.new
user.age = '21'
user.age # => 21

user.read_attribute_before_type_cast(:age) # => '21'

Parameters:

  • name (Symbol)

    attribute name

Returns:

  • original attribute value



347
348
349
350
351
# File 'lib/dynamoid/fields.rb', line 347

def read_attribute_before_type_cast(name)
  return nil unless name.respond_to?(:to_sym)

  @attributes_before_type_cast[name.to_sym]
end

#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



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

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