Module: Mongo::ObjectHelper

Defined in:
lib/mongo_db/object/object_helper.rb

Instance Method Summary collapse

Instance Method Details

#each(selector = {}, opts = {}, &block) ⇒ Object



53
54
55
56
57
58
59
60
61
# File 'lib/mongo_db/object/object_helper.rb', line 53

def each selector = {}, opts = {}, &block
  opts = opts.clone
  object = (opts.delete(:object) == false) ? false : true
  super selector, opts do |doc|
    doc = ::Mongo::ObjectSerializer.build(doc) if object
    block.call doc
  end
  nil
end

#first(selector = {}, opts = {}, &block) ⇒ Object

Querying



46
47
48
49
50
51
# File 'lib/mongo_db/object/object_helper.rb', line 46

def first selector = {}, opts = {}, &block
  opts = opts.clone
  object = (opts.delete(:object) == false) ? false : true
  doc = super selector, opts, &block
  object ? ::Mongo::ObjectSerializer.build(doc) : doc
end

#insert_with_object(args, opts = {}) ⇒ Object



13
14
15
16
17
18
19
# File 'lib/mongo_db/object/object_helper.rb', line 13

def insert_with_object args, opts = {}
  if args.is_a?(Hash) or args.is_a?(Array)
    insert_without_object args, opts
  else
    ::Mongo::ObjectSerializer.new(args).insert opts, self
  end
end

#remove_with_object(arg = {}, opts = {}) ⇒ Object



30
31
32
33
34
35
36
# File 'lib/mongo_db/object/object_helper.rb', line 30

def remove_with_object arg = {}, opts = {}
  if arg.is_a? Hash
    remove_without_object arg, opts
  else
    ::Mongo::ObjectSerializer.new(arg).remove opts, self
  end
end

#save!(doc, opts = {}) ⇒ Object



38
39
40
# File 'lib/mongo_db/object/object_helper.rb', line 38

def save! doc, opts = {}
  save(doc, opts) || raise(Mongo::Error, "can't save #{doc.inspect}!")
end

#save_with_object(doc, opts = {}) ⇒ Object

CRUD



5
6
7
8
9
10
11
# File 'lib/mongo_db/object/object_helper.rb', line 5

def save_with_object doc, opts = {}
  if doc.is_a? Hash
    save_without_object doc, opts
  else
    ::Mongo::ObjectSerializer.new(doc).save opts, self
  end
end

#update_with_object(selector, doc, opts = {}) ⇒ Object



21
22
23
24
25
26
27
28
# File 'lib/mongo_db/object/object_helper.rb', line 21

def update_with_object selector, doc, opts = {}
  if doc.is_a?(Hash)
    update_without_object selector, doc, opts
  else
    raise "can't use update selector with object (#{selector}, {#{doc}})!" unless selector == nil
    ::Mongo::ObjectSerializer.new(doc).update opts, self
  end
end