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
- #count ⇒ Object
- #create_indexes ⇒ Object
- #db ⇒ Object
- #db_name ⇒ Object
- #drop ⇒ Object
- #each ⇒ Object
- #empty? ⇒ Boolean
- #find(query = {}, opts = {}) ⇒ Object
- #find_one(query = {}, opts = {}) ⇒ Object
- #first ⇒ Object
- #index(spec, opts = {}) ⇒ Object
- #index_meta ⇒ Object
Instance Method Details
#all ⇒ Object
68 69 70 |
# File 'lib/mongoo/persistence.rb', line 68 def all find end |
#collection ⇒ Object
28 29 30 |
# File 'lib/mongoo/persistence.rb', line 28 def 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 |
#count ⇒ Object
84 85 86 |
# File 'lib/mongoo/persistence.rb', line 84 def count collection.count end |
#create_indexes ⇒ Object
100 101 102 103 104 105 |
# File 'lib/mongoo/persistence.rb', line 100 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_name ⇒ Object
40 41 42 |
# File 'lib/mongoo/persistence.rb', line 40 def db_name Mongoo.db_name end |
#drop ⇒ Object
88 89 90 |
# File 'lib/mongoo/persistence.rb', line 88 def drop collection.drop end |
#each ⇒ Object
72 73 74 |
# File 'lib/mongoo/persistence.rb', line 72 def each find.each { |found| yield(found) } end |
#empty? ⇒ Boolean
80 81 82 |
# File 'lib/mongoo/persistence.rb', line 80 def empty? count == 0 end |
#find(query = {}, opts = {}) ⇒ Object
44 45 46 |
# File 'lib/mongoo/persistence.rb', line 44 def find(query={}, opts={}) Mongoo::Cursor.new(self, collection.find(query, opts)) end |
#find_one(query = {}, opts = {}) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/mongoo/persistence.rb', line 48 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) doc = new(doc, true) Mongoo::IdentityMap.write(doc) if id_map_on && is_simple_query doc else nil end end |
#first ⇒ Object
76 77 78 |
# File 'lib/mongoo/persistence.rb', line 76 def first find.limit(1).next_document end |
#index(spec, opts = {}) ⇒ Object
96 97 98 |
# File 'lib/mongoo/persistence.rb', line 96 def index(spec, opts={}) self.[spec] = opts end |
#index_meta ⇒ Object
92 93 94 |
# File 'lib/mongoo/persistence.rb', line 92 def Mongoo::INDEX_META[self.collection_name] ||= {} end |