Module: Mongoo::Persistence::ClassMethods

Defined in:
lib/mongoo/persistence.rb

Instance Method Summary collapse

Instance Method Details

#allObject



128
129
130
# File 'lib/mongoo/persistence.rb', line 128

def all
  find
end

#collectionObject



84
85
86
# File 'lib/mongoo/persistence.rb', line 84

def collection
  @collection ||= db.collection(collection_name)
end

#collection_name(val = nil) ⇒ Object



47
48
49
50
51
52
53
# File 'lib/mongoo/persistence.rb', line 47

def collection_name(val=nil)
  if val
    @collection_name = val
  else
    @collection_name ||= self.model_name.tableize
  end
end

#connObject



70
71
72
# File 'lib/mongoo/persistence.rb', line 70

def conn
  @_conn ||= ((@conn_lambda && @conn_lambda.call) || Mongoo.conn)
end

#conn=(conn_lambda) ⇒ Object



55
56
57
58
59
60
61
# File 'lib/mongoo/persistence.rb', line 55

def conn=(conn_lambda)
  @conn_lambda = conn_lambda
  @_conn = nil
  @_db = nil
  @collection = nil
  @conn_lambda
end

#countObject



144
145
146
# File 'lib/mongoo/persistence.rb', line 144

def count
  collection.count
end

#create_indexesObject



161
162
163
164
165
166
# File 'lib/mongoo/persistence.rb', line 161

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

#dbObject



74
75
76
77
78
79
80
81
82
# File 'lib/mongoo/persistence.rb', line 74

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) ⇒ Object



63
64
65
66
67
68
# File 'lib/mongoo/persistence.rb', line 63

def db=(db_name)
  @db_name = db_name
  @_db = nil
  @collection = nil
  @db_name
end

#dropObject



148
149
150
# File 'lib/mongoo/persistence.rb', line 148

def drop
  collection.drop
end

#eachObject



132
133
134
# File 'lib/mongoo/persistence.rb', line 132

def each
  find.each { |found| yield(found) }
end

#empty?Boolean

Returns:

  • (Boolean)


140
141
142
# File 'lib/mongoo/persistence.rb', line 140

def empty?
  count == 0
end

#find(query = {}, opts = {}) ⇒ Object



108
109
110
# File 'lib/mongoo/persistence.rb', line 108

def find(query={}, opts={})
  Mongoo::Cursor.new(self, collection.find(query, opts))
end

#find_and_modify(opts) ⇒ Hash

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

Parameters:

  • opts (Hash)

    a customizable set of options

Options Hash (opts):

  • :query (Hash) — default: {}

    a query selector document for matching the desired document.

  • :update (Hash) — default: nil

    the update operation to perform on the matched document.

  • :sort (Array, String, OrderedHash) — default: {}

    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.

  • :remove (Boolean) — default: false

    If true, removes the the returned document from the collection.

  • :new (Boolean) — default: false

    If true, returns the updated document; otherwise, returns the document prior to update.

Returns:

  • (Hash)

    the matched document.



102
103
104
105
106
# File 'lib/mongoo/persistence.rb', line 102

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 = {}) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/mongoo/persistence.rb', line 112

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

#firstObject



136
137
138
# File 'lib/mongoo/persistence.rb', line 136

def first
  find.limit(1).next_document
end

#index(spec, opts = {}) ⇒ Object



157
158
159
# File 'lib/mongoo/persistence.rb', line 157

def index(spec, opts={})
  self.index_meta[spec] = opts
end

#index_metaObject



152
153
154
155
# File 'lib/mongoo/persistence.rb', line 152

def index_meta
  return @index_meta if @index_meta
  @index_meta = {}
end