class Dynamoid::Indexes::Index

Represents the attributes of a DynamoDB index.

Constants

DEFAULT_PROJECTION_TYPE
PROJECTION_TYPES

Attributes

dynamoid_class[RW]
hash_key[RW]
hash_key_schema[RW]
name[RW]
projected_attributes[RW]
range_key[RW]
range_key_schema[RW]
read_capacity[RW]
type[RW]
write_capacity[RW]

Public Class Methods

new(attrs = {}) click to toggle source
# File lib/dynamoid/indexes.rb, line 255
def initialize(attrs = {})
  unless attrs[:dynamoid_class].present?
    raise Dynamoid::Errors::InvalidIndex, ':dynamoid_class is required'
  end

  @dynamoid_class = attrs[:dynamoid_class]
  @type = attrs[:type]
  @hash_key = attrs[:hash_key]
  @range_key = attrs[:range_key]
  @name = attrs[:name] || @dynamoid_class.index_name(@hash_key, @range_key)
  @projected_attributes =
    attrs[:projected_attributes] || DEFAULT_PROJECTION_TYPE
  @read_capacity = attrs[:read_capacity]
  @write_capacity = attrs[:write_capacity]

  raise Dynamoid::Errors::InvalidIndex, self unless valid?
end

Public Instance Methods

projection_type() click to toggle source

Convenience method to determine the projection type for an index. Projection types are: :keys_only, :all, :include.

@return [Symbol] the projection type.

# File lib/dynamoid/indexes.rb, line 277
def projection_type
  if @projected_attributes.is_a? Array
    :include
  else
    @projected_attributes
  end
end

Private Instance Methods

dynamodb_type(field_type, options) click to toggle source
# File lib/dynamoid/indexes.rb, line 326
def dynamodb_type(field_type, options)
  PrimaryKeyTypeMapping.dynamodb_type(field_type, options)
rescue Errors::UnsupportedKeyType
  field_type
end
validate_hash_key() click to toggle source
# File lib/dynamoid/indexes.rb, line 301
def validate_hash_key
  validate_index_key(:hash_key, @hash_key)
end
validate_index_key(key_param, key_val) click to toggle source
# File lib/dynamoid/indexes.rb, line 309
def validate_index_key(key_param, key_val)
  return if key_val.blank?

  key_field_attributes = @dynamoid_class.attributes[key_val]
  if key_field_attributes.blank?
    errors.add(key_param, "No such field #{key_val} defined on table")
    return
  end

  key_dynamodb_type = dynamodb_type(key_field_attributes[:type], key_field_attributes)
  if PERMITTED_KEY_DYNAMODB_TYPES.include?(key_dynamodb_type)
    send("#{key_param}_schema=", { key_val => key_dynamodb_type })
  else
    errors.add(key_param, "Index :#{key_param} is not a valid key type")
  end
end
validate_index_type() click to toggle source
# File lib/dynamoid/indexes.rb, line 294
def validate_index_type
  unless @type.present? &&
         %i[local_secondary global_secondary].include?(@type)
    errors.add(:type, 'Invalid index :type specified')
  end
end
validate_projected_attributes() click to toggle source
# File lib/dynamoid/indexes.rb, line 287
def validate_projected_attributes
  unless @projected_attributes.is_a?(Array) ||
         PROJECTION_TYPES.include?(@projected_attributes)
    errors.add(:projected_attributes, 'Invalid projected attributes specified.')
  end
end
validate_range_key() click to toggle source
# File lib/dynamoid/indexes.rb, line 305
def validate_range_key
  validate_index_key(:range_key, @range_key)
end