class Aws::Record::ModelAttributes

@api private

Attributes

attributes[R]
storage_attributes[R]

Public Class Methods

new(model_class) click to toggle source
# File lib/aws-record/record/model_attributes.rb, line 21
def initialize(model_class)
  @model_class = model_class
  @attributes = {}
  @storage_attributes = {}
end

Public Instance Methods

attribute_for(name) click to toggle source
# File lib/aws-record/record/model_attributes.rb, line 35
def attribute_for(name)
  @attributes[name]
end
db_to_attribute_name(storage_name) click to toggle source
# File lib/aws-record/record/model_attributes.rb, line 47
def db_to_attribute_name(storage_name)
  @storage_attributes[storage_name]
end
present?(name) click to toggle source
# File lib/aws-record/record/model_attributes.rb, line 43
def present?(name)
  attribute_for(name) ? true : false
end
register_attribute(name, marshaler, opts) click to toggle source
# File lib/aws-record/record/model_attributes.rb, line 27
def register_attribute(name, marshaler, opts)
  attribute = Attribute.new(name, opts.merge(marshaler: marshaler))
  _new_attr_validation(name, attribute)
  @attributes[name] = attribute
  @storage_attributes[attribute.database_name] = name
  attribute
end
storage_name_for(name) click to toggle source
# File lib/aws-record/record/model_attributes.rb, line 39
def storage_name_for(name)
  attribute_for(name).database_name
end

Private Instance Methods

_check_for_naming_collisions(name, storage_name) click to toggle source
# File lib/aws-record/record/model_attributes.rb, line 78
def _check_for_naming_collisions(name, storage_name)
  if @attributes[storage_name.to_sym]
    raise Errors::NameCollision.new(
      "Custom storage name #{storage_name} already exists as an"\
        " attribute name in #{@attributes}"
    )
  elsif @storage_attributes[name.to_s]
    raise Errors::NameCollision.new(
      "Attribute name #{name} already exists as a custom storage"\
        " name in #{@storage_attributes}"
    )
  elsif @storage_attributes[storage_name]
    raise Errors::NameCollision.new(
      "Custom storage name #{storage_name} already in use in"\
        " #{@storage_attributes}"
    )
  end
end
_check_if_reserved(name) click to toggle source
# File lib/aws-record/record/model_attributes.rb, line 69
def _check_if_reserved(name)
  if @model_class.instance_methods.include?(name)
    raise Errors::ReservedName.new(
      "Cannot name an attribute #{name}, that would collide with an"\
        " existing instance method."
    )
  end
end
_new_attr_validation(name, attribute) click to toggle source
# File lib/aws-record/record/model_attributes.rb, line 52
def _new_attr_validation(name, attribute)
  _validate_attr_name(name)
  _check_for_naming_collisions(name, attribute.database_name)
  _check_if_reserved(name)
end
_validate_attr_name(name) click to toggle source
# File lib/aws-record/record/model_attributes.rb, line 58
def _validate_attr_name(name)
  unless name.is_a?(Symbol)
    raise ArgumentError.new("Must use symbolized :name attribute.")
  end
  if @attributes[name]
    raise Errors::NameCollision.new(
      "Cannot overwrite existing attribute #{name}"
    )
  end
end