Class: MongoHydrator
- Inherits:
-
Object
- Object
- MongoHydrator
- Defined in:
- lib/mongo_hydrator.rb
Instance Method Summary collapse
- #hydrate_document(document, path_or_paths) ⇒ Object
-
#initialize(collection, options = {}) ⇒ MongoHydrator
constructor
Create a new MongoHydrator instance.
Constructor Details
#initialize(collection, options = {}) ⇒ MongoHydrator
Create a new MongoHydrator instance
collection – The Mongo::Collection instance from which to fetch
subdocuments during hydration
options – Optional hash containing options to pass to
collection.find. Typically used to specify a :fields
option to limit the fields included in the subdocuments.
Returns the new MongoHydrator instance.
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/mongo_hydrator.rb', line 14 def initialize(collection, = {}) @hydration_proc = Proc.new do |ids| if [:fields] # We need the_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 = [: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 = collection.find({ '_id' => { '$in' => ids } }, ) subdocuments.inject({}) do |hash, subdocument| hash[subdocument['_id']] = subdocument subdocument.delete('_id') if strip_id hash end end end |
Instance Method Details
#hydrate_document(document, path_or_paths) ⇒ Object
36 37 38 |
# File 'lib/mongo_hydrator.rb', line 36 def hydrate_document(document, path_or_paths) DocumentHydrator.hydrate_document(document, path_or_paths, @hydration_proc) end |