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.)
176 177 178 |
# File 'lib/candy/crunch.rb', line 176 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.
161 162 163 164 165 166 167 168 169 170 171 172 |
# File 'lib/candy/crunch.rb', line 161 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.
102 103 104 |
# File 'lib/candy/crunch.rb', line 102 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.
108 109 110 111 |
# File 'lib/candy/crunch.rb', line 108 def connection=(val) self.db = nil @connection = val end |
#db ⇒ Object
Returns the database you gave, or uses the application-level Candy database.
131 132 133 |
# File 'lib/candy/crunch.rb', line 131 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.
116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/candy/crunch.rb', line 116 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.)
183 184 185 186 187 188 189 190 191 |
# File 'lib/candy/crunch.rb', line 183 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
155 156 157 |
# File 'lib/candy/crunch.rb', line 155 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.
150 151 152 |
# File 'lib/candy/crunch.rb', line 150 def password=(val) @password = val end |
#username ⇒ Object
The user for Mongo authentication.
143 144 145 |
# File 'lib/candy/crunch.rb', line 143 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.
138 139 140 |
# File 'lib/candy/crunch.rb', line 138 def username=(val) @username = val end |