Module: DocumentHydrator::HydrationProc::Mongo

Defined in:
lib/document_hydrator/hydration_proc/mongo.rb

Class Method Summary collapse

Class Method Details

.collection(coll, options = {}) ⇒ Object

Create a hydration proc that fetches subdocuments by ID from the provided collection.

coll - The Mongo::Collection containing the subdocuments options - (Optional) hash of options to pass to MongoDB::Collection#find.

Defaults to {}.

Returns a Proc that maps IDs to their corresponding subdocuments within the collection.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/document_hydrator/hydration_proc/mongo.rb', line 14

def collection(coll, options = {})
  Proc.new do |ids|
    if options[:fields]
      # We need to _id key in order to assemble the results hash.
      # If the caller has requested that it be omitted from the
      # result, re-enable it and then strip later.
      field_selectors = options[:fields]
      id_key = field_selectors.keys.detect { |k| k.to_s == '_id' }
      if id_key && field_selectors[id_key] == 0
        field_selectors.delete(id_key)
        strip_id = true
      end
    end
    subdocuments = coll.find({ '_id' => { '$in' => ids } }, options)
    subdocuments.inject({}) do |hash, subdocument|
      hash[subdocument['_id']] = subdocument
      subdocument.delete('_id') if strip_id
      hash
    end
  end
end