Module: Mongoo::Persistence::ClassMethods

Defined in:
lib/mongoo/persistence.rb

Instance Method Summary collapse

Instance Method Details

#allObject


68
69
70
# File 'lib/mongoo/persistence.rb', line 68

def all
  find
end

#collectionObject


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

#connObject


32
33
34
# File 'lib/mongoo/persistence.rb', line 32

def conn
  Mongoo.conn
end

#countObject


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

def count
  collection.count
end

#create_indexesObject


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

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


36
37
38
# File 'lib/mongoo/persistence.rb', line 36

def db
  Mongoo.db
end

#db_nameObject


40
41
42
# File 'lib/mongoo/persistence.rb', line 40

def db_name
  Mongoo.db_name
end

#dropObject


88
89
90
# File 'lib/mongoo/persistence.rb', line 88

def drop
  collection.drop
end

#eachObject


72
73
74
# File 'lib/mongoo/persistence.rb', line 72

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

#empty?Boolean

Returns:

  • (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

#firstObject


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.index_meta[spec] = opts
end

#index_metaObject


92
93
94
# File 'lib/mongoo/persistence.rb', line 92

def index_meta
  Mongoo::INDEX_META[self.collection_name] ||= {}
end