Module: Ripple::Indexes

Extended by:
ActiveSupport::Concern
Defined in:
lib/ripple/indexes.rb

Overview

Adds secondary-indexes to Document properties.

Defined Under Namespace

Modules: ClassMethods, DocumentMethods

Instance Method Summary collapse

Instance Method Details

#indexes_for_persistence(prefix = '') ⇒ Hash

Returns indexes in a form suitable for persisting to Riak.

Returns:

  • (Hash)

    indexes for this document



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/ripple/indexes.rb', line 43

def indexes_for_persistence(prefix = '')
  Hash.new {|h,k| h[k] = Set.new }.tap do |indexes|
    # Add embedded associations' indexes
    self.class.embedded_associations.each do |association|
      documents = instance_variable_get(association.ivar)
      unless documents.nil?
        Array(documents).each do |doc|
          embedded_indexes = doc.indexes_for_persistence("#{prefix}#{association.name}_")
          indexes.merge!(embedded_indexes) do |_,original,new|
            original.merge new
          end
        end
      end
    end

    # Add this document's indexes
    self.class.indexes.each do |key, index|
      if index.block
        index_value = index.to_index_value instance_exec(&index.block)
      else
        index_value = index.to_index_value send(key)
      end
      index_value = Set[index_value] unless index_value.is_a?(Enumerable) && !index_value.is_a?(String)
      indexes[prefix + index.index_key].merge index_value
    end
  end
end