Sha256: de21b6606c9297e305e2a8ff78f2c1fdaf5591e05dc90b88a74e11c624448930

Contents?: true

Size: 632 Bytes

Versions: 4

Compression:

Stored size: 632 Bytes

Contents

module GreyscaleRecord
  module DataStore
    class Index
      def initialize(field, data)
        @indexed_data = {}
        data.each do |id, datum|
          key = datum[field]

          # storing key => array of matching ids
          @indexed_data[key] = Array(@indexed_data[key]) + [id]
        end
      end

      # returns ids 
      def find(values)
        # find all the arrays of ids for the values, 
        #   get rid of nils (value not present), 
        #   and compact for a single array result
        values.map do |value|
          @indexed_data[value]
        end.compact.flatten
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
greyscale_record-1.0.3 lib/greyscale_record/data_store/index.rb
greyscale_record-1.0.2 lib/greyscale_record/data_store/index.rb
greyscale_record-1.0.1 lib/greyscale_record/data_store/index.rb
greyscale_record-1.0.0 lib/greyscale_record/data_store/index.rb