Sha256: 0d2e2f6728e66a0d94b2dd500de1f31cf49b12b345c8580b9a9621c6a2af45a6

Contents?: true

Size: 1.99 KB

Versions: 13

Compression:

Stored size: 1.99 KB

Contents

# encoding: utf-8
require 'dynamoid/indexes/index'

module Dynamoid #:nodoc:

  # Indexes are quick ways of performing queries by anything other than id in DynamoDB. They are denormalized tables; 
  # that is, data is duplicated in the initial table (where the object is saved) and the index table (where 
  # we perform indexing). 
  module Indexes
    extend ActiveSupport::Concern

    # Make some helpful attributes to persist indexes.
    included do
      class_attribute :indexes
      
      self.indexes = {}
    end
    
    module ClassMethods
      
      # The call to create an index. Generates a new index with the specified options -- for more information, see Dynamoid::Indexes::Index.
      # This function also attempts to immediately create the indexing table if it does not exist already.
      #
      # @since 0.2.0
      def index(name, options = {})
        index = Dynamoid::Indexes::Index.new(self, name, options)
        self.indexes[index.name] = index
        create_indexes        
      end
      
      # Helper function to find indexes.
      #
      # @since 0.2.0
      def find_index(index)
        self.indexes[Array(index).collect(&:to_s).sort.collect(&:to_sym)]
      end
      
      # Helper function to create indexes (if they don't exist already).
      #
      # @since 0.2.0
      def create_indexes
        self.indexes.each do |name, index|
          opts = {:table_name => index.table_name, :id => :id}
          opts[:range_key] = { :range => :number } if index.range_key?
          self.create_table(opts)
        end
      end
    end
    
    # Callback for an object to save itself to each of a class' indexes.
    #
    # @since 0.2.0
    def save_indexes
      self.class.indexes.each do |name, index|
        index.save(self)
      end
    end

    # Callback for an object to delete itself from each of a class' indexes.
    #
    # @since 0.2.0    
    def delete_indexes
      self.class.indexes.each do |name, index|
        index.delete(self)
      end
    end
  end
  
end

Version data entries

13 entries across 13 versions & 3 rubygems

Version Path
dynamoid-moda-0.7.2 lib/dynamoid/indexes.rb
dynamoid-moda-0.7.1 lib/dynamoid/indexes.rb
dynamoid-0.7.1 lib/dynamoid/indexes.rb
dynamoid-0.7.0 lib/dynamoid/indexes.rb
dynamoid-0.6.1 lib/dynamoid/indexes.rb
dynamoid-0.6.0 lib/dynamoid/indexes.rb
adept_dynamoid-0.6.0 lib/dynamoid/indexes.rb
adept_dynamoid-0.5.0.8 lib/dynamoid/indexes.rb
adept_dynamoid-0.5.0.7 lib/dynamoid/indexes.rb
adept_dynamoid-0.5.0.6 lib/dynamoid/indexes.rb
dynamoid-0.5.0 lib/dynamoid/indexes.rb
dynamoid-0.4.1 lib/dynamoid/indexes.rb
dynamoid-0.4.0 lib/dynamoid/indexes.rb