Module: MongoMapper::Document::ClassMethods

Defined in:
lib/mongo_mapper/document.rb

Instance Method Summary collapse

Instance Method Details

#all(options = {}) ⇒ Object



97
98
99
# File 'lib/mongo_mapper/document.rb', line 97

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

#count(options = {}) ⇒ Object



101
102
103
# File 'lib/mongo_mapper/document.rb', line 101

def count(options={})
  query(options).count
end

#create(*docs) ⇒ Object



109
110
111
# File 'lib/mongo_mapper/document.rb', line 109

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

#create!(*docs) ⇒ Object



113
114
115
# File 'lib/mongo_mapper/document.rb', line 113

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

#delete(*ids) ⇒ Object



126
127
128
# File 'lib/mongo_mapper/document.rb', line 126

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

#delete_all(options = {}) ⇒ Object



130
131
132
# File 'lib/mongo_mapper/document.rb', line 130

def delete_all(options={})
  query(options).remove
end

#destroy(*ids) ⇒ Object



134
135
136
# File 'lib/mongo_mapper/document.rb', line 134

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

#destroy_all(options = {}) ⇒ Object



138
139
140
# File 'lib/mongo_mapper/document.rb', line 138

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

#embeddable?Boolean

Returns:



142
143
144
# File 'lib/mongo_mapper/document.rb', line 142

def embeddable?
  false
end

#enslaveObject



146
147
148
# File 'lib/mongo_mapper/document.rb', line 146

def enslave
  @enslave = true
end

#enslave?Boolean

Returns:



150
151
152
# File 'lib/mongo_mapper/document.rb', line 150

def enslave?
  @enslave ||= false
end

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



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

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

#exists?(options = {}) ⇒ Boolean

Returns:



105
106
107
# File 'lib/mongo_mapper/document.rb', line 105

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

#find(*args) ⇒ Object



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

def find(*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
    query = query(options).update(:_id => args[0])
    find_one(query.to_hash)
  end
end

#find!(*args) ⇒ Object

Raises:



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

def find!(*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
    query = query(options).update(:_id => args[0])
    find_one(query.to_hash) || raise(DocumentNotFound, "Document match #{options.inspect} does not exist in #{collection.name} collection")
  end
end

#find_by_id(id) ⇒ Object



76
77
78
# File 'lib/mongo_mapper/document.rb', line 76

def find_by_id(id)
  find(id)
end

#find_each(options = {}) ⇒ Object



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

def find_each(options={})
  query(options).find().each { |doc| yield load(doc) }
end

#first(options = {}) ⇒ Object



88
89
90
# File 'lib/mongo_mapper/document.rb', line 88

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

#first_or_create(args) ⇒ Object



80
81
82
# File 'lib/mongo_mapper/document.rb', line 80

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

#first_or_new(args) ⇒ Object



84
85
86
# File 'lib/mongo_mapper/document.rb', line 84

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

#inherited(subclass) ⇒ Object



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

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

#last(options = {}) ⇒ Object



92
93
94
95
# File 'lib/mongo_mapper/document.rb', line 92

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

#query(options = {}) ⇒ Object



163
164
165
# File 'lib/mongo_mapper/document.rb', line 163

def query(options={})
  Query.new(self, options)
end

#single_collection_inherited?Boolean

Returns:



154
155
156
# File 'lib/mongo_mapper/document.rb', line 154

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

#single_collection_inherited_superclass?Boolean

Returns:



158
159
160
# File 'lib/mongo_mapper/document.rb', line 158

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

#update(*args) ⇒ Object



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

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