Module: Mongo::Model::Db::ClassMethods

Defined in:
lib/mongo_db/model/db.rb

Instance Method Summary collapse

Instance Method Details

#collection(*args, &block) ⇒ Object



36
37
38
39
40
41
42
43
44
45
# File 'lib/mongo_db/model/db.rb', line 36

def collection *args, &block
  if block
    self.collection = block
  elsif !args.empty?
    args.size.must == 1
    self.collection = args.first
  else
    (_collection && _collection.call) || db.collection(default_collection_name)
  end
end

#collection=(v) ⇒ Object



26
27
28
29
30
31
32
33
34
# File 'lib/mongo_db/model/db.rb', line 26

def collection= v
  self._collection = if v.is_a? ::Proc
    v
  elsif v.is_a? ::Symbol
    -> {db.collection v}
  else
    -> {v}
  end
end

#db(*args, &block) ⇒ Object



14
15
16
17
18
19
20
21
22
23
# File 'lib/mongo_db/model/db.rb', line 14

def db *args, &block
  if block
    self.db = block
  elsif !args.empty?
    args.size.must == 1
    self.db = args.first
  else
    (_db && _db.call) || ::Mongo::Model.db
  end
end

#db=(v) ⇒ Object



4
5
6
7
8
9
10
11
12
# File 'lib/mongo_db/model/db.rb', line 4

def db= v
  self._db = if v.is_a? ::Proc
    v
  elsif v.is_a? ::Symbol
    -> {::Mongo::Model.connection.db v.to_s}
  else
    -> {v}
  end
end

#default_collection_nameObject



47
48
49
50
51
# File 'lib/mongo_db/model/db.rb', line 47

def default_collection_name
  first_ancestor_class = ancestors.find{|a| a.is_a? Class} ||
    raise("can't evaluate default collection name for #{self}!")
  first_ancestor_class.alias.pluralize.underscore.to_sym
end