module Mongoo::Persistence
Public Class Methods
included(base)
click to toggle source
# File lib/mongoo/persistence.rb, line 31 def self.included(base) base.extend(ClassMethods) end
Public Instance Methods
collection()
click to toggle source
# File lib/mongoo/persistence.rb, line 195 def collection self.class.collection end
collection_name()
click to toggle source
# File lib/mongoo/persistence.rb, line 166 def collection_name self.class.collection_name end
conn()
click to toggle source
# File lib/mongoo/persistence.rb, line 162 def conn self.class.conn end
db()
click to toggle source
# File lib/mongoo/persistence.rb, line 158 def db self.class.db end
destroyed?()
click to toggle source
# File lib/mongoo/persistence.rb, line 294 def destroyed? @destroyed != nil end
insert(opts={})
click to toggle source
# File lib/mongoo/persistence.rb, line 199 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 rescue Mongo::OperationFailure => e if e.message.to_s =~ /^11000\:/ raise Mongoo::DuplicateKeyError, e.message else raise e end end
insert!(opts={})
click to toggle source
# File lib/mongoo/persistence.rb, line 231 def insert!(opts={}) insert(opts.merge(:safe => true)) end
new_record?()
click to toggle source
# File lib/mongoo/persistence.rb, line 298 def new_record? !persisted? end
persisted?()
click to toggle source
# File lib/mongoo/persistence.rb, line 190 def persisted? @persisted == true #!get("_id").nil? end
raw_update(&block)
click to toggle source
# File lib/mongoo/persistence.rb, line 235 def raw_update(&block) raw = RawUpdate.new(self) block.call(raw) raw.run end
reload(new_doc=nil)
click to toggle source
# File lib/mongoo/persistence.rb, line 322 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={})
click to toggle source
# File lib/mongoo/persistence.rb, line 302 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={})
click to toggle source
# File lib/mongoo/persistence.rb, line 318 def remove!(opts={}) remove(opts.merge(:safe => true)) end
save(*args)
click to toggle source
# File lib/mongoo/persistence.rb, line 186 def save(*args) persisted? ? update(*args) : insert(*args) end
save!(*args)
click to toggle source
# File lib/mongoo/persistence.rb, line 182 def save!(*args) persisted? ? update!(*args) : insert!(*args) end
to_key()
click to toggle source
# File lib/mongoo/persistence.rb, line 174 def to_key get("_id") end
to_model()
click to toggle source
# File lib/mongoo/persistence.rb, line 178 def to_model self end
to_param()
click to toggle source
# File lib/mongoo/persistence.rb, line 170 def to_param persisted? ? get("_id").to_s : nil end
update(opts={})
click to toggle source
# File lib/mongoo/persistence.rb, line 241 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) reset_persisted_mongohash 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 rescue Mongo::OperationFailure => e if e.message.to_s =~ /^11000\:/ raise Mongoo::DuplicateKeyError, e.message else raise e end end
update!(opts={})
click to toggle source
# File lib/mongoo/persistence.rb, line 290 def update!(opts={}) update(opts.merge(:safe => true)) end
Protected Instance Methods
build_update_hash(changelog)
click to toggle source
# File lib/mongoo/persistence.rb, line 330 def build_update_hash(changelog) update_hash = {} changelog.each do |op, k, v| update_hash["$#{op}"] ||= {} update_hash["$#{op}"][k] = v end update_hash end
build_update_query_hash(persisted_mongohash_kv, changelog)
click to toggle source
# File lib/mongoo/persistence.rb, line 340 def build_update_query_hash(persisted_mongohash_kv, changelog) update_query_hash = { "_id" => get("_id") } changelog.each do |op, k, v| if persisted_val = persisted_mongohash_kv[k] if persisted_val == [] # work around a bug where mongo won't find a doc # using an empty array [] if an index is defined # on that field. persisted_val = { "$size" => 0 } end update_query_hash[k] = persisted_val end end update_query_hash end