module Dynamodb::AttributeAssignment::ClassMethods

Attributes

attribute_definitions[R]
global_indexes[R]
hash_key[R]
indexes[R]
key_schema[R]
local_indexes[R]
range_key[R]

Public Instance Methods

global_index(options) click to toggle source
# File lib/dynamodb/attribute_assignment.rb, line 89
def global_index(options)
  @indexes ||= []
  @indexes << define_global_index(options)
  options[:keys].keys.each do |_key|
    define_attribute(
      {
        name: options[:keys][_key][:name],
        type: options[:keys][_key][:type]
      }
    )
  end
end
key(attr_name, attr_type, options) click to toggle source
# File lib/dynamodb/attribute_assignment.rb, line 72
def key(attr_name, attr_type, options)
  instance_variable_set("@#{options[:key]}_key", attr_name.to_s)
  define_attribute({ name: attr_name, type: attr_type })
  define_key_schema(attr_name, options[:key])
end
local_index(options) click to toggle source
# File lib/dynamodb/attribute_assignment.rb, line 78
def local_index(options)
  @indexes ||= []
  @indexes << define_local_index(options)
  define_attribute(
    {
      name: options[:key][:name],
      type: options[:key][:type]
    }
  )
end
table_name(_table_name = nil) click to toggle source
# File lib/dynamodb/attribute_assignment.rb, line 58
def table_name(_table_name = nil)
  return (@_table_name || name.tableize) if _table_name.nil?

  @_table_name = _table_name.to_s
end
time_to_live(&block) click to toggle source
# File lib/dynamodb/attribute_assignment.rb, line 102
def time_to_live(&block)
  define_method :schedule_time_to_live, &block
end

Private Instance Methods

define_attribute(name:, type:) click to toggle source
# File lib/dynamodb/attribute_assignment.rb, line 108
def define_attribute(name:, type:)
  @attribute_definitions ||= []
  attr_def = { attribute_name: name.to_s, attribute_type: ATTRIBUTE_TYPES[type] }
  @attribute_definitions << attr_def unless @attribute_definitions.include?(attr_def)
end
define_global_index(options) click to toggle source
# File lib/dynamodb/attribute_assignment.rb, line 134
def define_global_index(options)
  {
    type: :global,
    index_name: options[:name].to_s,
    key_schema: options[:keys].keys.each_with_object([]) do |_key, obj|
      obj << { attribute_name: options[:keys][_key][:name].to_s, key_type: KEY_TYPES[_key] }
    end,
    projection: define_projection(options)
  }
end
define_key_schema(attr_name, key_type) click to toggle source
# File lib/dynamodb/attribute_assignment.rb, line 114
def define_key_schema(attr_name, key_type)
  @key_schema ||= []
  @key_schema << { attribute_name: attr_name.to_s, key_type: KEY_TYPES[key_type] }
end
define_local_index(options) click to toggle source
# File lib/dynamodb/attribute_assignment.rb, line 119
def define_local_index(options)
  hash_key_schema =
    @key_schema.detect { |k| k[:attribute_name] == @hash_key }

  {
    type: :local,
    index_name: options[:name].to_s,
    key_schema: [
      hash_key_schema,
      { attribute_name: options[:key][:name].to_s, key_type: KEY_TYPES[:range] }
    ],
    projection: define_projection(options)
  }
end
define_projection(options) click to toggle source
# File lib/dynamodb/attribute_assignment.rb, line 145
def define_projection(options)
  p_hash = {
    projection_type: PROJECTION_TYPES[options[:projection]]
  }

  p_hash.merge!({
    non_key_attributes: options[:attributes]
  }) if options[:projection] == :include

  p_hash
end
indexes_for(type) click to toggle source
# File lib/dynamodb/attribute_assignment.rb, line 157
def indexes_for(type)
  return if @indexes.nil?

  @indexes.select { |h| h[:type] == type }
          .map { |h| h.select { |k| k != :type }}
end