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
Constant Summary collapse
- PERMITTED_KEY_TYPES =
Types allowed in indexes:
%i[ number integer string date datetime serialized ].freeze
Instance Attribute Summary collapse
-
#attributes ⇒ Object
(also: #raw_attributes)
You can access the attributes of an object directly on its attributes method, which is by default an empty hash.
Instance Method Summary collapse
-
#attributes_before_type_cast ⇒ Hash
Return attributes values before type casting.
-
#read_attribute(name) ⇒ Object
(also: #[])
Read an attribute from an object.
-
#read_attribute_before_type_cast(name) ⇒ Object
Return the value of the attribute identified by name before type casting.
-
#write_attribute(name, value) ⇒ Object
(also: #[]=)
Write an attribute on the object.
Instance Attribute Details
#attributes ⇒ Object 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.
280 281 282 |
# File 'lib/dynamoid/fields.rb', line 280 def attributes @attributes end |
Instance Method Details
#attributes_before_type_cast ⇒ Hash
Return attributes values before type casting.
user = User.new
user.age = '21'
user.age # => 21
user.attributes_before_type_cast # => { age: '21' }
337 338 339 |
# File 'lib/dynamoid/fields.rb', line 337 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
323 324 325 |
# File 'lib/dynamoid/fields.rb', line 323 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'
351 352 353 354 355 |
# File 'lib/dynamoid/fields.rb', line 351 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.
user.age = 20
user.write_attribute(:age, 21)
user.age # => 21
Also marks the previous value as dirty.
295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 |
# File 'lib/dynamoid/fields.rb', line 295 def write_attribute(name, value) name = name.to_sym unless attribute_is_present_on_model?(name) raise Dynamoid::Errors::UnknownAttribute.new("Attribute #{name} is not part of the model") end 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 |