Class: HashedRecord

Inherits:
Object
  • Object
show all
Includes:
Enumerable, Chainable
Defined in:
lib/hashedrecord.rb

Defined Under Namespace

Modules: Chainable

Instance Method Summary collapse

Methods included from Chainable

#each, #not, #where

Constructor Details

#initialize(collection, access_method: nil) ⇒ HashedRecord

Returns a new instance of HashedRecord.



25
26
27
28
29
30
# 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

Instance Method Details

#call(chain = []) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# 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