class MarkMapper::CriteriaHash

Constants

SimpleIdAndTypeQueryKeys

Internal: Used to determine if criteria keys match simple id and type lookup (for single collection inheritance).

SimpleIdQueryKeys

Internal: Used to determine if criteria keys match simple id lookup.

SimpleQueryMaxSize

Internal: Used to quickly check if it is possible that the criteria hash is simple.

Attributes

options[R]

Private: The Hash that stores options

source[R]

Private: The Hash that stores query criteria

Public Class Methods

new(hash={}, options={}) click to toggle source

Public

# File lib/mark_mapper/criteria_hash.rb, line 26
def initialize(hash={}, options={})
  @source, @options = {}, options
  hash.each { |key, value| self[key] = value }
end

Public Instance Methods

==(other) click to toggle source

Public

# File lib/mark_mapper/criteria_hash.rb, line 71
def ==(other)
  @source == other.source
end
[](key) click to toggle source

Public

# File lib/mark_mapper/criteria_hash.rb, line 41
def [](key)
  @source[key]
end
[]=(key, value) click to toggle source

Public The contents of this make me sad…need to clean it up

# File lib/mark_mapper/criteria_hash.rb, line 47
def []=(key, value)
  normalized_key = normalized_key(key)

  if key.is_a?(SymbolOperator)
    operator = :"$#{key.operator}"
    normalized_value = normalized_value(normalized_key, operator, value)
    @source[normalized_key] ||= {}
    @source[normalized_key][operator] = normalized_value
  else
    if key == :conditions
      value.each { |k, v| self[k] = v }
    else
      normalized_value = normalized_value(normalized_key, normalized_key, value)
      @source[normalized_key] = normalized_value
    end
  end
end
initialize_copy(original) click to toggle source
Calls superclass method
# File lib/mark_mapper/criteria_hash.rb, line 31
def initialize_copy(original)
  super
  @options = @options.dup
  @source  = @source.dup
  @source.each do |key, value|
    self[key] = value.clone if value.duplicable?
  end
end
keys() click to toggle source

Public

# File lib/mark_mapper/criteria_hash.rb, line 66
def keys
  @source.keys
end
merge(other) click to toggle source

Public

# File lib/mark_mapper/criteria_hash.rb, line 81
def merge(other)
  self.class.new hash_merge(@source, other.source)
end
merge!(other) click to toggle source

Public

# File lib/mark_mapper/criteria_hash.rb, line 86
def merge!(other)
  merge(other).to_hash.each do |key, value|
    self[key] = value
  end
  self
end
object_id?(key) click to toggle source
# File lib/mark_mapper/criteria_hash.rb, line 104
def object_id?(key)
  object_ids.include?(key.to_sym)
end
object_ids() click to toggle source

Private

# File lib/mark_mapper/criteria_hash.rb, line 109
def object_ids
  @options[:object_ids] ||= []
end
object_ids=(value) click to toggle source

Private

# File lib/mark_mapper/criteria_hash.rb, line 114
def object_ids=(value)
  raise ArgumentError unless value.is_a?(Array)
  @options[:object_ids] = value.flatten
end
simple?() click to toggle source

Public: The definition of simple is querying by only _id or _id and _type. If this is the case, you can use IdentityMap in library to not perform query and instead just return from map.

Returns true or false

# File lib/mark_mapper/criteria_hash.rb, line 98
def simple?
  return false if keys.size > SimpleQueryMaxSize
  key_set = keys.to_set
  key_set == SimpleIdQueryKeys || key_set == SimpleIdAndTypeQueryKeys
end
to_hash() click to toggle source

Public

# File lib/mark_mapper/criteria_hash.rb, line 76
def to_hash
  @source
end

Private Instance Methods

hash_merge(oldhash, newhash) click to toggle source

Private

# File lib/mark_mapper/criteria_hash.rb, line 122
def hash_merge(oldhash, newhash)
  merge_compound_or_clauses!(oldhash, newhash)
  oldhash.merge(newhash) do |key, oldval, newval|
    old_is_hash = oldval.instance_of? Hash
    new_is_hash = newval.instance_of? Hash

    if old_is_hash && new_is_hash
      hash_merge(oldval, newval)
    elsif old_is_hash
      modifier_merge(oldval, newval)
    elsif new_is_hash
      modifier_merge(newval, oldval)
    else
      merge_values_into_array(oldval, newval)
    end
  end
end
key_normalizer() click to toggle source

Private

# File lib/mark_mapper/criteria_hash.rb, line 176
def key_normalizer
  @key_normalizer ||= @options.fetch(:key_normalizer) {
    Normalizers::CriteriaHashKey.new
  }
end
merge_compound_or_clauses!(oldhash, newhash) click to toggle source
# File lib/mark_mapper/criteria_hash.rb, line 140
def merge_compound_or_clauses!(oldhash, newhash)
  old_or = oldhash[:$or]
  new_or = newhash[:$or]
  if old_or && new_or
    oldhash[:$and] ||= []
    oldhash[:$and] << {:$or => oldhash.delete(:$or)}
    oldhash[:$and] << {:$or => newhash.delete(:$or)}
  elsif new_or && oldhash[:$and]
    if oldhash[:$and].any? {|v| v.key? :$or }
      oldhash[:$and] << {:$or => newhash.delete(:$or)}
    end
  elsif old_or && newhash[:$and]
    if newhash[:$and].any? {|v| v.key? :$or }
      newhash[:$and] << {:$or => oldhash.delete(:$or)}
    end
  end
end
merge_values_into_array(value, other_value) click to toggle source

Private

# File lib/mark_mapper/criteria_hash.rb, line 166
def merge_values_into_array(value, other_value)
  Array(value).concat(Array(other_value)).uniq
end
modifier_merge(hash, value) click to toggle source

Private

# File lib/mark_mapper/criteria_hash.rb, line 159
def modifier_merge(hash, value)
  if modifier_key = hash.keys.detect { |k| MarkMapper.modifier?(k) }
    hash[modifier_key].concat( Array(value) ).uniq
  end
end
normalized_key(key) click to toggle source

Private

# File lib/mark_mapper/criteria_hash.rb, line 171
def normalized_key(key)
  key_normalizer.call(key)
end
normalized_value(parent_key, key, value) click to toggle source

Private

# File lib/mark_mapper/criteria_hash.rb, line 183
def normalized_value(parent_key, key, value)
  value_normalizer.call(parent_key, key, value)
end
value_normalizer() click to toggle source

Private

# File lib/mark_mapper/criteria_hash.rb, line 188
def value_normalizer
  @value_normalizer ||= @options.fetch(:value_normalizer) {
    Normalizers::CriteriaHashValue.new(self)
  }
end