Module: Mongo::Object::CollectionHelper

Defined in:
lib/mongo/object/collection_helper.rb

Instance Method Summary collapse

Instance Method Details

#create!(*args) ⇒ Object



40
41
42
# File 'lib/mongo/object/collection_helper.rb', line 40

def create! *args
  create(*args) || raise(Mongo::Error, "can't create #{args.inspect}!")
end

#create_with_object(doc, options = {}) ⇒ Object

CRUD.



4
5
6
7
8
9
10
# File 'lib/mongo/object/collection_helper.rb', line 4

def create_with_object doc, options = {}
  if doc.is_a? ::Mongo::Object
    doc.create_object self, options
  else
    create_without_object doc, options
  end
end

#delete!(*args) ⇒ Object



52
53
54
# File 'lib/mongo/object/collection_helper.rb', line 52

def delete! *args
  delete(*args) || raise(Mongo::Error, "can't delete #{args.inspect}!")
end

#delete_with_object(*args) ⇒ Object



30
31
32
33
34
35
36
37
38
# File 'lib/mongo/object/collection_helper.rb', line 30

def delete_with_object *args
  if args.first.is_a? ::Mongo::Object
    doc, options = args
    options ||= {}
    doc.delete_object self, options
  else
    delete_without_object *args
  end
end

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



68
69
70
71
72
73
74
75
76
77
# File 'lib/mongo/object/collection_helper.rb', line 68

def each selector = {}, options = {}, &block
  options = options.clone
  if options.delete(:object) == false
    super selector, options, &block
  else
    super selector, options do |doc|
      block.call build(doc)
    end
  end
end

#first(selector = {}, options = {}) ⇒ Object

Querying.



58
59
60
61
62
63
64
65
66
# File 'lib/mongo/object/collection_helper.rb', line 58

def first selector = {}, options = {}
  options = options.clone
  if options.delete(:object) == false
    super selector, options
  else
    doc = super(selector, options)
    build doc
  end
end

#save!(*args) ⇒ Object



48
49
50
# File 'lib/mongo/object/collection_helper.rb', line 48

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

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



22
23
24
25
26
27
28
# File 'lib/mongo/object/collection_helper.rb', line 22

def save_with_object doc, options = {}
  if doc.is_a? ::Mongo::Object
    doc.save_object self, options
  else
    save_without_object doc, options
  end
end

#update!(*args) ⇒ Object



44
45
46
# File 'lib/mongo/object/collection_helper.rb', line 44

def update! *args
  update(*args) || raise(Mongo::Error, "can't update #{args.inspect}!")
end

#update_with_object(*args) ⇒ Object



12
13
14
15
16
17
18
19
20
# File 'lib/mongo/object/collection_helper.rb', line 12

def update_with_object *args
  if args.first.is_a? ::Mongo::Object
    doc, options = args
    options ||= {}
    doc.update_object self, options
  else
    update_without_object *args
  end
end