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 190
def initialize(attrs={})
  unless attrs[:dynamoid_class].present?
    raise Dynamoid::Errors::InvalidIndex.new(':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.new(self) unless self.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 213
def projection_type
  if @projected_attributes.is_a? Array
    :include
  else
    @projected_attributes
  end
end

Private Instance Methods

validate_hash_key() click to toggle source
# File lib/dynamoid/indexes.rb, line 256
def validate_hash_key
  hash_field_attributes = @dynamoid_class.attributes[@hash_key]
  if hash_field_attributes.present?
    hash_field_type = hash_field_attributes[:type]
    if Dynamoid::Fields::PERMITTED_KEY_TYPES.include?(hash_field_type)
      @hash_key_schema = {
        @hash_key => @dynamoid_class.dynamo_type(hash_field_type)
      }
    else
      errors.add(:hash_key, 'Index :hash_key is not a valid key type')
    end
  else
    errors.add(:hash_key, "No such field #{@hash_key} defined on table")
  end
end
validate_index_type() click to toggle source
# File lib/dynamoid/indexes.rb, line 231
def validate_index_type
  unless (@type.present? &&
    [: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 224
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 238
def validate_range_key
  if @range_key.present?
    range_field_attributes = @dynamoid_class.attributes[@range_key]
    if range_field_attributes.present?
      range_key_type = range_field_attributes[:type]
      if Dynamoid::Fields::PERMITTED_KEY_TYPES.include?(range_key_type)
        @range_key_schema = {
          @range_key => @dynamoid_class.dynamo_type(range_key_type)
        }
      else
        errors.add(:range_key, 'Index :range_key is not a valid key type')
      end
    else
      errors.add(:range_key, "No such field #{@range_key} defined on table")
    end
  end
end