Module: Dynamoid::Fields

Extended by:
ActiveSupport::Concern
Included in:
Components
Defined in:
lib/dynamoid/fields.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

Constant Summary collapse

PERMITTED_KEY_TYPES =

Types allowed in indexes:

%i[
  number
  integer
  string
  datetime
  serialized
].freeze

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.



114
115
116
# File 'lib/dynamoid/fields.rb', line 114

def attributes
  @attributes
end

Instance Method Details

#attributes_before_type_castObject

Returns a hash of attributes before typecasting



150
151
152
# File 'lib/dynamoid/fields.rb', line 150

def attributes_before_type_cast
  @attributes_before_type_cast
end

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

Read an attribute from an object.

Parameters:

  • name (Symbol)

    the name of the field

Since:

  • 0.2.0



144
145
146
# File 'lib/dynamoid/fields.rb', line 144

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

#read_attribute_before_type_cast(name) ⇒ Object

Returns the value of the attribute identified by name before typecasting

Parameters:

  • attribute (Symbol)

    name



157
158
159
160
161
# File 'lib/dynamoid/fields.rb', line 157

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) ⇒ Object Also known as: []=

Write an attribute on the object. Also marks the previous value as dirty.

Parameters:

  • name (Symbol)

    the name of the field

  • value (Object)

    the value to assign to that field

Since:

  • 0.2.0



123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/dynamoid/fields.rb', line 123

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

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

  attribute_will_change!(name) # Dirty API

  @attributes_before_type_cast[name] = value

  value_casted = TypeCasting.cast_field(value, self.class.attributes[name])
  attributes[name] = value_casted
end