Module: MongoMapper::Document::ClassMethods

Defined in:
lib/mongo_mapper/document.rb

Instance Method Summary collapse

Instance Method Details

#all(options = {}) ⇒ Object



99
100
101
# File 'lib/mongo_mapper/document.rb', line 99

def all(options={})
  find_many(options)
end

#count(options = {}) ⇒ Object



103
104
105
# File 'lib/mongo_mapper/document.rb', line 103

def count(options={})
  collection.find(to_criteria(options)).count
end

#create(*docs) ⇒ Object



111
112
113
# File 'lib/mongo_mapper/document.rb', line 111

def create(*docs)
  initialize_each(*docs) { |doc| doc.save }
end

#create!(*docs) ⇒ Object



115
116
117
# File 'lib/mongo_mapper/document.rb', line 115

def create!(*docs)
  initialize_each(*docs) { |doc| doc.save! }
end

#delete(*ids) ⇒ Object



128
129
130
# File 'lib/mongo_mapper/document.rb', line 128

def delete(*ids)
  collection.remove(to_criteria(:_id => ids.flatten))
end

#delete_all(options = {}) ⇒ Object



132
133
134
# File 'lib/mongo_mapper/document.rb', line 132

def delete_all(options={})
  collection.remove(to_criteria(options))
end

#destroy(*ids) ⇒ Object



136
137
138
# File 'lib/mongo_mapper/document.rb', line 136

def destroy(*ids)
  find_some!(ids.flatten).each(&:destroy)
end

#destroy_all(options = {}) ⇒ Object



140
141
142
# File 'lib/mongo_mapper/document.rb', line 140

def destroy_all(options={})
  find_each(options) { |document| document.destroy }
end

#embeddable?Boolean

Returns:



144
145
146
# File 'lib/mongo_mapper/document.rb', line 144

def embeddable?
  false
end

#ensure_index(spec, options = {}) ⇒ Object



43
44
45
# File 'lib/mongo_mapper/document.rb', line 43

def ensure_index(spec, options={})
  collection.create_index(spec, options)
end

#exists?(options = {}) ⇒ Boolean

Returns:



107
108
109
# File 'lib/mongo_mapper/document.rb', line 107

def exists?(options={})
  !count(options).zero?
end

#find(*args) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
# File 'lib/mongo_mapper/document.rb', line 47

def find(*args)
  assert_no_first_last_or_all(args)
  options = args.extract_options!
  return nil if args.size == 0

  if args.first.is_a?(Array) || args.size > 1
    find_some(args, options)
  else
    find_one(options.merge({:_id => args[0]}))
  end
end

#find!(*args) ⇒ Object

Raises:



59
60
61
62
63
64
65
66
67
68
69
# File 'lib/mongo_mapper/document.rb', line 59

def find!(*args)
  assert_no_first_last_or_all(args)
  options = args.extract_options!
  raise DocumentNotFound, "Couldn't find without an ID" if args.size == 0

  if args.first.is_a?(Array) || args.size > 1
    find_some!(args, options)
  else
    find_one(options.merge({:_id => args[0]})) || raise(DocumentNotFound, "Document match #{options.inspect} does not exist in #{collection.name} collection")
  end
end

#find_by_id(id) ⇒ Object



78
79
80
# File 'lib/mongo_mapper/document.rb', line 78

def find_by_id(id)
  find(id)
end

#find_each(options = {}) ⇒ Object



71
72
73
74
75
76
# File 'lib/mongo_mapper/document.rb', line 71

def find_each(options={})
  criteria, options = to_query(options)
  collection.find(criteria, options).each do |doc|
    yield load(doc)
  end
end

#first(options = {}) ⇒ Object



90
91
92
# File 'lib/mongo_mapper/document.rb', line 90

def first(options={})
  find_one(options)
end

#first_or_create(args) ⇒ Object



82
83
84
# File 'lib/mongo_mapper/document.rb', line 82

def first_or_create(args)
  first(args) || create(args.reject { |key, value| !key?(key) })
end

#first_or_new(args) ⇒ Object



86
87
88
# File 'lib/mongo_mapper/document.rb', line 86

def first_or_new(args)
  first(args) || new(args.reject { |key, value| !key?(key) })
end

#inherited(subclass) ⇒ Object



38
39
40
41
# File 'lib/mongo_mapper/document.rb', line 38

def inherited(subclass)
  subclass.set_collection_name(collection_name)
  super
end

#last(options = {}) ⇒ Object



94
95
96
97
# File 'lib/mongo_mapper/document.rb', line 94

def last(options={})
  raise ':order option must be provided when using last' if options[:order].blank?
  find_one(options.merge(:order => invert_order_clause(options[:order])))
end

#single_collection_inherited?Boolean

Returns:



148
149
150
# File 'lib/mongo_mapper/document.rb', line 148

def single_collection_inherited?
  keys.key?(:_type) && single_collection_inherited_superclass?
end

#single_collection_inherited_superclass?Boolean

Returns:



152
153
154
# File 'lib/mongo_mapper/document.rb', line 152

def single_collection_inherited_superclass?
  superclass.respond_to?(:keys) && superclass.keys.key?(:_type)
end

#update(*args) ⇒ Object



119
120
121
122
123
124
125
126
# File 'lib/mongo_mapper/document.rb', line 119

def update(*args)
  if args.length == 1
    update_multiple(args[0])
  else
    id, attributes = args
    update_single(id, attributes)
  end
end