module Mongoo::Persistence::ClassMethods

Public Instance Methods

all() click to toggle source
# File lib/mongoo/persistence.rb, line 117
def all
  find
end
collection() click to toggle source
# File lib/mongoo/persistence.rb, line 73
def collection
  @collection ||= db.collection(collection_name)
end
collection_name(val=nil) click to toggle source
# File lib/mongoo/persistence.rb, line 36
def collection_name(val=nil)
  if val
    @collection_name = val
  else
    @collection_name ||= self.model_name.tableize
  end
end
conn() click to toggle source
# File lib/mongoo/persistence.rb, line 59
def conn
  @_conn ||= ((@conn_lambda && @conn_lambda.call) || Mongoo.conn)
end
conn=(conn_lambda) click to toggle source
# File lib/mongoo/persistence.rb, line 44
def conn=(conn_lambda)
  @conn_lambda = conn_lambda
  @_conn = nil
  @_db = nil
  @collection = nil
  @conn_lambda
end
count() click to toggle source
# File lib/mongoo/persistence.rb, line 133
def count
  collection.count
end
create_indexes() click to toggle source
# File lib/mongoo/persistence.rb, line 150
def create_indexes
  self.index_meta.each do |spec, opts|
    opts[:background] = true if !opts.has_key?(:background)
    collection.create_index(spec, opts)
  end; true
end
db() click to toggle source
# File lib/mongoo/persistence.rb, line 63
def db
  @_db ||= begin
    if db_name = (@db_name || (@conn_lambda && Mongoo.db.name))
      conn.db(db_name)
    else
      Mongoo.db
    end
  end
end
db=(db_name) click to toggle source
# File lib/mongoo/persistence.rb, line 52
def db=(db_name)
  @db_name = db_name
  @_db = nil
  @collection = nil
  @db_name
end
drop() click to toggle source
# File lib/mongoo/persistence.rb, line 137
def drop
  collection.drop
end
each() { |found| ... } click to toggle source
# File lib/mongoo/persistence.rb, line 121
def each
  find.each { |found| yield(found) }
end
empty?() click to toggle source
# File lib/mongoo/persistence.rb, line 129
def empty?
  count == 0
end
find(query={}, opts={}) click to toggle source
# File lib/mongoo/persistence.rb, line 97
def find(query={}, opts={})
  Mongoo::Cursor.new(self, collection.find(query, opts))
end
find_and_modify(opts) click to toggle source

Atomically update and return a document using MongoDB’s findAndModify command. (MongoDB > 1.3.0)

@option opts [Hash] :query ({}) a query selector document for matching the desired document. @option opts [Hash] :update (nil) the update operation to perform on the matched document. @option opts [Array, String, OrderedHash] :sort ({}) specify a sort option for the query using any

of the sort options available for Cursor#sort. Sort order is important if the query will be matching
multiple documents since only the first matching document will be updated and returned.

@option opts [Boolean] :remove (false) If true, removes the the returned document from the collection. @option opts [Boolean] :new (false) If true, returns the updated document; otherwise, returns the document

prior to update.

@return [Hash] the matched document.

@core findandmodify find_and_modify-instance_method

# File lib/mongoo/persistence.rb, line 91
def find_and_modify(opts)
  if doc = collection.find_and_modify(opts)
    Mongoo::Cursor.new(self, nil).obj_from_doc(doc)
  end
end
find_one(query={}, opts={}) click to toggle source
# File lib/mongoo/persistence.rb, line 101
def find_one(query={}, opts={})
  id_map_on = Mongoo::IdentityMap.on?
  is_simple_query = nil
  is_simple_query = Mongoo::IdentityMap.simple_query?(query, opts) if id_map_on

  if id_map_on && is_simple_query
    if doc = Mongoo::IdentityMap.read(query)
      return doc
    end
  end

  if doc = collection.find_one(query, opts)
    Mongoo::Cursor.new(self, nil).obj_from_doc(doc)
  end
end
first() click to toggle source
# File lib/mongoo/persistence.rb, line 125
def first
  find.limit(1).next_document
end
index(spec, opts={}) click to toggle source
# File lib/mongoo/persistence.rb, line 146
def index(spec, opts={})
  self.index_meta[spec] = opts
end
index_meta() click to toggle source
# File lib/mongoo/persistence.rb, line 141
def index_meta
  return @index_meta if @index_meta
  @index_meta = {}
end