module MongoMapper::Plugins::Querying
Constants
- Methods
Public Instance Methods
delete()
click to toggle source
# File lib/mongo_mapper/plugins/querying.rb, line 116 def delete self.class.delete(id).tap { @_destroyed = true } if persisted? end
destroy()
click to toggle source
# File lib/mongo_mapper/plugins/querying.rb, line 112 def destroy delete end
save(options={})
click to toggle source
# File lib/mongo_mapper/plugins/querying.rb, line 102 def save(options={}) options.assert_valid_keys(:validate, :safe) create_or_update(options) end
save!(options={})
click to toggle source
# File lib/mongo_mapper/plugins/querying.rb, line 107 def save!(options={}) options.assert_valid_keys(:safe) save(options) || raise(DocumentNotValid.new(self)) end
Private Instance Methods
create(options={})
click to toggle source
# File lib/mongo_mapper/plugins/querying.rb, line 127 def create(options={}) save_to_collection(options.merge(:persistence_method => :insert)) end
create_or_update(options={})
click to toggle source
# File lib/mongo_mapper/plugins/querying.rb, line 122 def create_or_update(options={}) result = persisted? ? update(options) : create(options) result != false end
save_to_collection(options={})
click to toggle source
# File lib/mongo_mapper/plugins/querying.rb, line 135 def save_to_collection(options={}) @_new = false method = options.delete(:persistence_method) || :save update = to_mongo query_options = Utils.get_safe_options(options) if query_options.any? collection = self.collection.with(write: query_options) else collection = self.collection end case method when :insert collection.insert_one(update, query_options) when :save collection.update_one({:_id => _id}.merge(shard_key_filter), update, query_options.merge(upsert: true)) when :update update.stringify_keys! id = update.delete("_id") set_values = update unset_values = {} if fields_for_set = options.delete(:set_fields) set_values = set_values.slice(*fields_for_set) end if fields_for_unset = options.delete(:unset_fields) fields_for_unset.each do |field| unset_values[field] = true end end find_query = { :_id => id } update_query = {} update_query["$set"] = set_values if set_values.any? update_query["$unset"] = unset_values if unset_values.any? if update_query.any? collection.update_one(find_query, update_query, query_options) end end end
update(options={})
click to toggle source
# File lib/mongo_mapper/plugins/querying.rb, line 131 def update(options={}) save_to_collection(options.reverse_merge(:persistence_method => :save)) end