module Aws::Record::Attributes::ClassMethods

Public Instance Methods

attr(name, marshaler, opts = {}) click to toggle source

Define an attribute for your model, providing your own attribute type.

@param [Symbol] name Name of this attribute. It should be a name that

is safe to use as a method.

@param [Marshaler] marshaler The marshaler for this attribute. So long

as you provide a marshaler which implements +#type_cast+ and
+#serialize+ that consume raw values as expected, you can bring your
own marshaler type. Convenience methods will provide this for you.

@param [Hash] opts @option opts [String] :database_attribute_name Optional attribute

used to specify a different name for database persistence than the
`name` parameter. Must be unique (you can't have overlap between
database attribute names and the names of other attributes).

@option opts [String] :dynamodb_type Generally used for keys and

index attributes, one of "S", "N", "B", "BOOL", "SS", "NS", "BS",
"M", "L". Optional if this attribute will never be used for a key or
secondary index, but most convenience methods for setting attributes
will provide this.

@option opts [Boolean] :persist_nil Optional attribute used to

indicate whether nil values should be persisted. If true, explicitly
set nil values will be saved to DynamoDB as a "null" type. If false,
nil values will be ignored and not persisted. By default, is false.

@option opts [Object] :default_value Optional attribute used to

define a "default value" to be used if the attribute's value on an
item is nil or not set at persistence time.

@option opts [Boolean] :hash_key Set to true if this attribute is

the hash key for the table.

@option opts [Boolean] :range_key Set to true if this attribute is

the range key for the table.
# File lib/aws-record/record/attributes.rb, line 90
def attr(name, marshaler, opts = {})
  @attributes.register_attribute(name, marshaler, opts)
  _define_attr_methods(name)
  _key_attributes(name, opts)
end
attributes() click to toggle source

@api private

# File lib/aws-record/record/attributes.rb, line 396
def attributes
  @attributes
end
boolean_attr(name, opts = {}) click to toggle source

Define a boolean-type attribute for your model.

@param [Symbol] name Name of this attribute. It should be a name that

is safe to use as a method.

@param [Hash] opts @option opts [Boolean] :hash_key Set to true if this attribute is

the hash key for the table.

@option opts [Boolean] :range_key Set to true if this attribute is

the range key for the table.

@option opts [Boolean] :persist_nil Optional attribute used to

indicate whether nil values should be persisted. If true, explicitly
set nil values will be saved to DynamoDB as a "null" type. If false,
nil values will be ignored and not persisted. By default, is false.

@option opts [Object] :default_value Optional attribute used to

define a "default value" to be used if the attribute's value on an
item is nil or not set at persistence time.
# File lib/aws-record/record/attributes.rb, line 133
def boolean_attr(name, opts = {})
  opts[:dynamodb_type] = "BOOL"
  attr(name, Marshalers::BooleanMarshaler.new(opts), opts)
end
date_attr(name, opts = {}) click to toggle source

Define a date-type attribute for your model.

@param [Symbol] name Name of this attribute. It should be a name that

is safe to use as a method.

@param [Hash] opts @option opts [Boolean] :hash_key Set to true if this attribute is

the hash key for the table.

@option opts [Boolean] :range_key Set to true if this attribute is

the range key for the table.

@option opts [Boolean] :persist_nil Optional attribute used to

indicate whether nil values should be persisted. If true, explicitly
set nil values will be saved to DynamoDB as a "null" type. If false,
nil values will be ignored and not persisted. By default, is false.

@option options [Object] :default_value Optional attribute used to

define a "default value" to be used if the attribute's value on an
item is nil or not set at persistence time.
# File lib/aws-record/record/attributes.rb, line 196
def date_attr(name, opts = {})
  opts[:dynamodb_type] = "S"
  attr(name, Marshalers::DateMarshaler.new(opts), opts)
end
datetime_attr(name, opts = {}) click to toggle source

Define a datetime-type attribute for your model.

@param [Symbol] name Name of this attribute. It should be a name that

is safe to use as a method.

@param [Hash] opts @option opts [Boolean] :hash_key Set to true if this attribute is

the hash key for the table.

@option opts [Boolean] :range_key Set to true if this attribute is

the range key for the table.

@option opts [Boolean] :persist_nil Optional attribute used to

indicate whether nil values should be persisted. If true, explicitly
set nil values will be saved to DynamoDB as a "null" type. If false,
nil values will be ignored and not persisted. By default, is false.

@option opts [Object] :default_value Optional attribute used to

define a "default value" to be used if the attribute's value on an
item is nil or not set at persistence time.
# File lib/aws-record/record/attributes.rb, line 217
def datetime_attr(name, opts = {})
  opts[:dynamodb_type] = "S"
  attr(name, Marshalers::DateTimeMarshaler.new(opts), opts)
end
epoch_time_attr(name, opts = {}) click to toggle source

Define a time-type attribute for your model which persists as

epoch-seconds.

@param [Symbol] name Name of this attribute. It should be a name

that is safe to use as a method.

@param [Hash] opts @option opts [Boolean] :hash_key Set to true if this attribute is

the hash key for the table.

@option opts [Boolean] :range_key Set to true if this attribute is

the range key for the table.

@option opts [Boolean] :persist_nil Optional attribute used to

indicate whether nil values should be persisted. If true, explicitly
set nil values will be saved to DynamoDB as a "null" type. If false,
nil values will be ignored and not persisted. By default, is false.

@option opts [Object] :default_value Optional attribute used to

define a "default value" to be used if the attribute's value on an
item is nil or not set at persistence time.
# File lib/aws-record/record/attributes.rb, line 260
def epoch_time_attr(name, opts = {})
  opts[:dynamodb_type] = "N"
  attr(name, Marshalers::EpochTimeMarshaler.new(opts), opts)
end
float_attr(name, opts = {}) click to toggle source

Define a float-type attribute for your model.

@param [Symbol] name Name of this attribute. It should be a name that

is safe to use as a method.

@param [Hash] opts @option opts [Boolean] :hash_key Set to true if this attribute is

the hash key for the table.

@option opts [Boolean] :range_key Set to true if this attribute is

the range key for the table.

@option opts [Boolean] :persist_nil Optional attribute used to

indicate whether nil values should be persisted. If true, explicitly
set nil values will be saved to DynamoDB as a "null" type. If false,
nil values will be ignored and not persisted. By default, is false.

@option opts [Object] :default_value Optional attribute used to

define a "default value" to be used if the attribute's value on an
item is nil or not set at persistence time.
# File lib/aws-record/record/attributes.rb, line 175
def float_attr(name, opts = {})
  opts[:dynamodb_type] = "N"
  attr(name, Marshalers::FloatMarshaler.new(opts), opts)
end
hash_key() click to toggle source

@return [Symbol,nil] The symbolic name of the table's hash key.

# File lib/aws-record/record/attributes.rb, line 386
def hash_key
  @keys.hash_key
end
integer_attr(name, opts = {}) click to toggle source

Define a integer-type attribute for your model.

@param [Symbol] name Name of this attribute. It should be a name that

is safe to use as a method.

@param [Hash] opts @option opts [Boolean] :hash_key Set to true if this attribute is

the hash key for the table.

@option opts [Boolean] :range_key Set to true if this attribute is

the range key for the table.

@option opts [Boolean] :persist_nil Optional attribute used to

indicate whether nil values should be persisted. If true, explicitly
set nil values will be saved to DynamoDB as a "null" type. If false,
nil values will be ignored and not persisted. By default, is false.

@option opts [Object] :default_value Optional attribute used to

define a "default value" to be used if the attribute's value on an
item is nil or not set at persistence time.
# File lib/aws-record/record/attributes.rb, line 154
def integer_attr(name, opts = {})
  opts[:dynamodb_type] = "N"
  attr(name, Marshalers::IntegerMarshaler.new(opts), opts)
end
keys() click to toggle source

@api private

# File lib/aws-record/record/attributes.rb, line 401
def keys
  @keys.keys
end
list_attr(name, opts = {}) click to toggle source

Define a list-type attribute for your model.

Lists do not have to be homogeneous, but they do have to be types that the AWS SDK for Ruby V2's DynamoDB client knows how to marshal and unmarshal. Those types are:

  • Hash

  • Array

  • String

  • Numeric

  • Boolean

  • IO

  • Set

  • nil

Also note that, since lists are heterogeneous, you may lose some precision when marshaling and unmarshaling. For example, symbols will be stringified, but there is no way to return those strings to symbols when the object is read back from DynamoDB.

@param [Symbol] name Name of this attribute. It should be a name that

is safe to use as a method.

@param [Hash] opts @option opts [Boolean] :hash_key Set to true if this attribute is

the hash key for the table.

@option opts [Boolean] :range_key Set to true if this attribute is

the range key for the table.

@option opts [Object] :default_value Optional attribute used to

define a "default value" to be used if the attribute's value on an
item is nil or not set at persistence time.
# File lib/aws-record/record/attributes.rb, line 295
def list_attr(name, opts = {})
  opts[:dynamodb_type] = "L"
  attr(name, Marshalers::ListMarshaler.new(opts), opts)
end
map_attr(name, opts = {}) click to toggle source

Define a map-type attribute for your model.

Maps do not have to be homogeneous, but they do have to use types that the AWS SDK for Ruby V2's DynamoDB client knows how to marshal and unmarshal. Those types are:

  • Hash

  • Array

  • String

  • Numeric

  • Boolean

  • IO

  • Set

  • nil

Also note that, since maps are heterogeneous, you may lose some precision when marshaling and unmarshaling. For example, symbols will be stringified, but there is no way to return those strings to symbols when the object is read back from DynamoDB.

@param [Symbol] name Name of this attribute. It should be a name that

is safe to use as a method.

@param [Hash] opts @option opts [Boolean] :hash_key Set to true if this attribute is

the hash key for the table.

@option opts [Boolean] :range_key Set to true if this attribute is

the range key for the table.

@option opts [Object] :default_value Optional attribute used to

define a "default value" to be used if the attribute's value on an
item is nil or not set at persistence time.
# File lib/aws-record/record/attributes.rb, line 330
def map_attr(name, opts = {})
  opts[:dynamodb_type] = "M"
  attr(name, Marshalers::MapMarshaler.new(opts), opts)
end
numeric_set_attr(name, opts = {}) click to toggle source

Define a numeric set attribute for your model.

Numeric sets are homogeneous sets, containing only numbers. Note that empty sets cannot be persisted to DynamoDB. Empty sets are valid for aws-record items, but they will not be persisted as sets. nil values from your table, or a lack of value from your table, will be treated as an empty set for item instances. At persistence time, the marshaler will attempt to marshal any non-numerics within the set to be Numeric objects.

@param [Symbol] name Name of this attribute. It should be a name that

is safe to use as a method.

@param [Hash] opts @option opts [Boolean] :hash_key Set to true if this attribute is

the hash key for the table.

@option opts [Boolean] :range_key Set to true if this attribute is

the range key for the table.

@option opts [Object] :default_value Optional attribute used to

define a "default value" to be used if the attribute's value on an
item is nil or not set at persistence time.
# File lib/aws-record/record/attributes.rb, line 380
def numeric_set_attr(name, opts = {})
  opts[:dynamodb_type] = "NS"
  attr(name, Marshalers::NumericSetMarshaler.new(opts), opts)
end
range_key() click to toggle source

@return [Symbol,nil] The symbloc name of the table's range key, or nil if there is no range key.

# File lib/aws-record/record/attributes.rb, line 391
def range_key
  @keys.range_key
end
string_attr(name, opts = {}) click to toggle source

Define a string-type attribute for your model.

@param [Symbol] name Name of this attribute. It should be a name that

is safe to use as a method.

@param [Hash] opts @option opts [Boolean] :hash_key Set to true if this attribute is

the hash key for the table.

@option opts [Boolean] :range_key Set to true if this attribute is

the range key for the table.

@option opts [Boolean] :persist_nil Optional attribute used to

indicate whether nil values should be persisted. If true, explicitly
set nil values will be saved to DynamoDB as a "null" type. If false,
nil values will be ignored and not persisted. By default, is false.

@option opts [Object] :default_value Optional attribute used to

define a "default value" to be used if the attribute's value on an
item is nil or not set at persistence time.
# File lib/aws-record/record/attributes.rb, line 112
def string_attr(name, opts = {})
  opts[:dynamodb_type] = "S"
  attr(name, Marshalers::StringMarshaler.new(opts), opts)
end
string_set_attr(name, opts = {}) click to toggle source

Define a string set attribute for your model.

String sets are homogeneous sets, containing only strings. Note that empty sets cannot be persisted to DynamoDB. Empty sets are valid for aws-record items, but they will not be persisted as sets. nil values from your table, or a lack of value from your table, will be treated as an empty set for item instances. At persistence time, the marshaler will attempt to marshal any non-strings within the set to be String objects.

@param [Symbol] name Name of this attribute. It should be a name that

is safe to use as a method.

@param [Hash] opts @option opts [Boolean] :hash_key Set to true if this attribute is

the hash key for the table.

@option opts [Boolean] :range_key Set to true if this attribute is

the range key for the table.

@option opts [Object] :default_value Optional attribute used to

define a "default value" to be used if the attribute's value on an
item is nil or not set at persistence time.
# File lib/aws-record/record/attributes.rb, line 355
def string_set_attr(name, opts = {})
  opts[:dynamodb_type] = "SS"
  attr(name, Marshalers::StringSetMarshaler.new(opts), opts)
end
time_attr(name, opts = {}) click to toggle source

Define a time-type attribute for your model.

@param [Symbol] name Name of this attribute. It should be a name that

is safe to use as a method.

@param [Hash] opts @option opts [Boolean] :hash_key Set to true if this attribute is

the hash key for the table.

@option opts [Boolean] :range_key Set to true if this attribute is

the range key for the table.

@option opts [Boolean] :persist_nil Optional attribute used to

indicate whether nil values should be persisted. If true, explicitly
set nil values will be saved to DynamoDB as a "null" type. If false,
nil values will be ignored and not persisted. By default, is false.

@option opts [Object] :default_value Optional attribute used to

define a "default value" to be used if the attribute's value on an
item is nil or not set at persistence time.
# File lib/aws-record/record/attributes.rb, line 238
def time_attr(name, opts = {})
  opts[:dynamodb_type] = "S"
  attr(name, Marshalers::TimeMarshaler.new(opts), opts)
end

Private Instance Methods

_define_attr_methods(name) click to toggle source
# File lib/aws-record/record/attributes.rb, line 406
def _define_attr_methods(name)
  define_method(name) do
    @data.get_attribute(name)
  end

  define_method("#{name}=") do |value|
    @data.set_attribute(name, value)
  end
end
_key_attributes(id, opts) click to toggle source
# File lib/aws-record/record/attributes.rb, line 416
def _key_attributes(id, opts)
  if opts[:hash_key] == true && opts[:range_key] == true
    raise ArgumentError.new(
      "Cannot have the same attribute be a hash and range key."
    )
  elsif opts[:hash_key] == true
    @keys.hash_key = id
  elsif opts[:range_key] == true
    @keys.range_key = id
  end
end