class HashedRecord

Attributes

access_method[R]
collection[R]
collection_ids[R]
index[R]

Public Class Methods

new(collection, access_method: nil) click to toggle source
# File lib/hashedrecord.rb, line 25
def initialize(collection, access_method: nil)
  @collection = collection
  @collection_ids = !collection.empty? ? Array(0...collection.size) : []
  @index = {}
  @access_method = access_method
end

Public Instance Methods

call(chain = []) click to toggle source
# File lib/hashedrecord.rb, line 32
def call(chain = [])
  chain.inject(collection_ids) do |result, (params, method)|
    subsets = params.map do |key, value|
      unless index.key? key
        index[key] = collection_ids.group_by do |collection_id|
          get_value(collection[collection_id], key)
        end
      end
      index[key].slice(*value).values.flatten(1)
    end
    subsets.inject(result) do |result, subset|
      result.send(method, subset)
    end
  end.map { |object_id| collection[object_id] }
end

Private Instance Methods

get_value(record, key) click to toggle source
# File lib/hashedrecord.rb, line 52
def get_value(record, key)
  if @access_method.nil?
    @access_method = if record.respond_to?(key)
                       ->(record, key) { record.send(key) }
                     elsif record.respond_to?(:[]) && record.respond_to?(:key)
                       if record.key? key
                         ->(record, key) { record.send(:[], key) }
                       elsif record.key? key.to_s
                         ->(record, key) { record.send(:[], key.to_s) }
                       end
       end
    raise 'Cannot determinate access method' if @access_method.nil?
  end
  access_method.call(record, key)
end