Class: MarkMapper::Normalizers::CriteriaHashValue

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

Constant Summary collapse

NestingOperators =

Internal: Used by normalized_value to determine if we need to run the value through another criteria hash to normalize it.

[:$or, :$and, :$nor]

Instance Method Summary collapse

Constructor Details

#initialize(criteria_hash) ⇒ CriteriaHashValue

Returns a new instance of CriteriaHashValue.


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

def initialize(criteria_hash)
  @criteria_hash = criteria_hash
end

Instance Method Details

#call(parent_key, key, value) ⇒ Object

Public: Returns value normalized for MarkLogic

parent_key - The parent key if nested, otherwise same as key key - The key we are currently normalizing value - The value that should be normalized

Returns value normalized for MarkLogic


20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/mark_mapper/normalizers/criteria_hash_value.rb', line 20

def call(parent_key, key, value)
  case value
    when Array, Set
      if nesting_operator?(key)
        value.map  { |v| criteria_hash_class.new(v, options).to_hash }
      elsif parent_key == key && !modifier?(key) && !value.empty?
        # we're not nested and not the value for a symbol operator
        {:$eq => value.to_a}
      else
        # we are a value for a symbol operator or nested hash
        value.to_a
      end
    when Time
      value.utc
    when String
      value
    when Hash
      value.each { |k, v| value[k] = call(key, k, v) }
      value
    when Regexp
      Regexp.new(value)
    else
      value
  end
end

#criteria_hash_classObject

Private: Returns class of provided criteria hash


47
48
49
# File 'lib/mark_mapper/normalizers/criteria_hash_value.rb', line 47

def criteria_hash_class
  @criteria_hash.class
end

#modifier?(key) ⇒ Boolean

Returns:


61
62
63
# File 'lib/mark_mapper/normalizers/criteria_hash_value.rb', line 61

def modifier?(key)
  MarkMapper.modifier?(key)
end

#nesting_operator?(key) ⇒ Boolean

Private: Returns true or false if key is a nesting operator

Returns:


57
58
59
# File 'lib/mark_mapper/normalizers/criteria_hash_value.rb', line 57

def nesting_operator?(key)
  NestingOperators.include?(key)
end

#optionsObject

Private: Returns options of provided criteria hash


52
53
54
# File 'lib/mark_mapper/normalizers/criteria_hash_value.rb', line 52

def options
  @criteria_hash.options
end