Module: Mongoo::Persistence
- Included in:
- Base
- Defined in:
- lib/mongoo/persistence.rb
Defined Under Namespace
Modules: ClassMethods
Class Method Summary collapse
Instance Method Summary collapse
- #collection ⇒ Object
- #collection_name ⇒ Object
- #conn ⇒ Object
-
#db ⇒ Object
ClassMethods.
- #destroyed? ⇒ Boolean
- #insert(opts = {}) ⇒ Object
- #insert!(opts = {}) ⇒ Object
- #new_record? ⇒ Boolean
- #persisted? ⇒ Boolean
- #reload(new_doc = nil) ⇒ Object
- #remove(opts = {}) ⇒ Object
- #remove!(opts = {}) ⇒ Object
- #save(*args) ⇒ Object
- #save!(*args) ⇒ Object
- #to_key ⇒ Object
- #to_model ⇒ Object
- #to_param ⇒ Object
- #update(opts = {}) ⇒ Object
- #update!(opts = {}) ⇒ Object
Class Method Details
.included(base) ⇒ Object
15 16 17 |
# File 'lib/mongoo/persistence.rb', line 15 def self.included(base) base.extend(ClassMethods) end |
Instance Method Details
#collection ⇒ Object
179 180 181 |
# File 'lib/mongoo/persistence.rb', line 179 def collection self.class.collection end |
#collection_name ⇒ Object
150 151 152 |
# File 'lib/mongoo/persistence.rb', line 150 def collection_name self.class.collection_name end |
#conn ⇒ Object
146 147 148 |
# File 'lib/mongoo/persistence.rb', line 146 def conn self.class.conn end |
#db ⇒ Object
ClassMethods
142 143 144 |
# File 'lib/mongoo/persistence.rb', line 142 def db self.class.db end |
#destroyed? ⇒ Boolean
261 262 263 |
# File 'lib/mongoo/persistence.rb', line 261 def destroyed? @destroyed != nil end |
#insert(opts = {}) ⇒ Object
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 |
# File 'lib/mongoo/persistence.rb', line 183 def insert(opts={}) ret = _run_insert_callbacks do if persisted? raise AlreadyInsertedError, "document has already been inserted" end unless valid? if opts[:safe] == true raise Mongoo::NotValidError, "document contains errors" else return false end end ret = self.collection.insert(mongohash, opts) unless ret.is_a?(BSON::ObjectId) raise InsertError, "not an object: #{ret.inspect}" end mongohash.delete(:_id) set("_id", ret) @persisted = true set_persisted_mongohash(mongohash) ret end Mongoo::IdentityMap.write(self) if Mongoo::IdentityMap.on? ret end |
#insert!(opts = {}) ⇒ Object
209 210 211 |
# File 'lib/mongoo/persistence.rb', line 209 def insert!(opts={}) insert(opts.merge(:safe => true)) end |
#new_record? ⇒ Boolean
265 266 267 |
# File 'lib/mongoo/persistence.rb', line 265 def new_record? !persisted? end |
#persisted? ⇒ Boolean
174 175 176 177 |
# File 'lib/mongoo/persistence.rb', line 174 def persisted? @persisted == true #!get("_id").nil? end |
#reload(new_doc = nil) ⇒ Object
289 290 291 292 293 294 295 |
# File 'lib/mongoo/persistence.rb', line 289 def reload(new_doc=nil) new_doc ||= collection.find_one(get("_id")) init_from_hash(new_doc) @persisted = true set_persisted_mongohash(mongohash) true end |
#remove(opts = {}) ⇒ Object
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 |
# File 'lib/mongoo/persistence.rb', line 269 def remove(opts={}) _run_remove_callbacks do unless persisted? raise NotInsertedError, "document must be inserted before it can be removed" end ret = self.collection.remove({"_id" => get("_id")}, opts) if !ret.is_a?(Hash) || (ret["err"] == nil && ret["n"] == 1) @destroyed = true @persisted = false true else raise RemoveError, ret.inspect end end end |
#remove!(opts = {}) ⇒ Object
285 286 287 |
# File 'lib/mongoo/persistence.rb', line 285 def remove!(opts={}) remove(opts.merge(:safe => true)) end |
#save(*args) ⇒ Object
170 171 172 |
# File 'lib/mongoo/persistence.rb', line 170 def save(*args) persisted? ? update(*args) : insert(*args) end |
#save!(*args) ⇒ Object
166 167 168 |
# File 'lib/mongoo/persistence.rb', line 166 def save!(*args) persisted? ? update!(*args) : insert!(*args) end |
#to_key ⇒ Object
158 159 160 |
# File 'lib/mongoo/persistence.rb', line 158 def to_key get("_id") end |
#to_model ⇒ Object
162 163 164 |
# File 'lib/mongoo/persistence.rb', line 162 def to_model self end |
#to_param ⇒ Object
154 155 156 |
# File 'lib/mongoo/persistence.rb', line 154 def to_param persisted? ? get("_id").to_s : nil end |
#update(opts = {}) ⇒ Object
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 |
# File 'lib/mongoo/persistence.rb', line 213 def update(opts={}) _run_update_callbacks do unless persisted? raise NotInsertedError, "document must be inserted before being updated" end unless valid? if opts[:safe] == true raise Mongoo::NotValidError, "document contains errors" else return false end end opts[:only_if_current] = true unless opts.has_key?(:only_if_current) opts[:safe] = true if !opts.has_key?(:safe) && opts[:only_if_current] == true update_hash = build_update_hash(self.changelog) return true if update_hash.empty? update_query_hash = build_update_query_hash(persisted_mongohash.to_key_value, self.changelog) if Mongoo.verbose_debug puts "\n* update_query_hash: #{update_query_hash.inspect}\n update_hash: #{update_hash.inspect}\n opts: #{opts.inspect}\n" end if opts.delete(:find_and_modify) == true ret = self.collection.find_and_modify(query: update_query_hash, update: update_hash, new: true) reload(ret) else ret = self.collection.update(update_query_hash, update_hash, opts) if !ret.is_a?(Hash) || (ret["updatedExisting"] && ret["n"] == 1) set_persisted_mongohash(mongohash) @persisted = true true else if opts[:only_if_current] raise StaleUpdateError, ret.inspect else raise UpdateError, ret.inspect end end end # if opts.delete(:find_and_modify) end end |
#update!(opts = {}) ⇒ Object
257 258 259 |
# File 'lib/mongoo/persistence.rb', line 257 def update!(opts={}) update(opts.merge(:safe => true)) end |