Class: MarkMapper::CriteriaHash

Inherits:
Object
  • Object
show all
Defined in:
lib/mark_mapper/criteria_hash.rb

Constant Summary collapse

SimpleIdQueryKeys =

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

[:_id].to_set
SimpleIdAndTypeQueryKeys =

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

[:_id, :_type].to_set
SimpleQueryMaxSize =

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

[SimpleIdQueryKeys.size, SimpleIdAndTypeQueryKeys.size].max

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash = {}, options = {}) ⇒ CriteriaHash

Public


26
27
28
29
# File 'lib/mark_mapper/criteria_hash.rb', line 26

def initialize(hash={}, options={})
  @source, @options = {}, options
  hash.each { |key, value| self[key] = value }
end

Instance Attribute Details

#optionsObject (readonly)

Private: The Hash that stores options


12
13
14
# File 'lib/mark_mapper/criteria_hash.rb', line 12

def options
  @options
end

#sourceObject (readonly)

Private: The Hash that stores query criteria


9
10
11
# File 'lib/mark_mapper/criteria_hash.rb', line 9

def source
  @source
end

Instance Method Details

#==(other) ⇒ Object

Public


71
72
73
# File 'lib/mark_mapper/criteria_hash.rb', line 71

def ==(other)
  @source == other.source
end

#[](key) ⇒ Object

Public


41
42
43
# File 'lib/mark_mapper/criteria_hash.rb', line 41

def [](key)
  @source[key]
end

#[]=(key, value) ⇒ Object

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


47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# 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) ⇒ Object


31
32
33
34
35
36
37
38
# 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

#keysObject

Public


66
67
68
# File 'lib/mark_mapper/criteria_hash.rb', line 66

def keys
  @source.keys
end

#merge(other) ⇒ Object

Public


81
82
83
# File 'lib/mark_mapper/criteria_hash.rb', line 81

def merge(other)
  self.class.new hash_merge(@source, other.source)
end

#merge!(other) ⇒ Object

Public


86
87
88
89
90
91
# 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) ⇒ Boolean

Returns:


104
105
106
# File 'lib/mark_mapper/criteria_hash.rb', line 104

def object_id?(key)
  object_ids.include?(key.to_sym)
end

#object_idsObject

Private


109
110
111
# File 'lib/mark_mapper/criteria_hash.rb', line 109

def object_ids
  @options[:object_ids] ||= []
end

#object_ids=(value) ⇒ Object

Private

Raises:

  • (ArgumentError)

114
115
116
117
# 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?Boolean

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

Returns:


98
99
100
101
102
# 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_hashObject

Public


76
77
78
# File 'lib/mark_mapper/criteria_hash.rb', line 76

def to_hash
  @source
end