Class: Mongo::Sugar::Collection

Inherits:
Delegator
  • Object
show all
Includes:
Singleton
Defined in:
lib/mongo-sugar/collection.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(store_in = {}) ⇒ Collection

Instance methods



31
32
33
# File 'lib/mongo-sugar/collection.rb', line 31

def initialize(store_in={})
  __setobj__(self.class.store_in_options || {})
end

Class Attribute Details

.store_in_optionsObject (readonly)

Returns the value of attribute store_in_options.



15
16
17
# File 'lib/mongo-sugar/collection.rb', line 15

def store_in_options
  @store_in_options
end

Instance Attribute Details

#databaseObject (readonly)

Returns the value of attribute database.



8
9
10
# File 'lib/mongo-sugar/collection.rb', line 8

def database
  @database
end

Class Method Details

.databaseObject



18
19
20
# File 'lib/mongo-sugar/collection.rb', line 18

def database
  self.instance.database
end

.store_in(options) ⇒ Object



22
23
24
# File 'lib/mongo-sugar/collection.rb', line 22

def store_in(options)
  @store_in_options = options
end

Instance Method Details

#__getobj__Object



44
45
46
# File 'lib/mongo-sugar/collection.rb', line 44

def __getobj__
  @collection
end

#__setobj__(store_in_options) ⇒ Object



35
36
37
38
39
40
41
42
# File 'lib/mongo-sugar/collection.rb', line 35

def __setobj__(store_in_options)
  conn = store_in_options.delete(:connection) || 'default'
  db = store_in_options.delete(:database)
  coll = store_in_options.delete(:collection) || self.class.name.delete(':').gsub(/(.)([A-Z])/,'\1_\2').downcase

  @database = Mongo::Sugar::Connection::Manager.database(conn, db)
  @collection = @database[coll]
end