Module: Mongoo::Persistence::ClassMethods
- Defined in:
- lib/mongoo/persistence.rb
Instance Method Summary collapse
- #all ⇒ Object
- #collection ⇒ Object
- #collection_name(val = nil) ⇒ Object
- #conn ⇒ Object
- #conn=(conn_lambda) ⇒ Object
- #count ⇒ Object
- #create_indexes ⇒ Object
- #db ⇒ Object
- #db=(db_name) ⇒ Object
- #drop ⇒ Object
- #each ⇒ Object
- #empty? ⇒ Boolean
- #find(query = {}, opts = {}) ⇒ Object
-
#find_and_modify(opts) ⇒ Hash
Atomically update and return a document using MongoDB’s findAndModify command.
- #find_one(query = {}, opts = {}) ⇒ Object
- #first ⇒ Object
- #index(spec, opts = {}) ⇒ Object
- #index_meta ⇒ Object
Instance Method Details
#all ⇒ Object
101 102 103 |
# File 'lib/mongoo/persistence.rb', line 101 def all find end |
#collection ⇒ Object
57 58 59 |
# File 'lib/mongoo/persistence.rb', line 57 def collection @collection ||= db.collection(collection_name) end |
#collection_name(val = nil) ⇒ Object
20 21 22 23 24 25 26 |
# File 'lib/mongoo/persistence.rb', line 20 def collection_name(val=nil) if val @collection_name = val else @collection_name ||= self.model_name.tableize end end |
#conn ⇒ Object
43 44 45 |
# File 'lib/mongoo/persistence.rb', line 43 def conn @_conn ||= ((@conn_lambda && @conn_lambda.call) || Mongoo.conn) end |
#conn=(conn_lambda) ⇒ Object
28 29 30 31 32 33 34 |
# File 'lib/mongoo/persistence.rb', line 28 def conn=(conn_lambda) @conn_lambda = conn_lambda @_conn = nil @_db = nil @collection = nil @conn_lambda end |
#count ⇒ Object
117 118 119 |
# File 'lib/mongoo/persistence.rb', line 117 def count collection.count end |
#create_indexes ⇒ Object
134 135 136 137 138 139 |
# File 'lib/mongoo/persistence.rb', line 134 def create_indexes self..each do |spec, opts| opts[:background] = true if !opts.has_key?(:background) collection.create_index(spec, opts) end; true end |
#db ⇒ Object
47 48 49 50 51 52 53 54 55 |
# File 'lib/mongoo/persistence.rb', line 47 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
36 37 38 39 40 41 |
# File 'lib/mongoo/persistence.rb', line 36 def db=(db_name) @db_name = db_name @_db = nil @collection = nil @db_name end |
#drop ⇒ Object
121 122 123 |
# File 'lib/mongoo/persistence.rb', line 121 def drop collection.drop end |
#each ⇒ Object
105 106 107 |
# File 'lib/mongoo/persistence.rb', line 105 def each find.each { |found| yield(found) } end |
#empty? ⇒ Boolean
113 114 115 |
# File 'lib/mongoo/persistence.rb', line 113 def empty? count == 0 end |
#find(query = {}, opts = {}) ⇒ Object
81 82 83 |
# File 'lib/mongoo/persistence.rb', line 81 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)
75 76 77 78 79 |
# File 'lib/mongoo/persistence.rb', line 75 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
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/mongoo/persistence.rb', line 85 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 ⇒ Object
109 110 111 |
# File 'lib/mongoo/persistence.rb', line 109 def first find.limit(1).next_document end |
#index(spec, opts = {}) ⇒ Object
130 131 132 |
# File 'lib/mongoo/persistence.rb', line 130 def index(spec, opts={}) self.[spec] = opts end |
#index_meta ⇒ Object
125 126 127 128 |
# File 'lib/mongoo/persistence.rb', line 125 def return @index_meta if @index_meta @index_meta = {} end |