Module: Zdi::DocumentWrapper::ClassMethods

Included in:
Zdi::DocumentWrapper
Defined in:
lib/zentradi/document_wrapper.rb

Instance Method Summary collapse

Instance Method Details

#mongo_collection_method_wrap(mongo_collection, method, zdi_collection_class, *args, &block) ⇒ Object

Sends a method to a mongo collection. On send, all DocumentWrapper args are converted to hash. On return, all hashes in the result are converted to DocumentWrappers.



8
9
10
11
12
# File 'lib/zentradi/document_wrapper.rb', line 8

def mongo_collection_method_wrap(mongo_collection, method, zdi_collection_class, *args, &block)
  new_args = Zdi::DocumentWrapper.unwrap(args)
  result = mongo_collection.send(method, *new_args, &block)
  Zdi::DocumentWrapper.wrap(zdi_collection_class, result)
end

#unwrap(object) ⇒ Object

Convert DocumentWrapper instance/s to normal hashes.



15
16
17
18
19
20
21
22
# File 'lib/zentradi/document_wrapper.rb', line 15

def unwrap(object)
  case object
  when DocumentWrapper then object.to_hash
  when Array           then object.map { |e| DocumentWrapper.unwrap(e) }
  else
    object
  end
end

#wrap(zdi_collection_class, object) ⇒ Object

Convert hashes instance/s to normal DocumentWrapper instances.



25
26
27
28
29
30
31
32
33
# File 'lib/zentradi/document_wrapper.rb', line 25

def wrap(zdi_collection_class, object)
  case object
  when Hash          then DocumentWrapper.new(zdi_collection_class, object)
  when Array         then object.map { |e| DocumentWrapper.wrap(zdi_collection_class, e) }
  when Mongo::Cursor then object.extend(MongoCursorMethods).zentradi_collection = zdi_collection_class; object
  else
    object
  end
end