module MarkMapper::Plugins::Querying

Constants

Methods

Public Instance Methods

delete() click to toggle source
# File lib/mark_mapper/plugins/querying.rb, line 114
def delete
  self.class.delete(id).tap { @_destroyed = true } if persisted?
end
destroy() click to toggle source
# File lib/mark_mapper/plugins/querying.rb, line 110
def destroy
  delete
end
save(options={}) click to toggle source
# File lib/mark_mapper/plugins/querying.rb, line 101
def save(options={})
  options.assert_valid_keys(:validate)
  create_or_update(options)
end
save!(options={}) click to toggle source
# File lib/mark_mapper/plugins/querying.rb, line 106
def save!(options={})
  save(options) || raise(DocumentNotValid.new(self))
end

Private Instance Methods

_update(options={}) click to toggle source
# File lib/mark_mapper/plugins/querying.rb, line 128
def _update(options={})
  save_to_collection(options.reverse_merge(:persistence_method => :save))
end
create(options={}) click to toggle source
# File lib/mark_mapper/plugins/querying.rb, line 124
def create(options={})
  save_to_collection(options.merge(:persistence_method => :insert))
end
create_or_update(options={}) click to toggle source
# File lib/mark_mapper/plugins/querying.rb, line 119
def create_or_update(options={})
  result = persisted? ? _update(options) : create(options)
  result != false
end
save_to_collection(options={}) click to toggle source
# File lib/mark_mapper/plugins/querying.rb, line 132
def save_to_collection(options={})
  @_new = false
  method = options.delete(:persistence_method) || :save
  update = to_marklogic

  case method
  when :insert
    collection.insert(update)
  when :save
    collection.save(update)
  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(find_query, update_query)
    end
  end
end