module MarkMapper::Query::DSL
finder DSL
methods to delegate to your model if you’re building an ODM e.g. MyModel.last needs to be equivalent to MyModel.query.last
Public Instance Methods
all(opts={})
click to toggle source
# File lib/mark_mapper/query.rb, line 100 def all(opts={}) [].tap do |docs| find_each(opts) {|doc| docs << doc } end end
Also aliased as: to_a
count(opts={})
click to toggle source
# File lib/mark_mapper/query.rb, line 115 def count(opts={}) query = clone.amend(opts) cursor = query.cursor cursor.count end
Also aliased as: size
empty?()
click to toggle source
# File lib/mark_mapper/query.rb, line 165 def empty? count == 0 end
exists?(query_options={})
click to toggle source
# File lib/mark_mapper/query.rb, line 169 def exists?(query_options={}) !only(:_id).find_one(query_options).nil? end
Also aliased as: exist?
fields(*args)
click to toggle source
def distinct(key, opts = {})
query = clone.amend(opts) query.collection.distinct(key, query.criteria_hash)
end
# File lib/mark_mapper/query.rb, line 126 def fields(*args) clone.tap { |query| query.options[:fields] = *args } end
find(*ids)
click to toggle source
# File lib/mark_mapper/query.rb, line 88 def find(*ids) return nil if ids.empty? single_id_find = ids.size == 1 && !ids[0].is_a?(Array) if single_id_find first(:_id => ids[0]) else all(:_id => ids.flatten) end end
find_each(opts={}) { |doc| ... }
click to toggle source
# File lib/mark_mapper/query.rb, line 67 def find_each(opts={}) query = clone.amend(opts) if block_given? result = nil query.cursor do |cursor| result = cursor cursor.each { |doc| yield doc } cursor.rewind! end result else query.cursor end end
Also aliased as: each
find_one(opts={})
click to toggle source
# File lib/mark_mapper/query.rb, line 83 def find_one(opts={}) query = clone.amend(opts) query.collection.find_one(query.criteria_hash, query.options_hash) end
Also aliased as: first
ignore(*args)
click to toggle source
# File lib/mark_mapper/query.rb, line 130 def ignore(*args) set_field_inclusion(args, 0) end
last(opts={})
click to toggle source
# File lib/mark_mapper/query.rb, line 106 def last(opts={}) clone.amend(opts).reverse.find_one end
limit(count=nil)
click to toggle source
# File lib/mark_mapper/query.rb, line 138 def limit(count=nil) clone.tap { |query| query.options[:limit] = count } end
only(*args)
click to toggle source
# File lib/mark_mapper/query.rb, line 134 def only(*args) set_field_inclusion(args, 1) end
paginate(opts={})
click to toggle source
# File lib/mark_mapper/query.rb, line 53 def paginate(opts={}) page = opts.delete(:page) limit = opts.delete(:per_page) || per_page total_entries = opts.delete(:total_entries) query = clone.amend(opts) paginator = Pagination::Paginator.new(total_entries || query.count, page, limit) docs = query.amend({ :limit => paginator.limit, :skip => paginator.skip, }).all Pagination::Collection.new(docs, paginator) end
per_page(limit=nil)
click to toggle source
# File lib/mark_mapper/query.rb, line 47 def per_page(limit=nil) return @per_page || MarkLogic::Cursor::DEFAULT_PAGE_LENGTH if limit.nil? @per_page = limit self end
remove(opts={})
click to toggle source
# File lib/mark_mapper/query.rb, line 110 def remove(opts={}) query = clone.amend(opts) query.collection.remove(query.criteria_hash, query.options_hash) end
reverse()
click to toggle source
# File lib/mark_mapper/query.rb, line 142 def reverse clone.tap do |query| sort = query[:sort] if sort.nil? query.options[:sort] = [[:_id, -1]] else query.options[:sort] = sort.map { |s| [s[0], -s[1]] } end end end
skip(count=nil)
click to toggle source
# File lib/mark_mapper/query.rb, line 153 def skip(count=nil) clone.tap { |query| query.options[:skip] = count } end
Also aliased as: offset
sort(*args)
click to toggle source
# File lib/mark_mapper/query.rb, line 157 def sort(*args) clone.tap { |query| query.options[:sort] = *args } end
Also aliased as: order
where(hash={})
click to toggle source
# File lib/mark_mapper/query.rb, line 161 def where(hash={}) clone.tap { |query| query.criteria.merge!(CriteriaHash.new(hash)) } end
Also aliased as: filter