module MarkMapper::Plugins::Querying::ClassMethods

Public Instance Methods

create(*docs) { |doc| ... } click to toggle source
# File lib/mark_mapper/plugins/querying.rb, line 26
def create(*docs)
  initialize_each(*docs) do |doc|
    yield doc if block_given?
    doc.save
  end
end
create!(*docs) { |doc| ... } click to toggle source
# File lib/mark_mapper/plugins/querying.rb, line 33
def create!(*docs)
  initialize_each(*docs) do |doc|
    yield doc if block_given?
    doc.save!
  end
end
criteria_hash(criteria={}) click to toggle source

@api private for now

# File lib/mark_mapper/plugins/querying.rb, line 60
def criteria_hash(criteria={})
  MarkMapper::CriteriaHash.new(criteria, :object_ids => object_id_keys)
end
find_by_id(id) click to toggle source
# File lib/mark_mapper/plugins/querying.rb, line 14
def find_by_id(id)
  find_one(:_id => id)
end
first_or_create(args) click to toggle source
# File lib/mark_mapper/plugins/querying.rb, line 18
def first_or_create(args)
  first(args) || create(args.reject { |key, value| !key?(key) })
end
first_or_new(args) click to toggle source
# File lib/mark_mapper/plugins/querying.rb, line 22
def first_or_new(args)
  first(args) || new(args.reject { |key, value| !key?(key) })
end
query(options={}) click to toggle source

@api private for now

# File lib/mark_mapper/plugins/querying.rb, line 50
def query(options={})
  query = MarkMapper::Plugins::Querying::DecoratedMarkMapperQuery.new(collection, :transformer => transformer)
  query.object_ids(object_id_keys)
  query.amend(options)
  query.model(self)
  query
end
Also aliased as: scoped
scoped(options={})
Alias for: query
update(*args) click to toggle source
# File lib/mark_mapper/plugins/querying.rb, line 40
def update(*args)
  if args.length == 1
    update_multiple(args[0])
  else
    id, attributes = args
    update_single(id, attributes)
  end
end

Private Instance Methods

initialize_each(*docs) { |doc| ... } click to toggle source
# File lib/mark_mapper/plugins/querying.rb, line 69
def initialize_each(*docs)
  instances = []
  docs = [{}] if docs.blank?
  docs.flatten.each do |attrs|
    doc = new(attrs)
    yield(doc)
    instances << doc
  end
  instances.size == 1 ? instances[0] : instances
end
transformer() click to toggle source
# File lib/mark_mapper/plugins/querying.rb, line 65
def transformer
  @transformer ||= lambda { |doc| load(doc) }
end
update_multiple(docs) click to toggle source
# File lib/mark_mapper/plugins/querying.rb, line 90
def update_multiple(docs)
  unless docs.is_a?(Hash)
    raise ArgumentError, "Updating multiple documents takes 1 argument and it must be hash"
  end

  instances = []
  docs.each_pair { |id, attrs| instances << update(id, attrs) }
  instances
end
update_single(id, attrs) click to toggle source
# File lib/mark_mapper/plugins/querying.rb, line 80
def update_single(id, attrs)
  if id.blank? || attrs.blank? || !attrs.is_a?(Hash)
    raise ArgumentError, "Updating a single document requires an id and a hash of attributes"
  end

  find(id).tap do |doc|
    doc.update_attributes(attrs)
  end
end