class MarkMapper::Query

Constants

OptionKeys

Private

Attributes

collection[R]
criteria[R]
options[R]

Public Class Methods

new(collection, query_options = {}) click to toggle source

Public

# File lib/mark_mapper/query.rb, line 26
def initialize(collection, query_options = {})
  @collection, @options, @criteria = collection, OptionsHash.new, CriteriaHash.new
  query_options.each { |key, value| self[key] = value }
end

Public Instance Methods

[](key) click to toggle source
# File lib/mark_mapper/query.rb, line 194
def [](key)
  key = symbolized_key(key)
  source = hash_for_key(key)
  source[key]
end
[]=(key, value) click to toggle source
# File lib/mark_mapper/query.rb, line 200
def []=(key, value)
  key = symbolized_key(key)
  source = hash_for_key(key)
  source[key] = value
end
amend(opts={}) click to toggle source
# File lib/mark_mapper/query.rb, line 189
def amend(opts={})
  opts.each { |key, value| self[key] = value }
  self
end
criteria_hash() click to toggle source
# File lib/mark_mapper/query.rb, line 227
def criteria_hash
  @criteria.to_hash
end
cursor(&block) click to toggle source
# File lib/mark_mapper/query.rb, line 235
def cursor(&block)
  @collection.find(criteria_hash, options_hash, &block)
end
explain() click to toggle source
# File lib/mark_mapper/query.rb, line 216
def explain
  @collection.find(query.criteria_hash, query.options_hash).explain
end
initialize_copy(original) click to toggle source
Calls superclass method
# File lib/mark_mapper/query.rb, line 31
def initialize_copy(original)
  super
  @criteria = @criteria.dup
  @options = @options.dup
end
inspect() click to toggle source
# File lib/mark_mapper/query.rb, line 220
def inspect
  as_nice_string = to_hash.collect do |key, value|
    " #{key}: #{value.inspect}"
  end.sort.join(",")
  "#<#{self.class}#{as_nice_string}>"
end
merge(other) click to toggle source
# File lib/mark_mapper/query.rb, line 206
def merge(other)
  merged_criteria = @criteria.merge(other.criteria).to_hash
  merged_options  = @options.merge(other.options).to_hash
  clone.amend(merged_criteria).amend(merged_options)
end
object_ids(*keys) click to toggle source

Public

# File lib/mark_mapper/query.rb, line 38
def object_ids(*keys)
  return @criteria.object_ids if keys.empty?
  @criteria.object_ids = *keys
  self
end
options_hash() click to toggle source
# File lib/mark_mapper/query.rb, line 231
def options_hash
  @options.to_hash
end
to_hash() click to toggle source
# File lib/mark_mapper/query.rb, line 212
def to_hash
  criteria_hash.merge(options_hash)
end
update(document, driver_opts={}) click to toggle source
# File lib/mark_mapper/query.rb, line 184
def update(document, driver_opts={})
  query = clone
  query.collection.update(query.criteria_hash, document, driver_opts)
end

Private Instance Methods

hash_for_key(key) click to toggle source

Private

# File lib/mark_mapper/query.rb, line 253
def hash_for_key(key)
  options_key?(key) ? @options : @criteria
end
options_key?(key) click to toggle source

Private

# File lib/mark_mapper/query.rb, line 267
def options_key?(key)
  OptionKeys.include?(key)
end
qb() click to toggle source

Private

# File lib/mark_mapper/query.rb, line 248
def qb
  @qb || MarkLogic::QueryBuilder.new(@collection)
end
set_field_inclusion(fields, value) click to toggle source

Private

# File lib/mark_mapper/query.rb, line 272
def set_field_inclusion(fields, value)
  fields_option = {}
  fields.each { |field| fields_option[symbolized_key(field)] = value }
  clone.tap { |query| query.options[:fields] = fields_option }
end
symbolized_key(key) click to toggle source

Private

# File lib/mark_mapper/query.rb, line 258
def symbolized_key(key)
  if key.respond_to?(:to_sym)
    key.to_sym
  else
    key
  end
end