Module: Candy::Crunch::ClassMethods
- Included in:
- Candy::Collection::ClassMethods, Piece::ClassMethods
- Defined in:
- lib/candy/crunch.rb
Instance Method Summary collapse
-
#collection ⇒ Object
Returns the collection you gave, or creates a default collection named for the current class.
-
#collection=(val) ⇒ Object
Accepts either a Mongo::Collection object or a string with the collection name.
-
#connection ⇒ Object
Returns the connection you gave, or uses the application-level Candy collection.
-
#connection=(val) ⇒ Object
First clears any collection and database we’re talking to, then accepts a connection you provide.
-
#db ⇒ Object
Returns the database you gave, or uses the application-level Candy database.
-
#db=(val) ⇒ Object
First clears any collection we’re talking to, then accepts a database you provide.
-
#index(property, direction = :asc) ⇒ Object
Creates an index on the specified property, with an optional direction specified as either :asc or :desc.
-
#password ⇒ Object
The password for Mongo authentication.
-
#password=(val) ⇒ Object
Sets the password for Mongo authentication.
-
#username ⇒ Object
The user for Mongo authentication.
-
#username=(val) ⇒ Object
Sets the user for Mongo authentication.
Instance Method Details
#collection ⇒ Object
Returns the collection you gave, or creates a default collection named for the current class. (By which we mean just the class name, not the full module namespace.)
178 179 180 |
# File 'lib/candy/crunch.rb', line 178 def collection @collection ||= db.collection(collection_name) end |
#collection=(val) ⇒ Object
Accepts either a Mongo::Collection object or a string with the collection name. If you provide a Mongo::Collection object, the default database and connection are not used.
163 164 165 166 167 168 169 170 171 172 173 174 |
# File 'lib/candy/crunch.rb', line 163 def collection=(val) case val when Mongo::Collection @collection = val when String @collection_name = val # Don't collapse the probability wave until called upon when nil @collection = nil else raise ConnectionError, "The collection attribute needs a Mongo::Collection object or a name string." end end |
#connection ⇒ Object
Returns the connection you gave, or uses the application-level Candy collection.
104 105 106 |
# File 'lib/candy/crunch.rb', line 104 def connection @connection ||= Candy.connection end |
#connection=(val) ⇒ Object
First clears any collection and database we’re talking to, then accepts a connection you provide. You’re responsible for your own host, port and options if you use this.
110 111 112 113 |
# File 'lib/candy/crunch.rb', line 110 def connection=(val) self.db = nil @connection = val end |
#db ⇒ Object
Returns the database you gave, or uses the application-level Candy database.
133 134 135 |
# File 'lib/candy/crunch.rb', line 133 def db @db ||= Candy.db end |
#db=(val) ⇒ Object
First clears any collection we’re talking to, then accepts a database you provide. You can provide a Mongo::DB object or a string with the database name. If you provide a Mongo::DB object, the default connection is not used, and the :strict flag should be false or default collection lookup will fail.
118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/candy/crunch.rb', line 118 def db=(val) self.collection = nil case val when Mongo::DB @db = val when String @db = maybe_authenticate(Mongo::DB.new(val, connection)) when nil @db = nil else raise ConnectionError, "The db attribute needs a Mongo::DB object or a name string." end end |
#index(property, direction = :asc) ⇒ Object
Creates an index on the specified property, with an optional direction specified as either :asc or :desc. (Note that this is deliberately a very simple method. If you want multi-key or unique indexes, just call #create_index directly on the collection.)
185 186 187 188 189 190 191 192 193 |
# File 'lib/candy/crunch.rb', line 185 def index(property, direction=:asc) case direction when :asc then mongo_direction = Mongo::ASCENDING when :desc then mongo_direction = Mongo::DESCENDING else raise TypeError, "Index direction should be :asc or :desc" end collection.create_index([[property, mongo_direction]]) end |
#password ⇒ Object
The password for Mongo authentication
157 158 159 |
# File 'lib/candy/crunch.rb', line 157 def password @password ||= Candy.password end |
#password=(val) ⇒ Object
Sets the password for Mongo authentication. Defaults to the global Candy.password. Both username AND password must be set or nothing will happen. Also ignored if you supply your own Mongo::DB object instead of a string.
152 153 154 |
# File 'lib/candy/crunch.rb', line 152 def password=(val) @password = val end |
#username ⇒ Object
The user for Mongo authentication.
145 146 147 |
# File 'lib/candy/crunch.rb', line 145 def username @username ||= Candy.username end |
#username=(val) ⇒ Object
Sets the user for Mongo authentication. Defaults to the global Candy.username. Both username AND password must be set or nothing will happen. Also ignored if you supply your own Mongo::DB object instead of a string.
140 141 142 |
# File 'lib/candy/crunch.rb', line 140 def username=(val) @username = val end |