Class: Plucky::Normalizers::CriteriaHashValue

Inherits:
Object
  • Object
show all
Defined in:
lib/plucky/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.

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

Instance Method Summary collapse

Constructor Details

#initialize(criteria_hash) ⇒ CriteriaHashValue

Returns a new instance of CriteriaHashValue.



11
12
13
# File 'lib/plucky/normalizers/criteria_hash_value.rb', line 11

def initialize(criteria_hash)
  @criteria_hash = criteria_hash
end

Instance Method Details

#call(parent_key, key, value) ⇒ Object

Public: Returns value normalized for Mongo

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 Mongo



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/plucky/normalizers/criteria_hash_value.rb', line 22

def call(parent_key, key, value)
  case value
    when Array, Set
      if object_id?(parent_key)
        value = value.map { |v| to_object_id(v) }
      end

      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
        {:$in => 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
      if object_id?(key)
        return to_object_id(value)
      end
      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



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

def criteria_hash_class
  @criteria_hash.class
end

#modifier?(key) ⇒ Boolean

Returns:

  • (Boolean)


80
81
82
# File 'lib/plucky/normalizers/criteria_hash_value.rb', line 80

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

#nesting_operator?(key) ⇒ Boolean

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

Returns:

  • (Boolean)


76
77
78
# File 'lib/plucky/normalizers/criteria_hash_value.rb', line 76

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

#object_id?(key) ⇒ Boolean

Private: Returns true or false if key should be converted to object id

Returns:

  • (Boolean)


71
72
73
# File 'lib/plucky/normalizers/criteria_hash_value.rb', line 71

def object_id?(key)
  @criteria_hash.object_id?(key)
end

#optionsObject

Private: Returns options of provided criteria hash



66
67
68
# File 'lib/plucky/normalizers/criteria_hash_value.rb', line 66

def options
  @criteria_hash.options
end

#to_object_id(value) ⇒ Object

Private: Ensures value is object id if possible



56
57
58
# File 'lib/plucky/normalizers/criteria_hash_value.rb', line 56

def to_object_id(value)
  Plucky.to_object_id(value)
end