Class: Aws::Record::Attribute
- Inherits:
-
Object
- Object
- Aws::Record::Attribute
- Defined in:
- lib/aws-record/record/attribute.rb
Overview
This class provides helper methods for Aws::Record
attributes. These include marshalers for type casting of item attributes, the Amazon DynamoDB type for use in certain table and item operation calls, and the ability to define a database name that is separate from the name used within the model class and item instances.
Instance Attribute Summary collapse
-
#database_name ⇒ Object
readonly
Returns the value of attribute database_name.
-
#dynamodb_type ⇒ Object
readonly
Returns the value of attribute dynamodb_type.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Instance Method Summary collapse
- #default_value ⇒ Object private
- #extract(dynamodb_item) ⇒ Object private
-
#initialize(name, options = {}) ⇒ Attribute
constructor
A new instance of Attribute.
-
#persist_nil? ⇒ Boolean
true
if this attribute will actively persist nil values,false
otherwise. -
#serialize(raw_value) ⇒ Object
Attempts to serialize a raw value into the attribute’s serialized storage type.
-
#type_cast(raw_value) ⇒ Object
Attempts to type cast a raw value into the attribute’s type.
Constructor Details
#initialize(name, options = {}) ⇒ Attribute
Returns a new instance of Attribute.
37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/aws-record/record/attribute.rb', line 37 def initialize(name, = {}) @name = name @database_name = ([:database_attribute_name] || name).to_s @dynamodb_type = [:dynamodb_type] @marshaler = [:marshaler] || DefaultMarshaler @persist_nil = [:persist_nil] @default_value_or_lambda = if .key?(:default_value) dv = [:default_value] _is_lambda?(dv) ? dv : type_cast(dv) end end |
Instance Attribute Details
#database_name ⇒ Object (readonly)
Returns the value of attribute database_name.
11 12 13 |
# File 'lib/aws-record/record/attribute.rb', line 11 def database_name @database_name end |
#dynamodb_type ⇒ Object (readonly)
Returns the value of attribute dynamodb_type.
11 12 13 |
# File 'lib/aws-record/record/attribute.rb', line 11 def dynamodb_type @dynamodb_type end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
11 12 13 |
# File 'lib/aws-record/record/attribute.rb', line 11 def name @name end |
Instance Method Details
#default_value ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
84 85 86 87 88 89 90 |
# File 'lib/aws-record/record/attribute.rb', line 84 def default_value if _is_lambda?(@default_value_or_lambda) type_cast(@default_value_or_lambda.call) else _deep_copy(@default_value_or_lambda) end end |
#extract(dynamodb_item) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
79 80 81 |
# File 'lib/aws-record/record/attribute.rb', line 79 def extract(dynamodb_item) dynamodb_item[@database_name] end |
#persist_nil? ⇒ Boolean
Returns true
if this attribute will actively persist nil values, false
otherwise. Default: false
.
74 75 76 |
# File 'lib/aws-record/record/attribute.rb', line 74 def persist_nil? @persist_nil ? true : false end |
#serialize(raw_value) ⇒ Object
Attempts to serialize a raw value into the attribute’s serialized storage type. This call will forward the raw value to this attribute’s marshaler class.
66 67 68 69 70 |
# File 'lib/aws-record/record/attribute.rb', line 66 def serialize(raw_value) cast_value = type_cast(raw_value) cast_value = default_value if cast_value.nil? @marshaler.serialize(cast_value) end |
#type_cast(raw_value) ⇒ Object
Attempts to type cast a raw value into the attribute’s type. This call will forward the raw value to this attribute’s marshaler class.
54 55 56 57 58 |
# File 'lib/aws-record/record/attribute.rb', line 54 def type_cast(raw_value) cast_value = @marshaler.type_cast(raw_value) cast_value = default_value if cast_value.nil? cast_value end |