Class: Plucky::Normalizers::CriteriaHashValue
- 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.
[:$or, :$and, :$nor]
Instance Method Summary collapse
-
#call(parent_key, key, value) ⇒ Object
Public: Returns value normalized for Mongo.
-
#criteria_hash_class ⇒ Object
Private: Returns class of provided criteria hash.
-
#initialize(criteria_hash) ⇒ CriteriaHashValue
constructor
A new instance of CriteriaHashValue.
- #modifier?(key) ⇒ Boolean
-
#nesting_operator?(key) ⇒ Boolean
Private: Returns true or false if key is a nesting operator.
-
#object_id?(key) ⇒ Boolean
Private: Returns true or false if key should be converted to object id.
-
#options ⇒ Object
Private: Returns options of provided criteria hash.
-
#to_object_id(value) ⇒ Object
Private: Ensures value is object id if possible.
Constructor Details
#initialize(criteria_hash) ⇒ CriteriaHashValue
Returns a new instance of CriteriaHashValue.
9 10 11 |
# File 'lib/plucky/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 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
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 45 46 47 48 49 50 51 |
# File 'lib/plucky/normalizers/criteria_hash_value.rb', line 20 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, ).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_class ⇒ Object
Private: Returns class of provided criteria hash
59 60 61 |
# File 'lib/plucky/normalizers/criteria_hash_value.rb', line 59 def criteria_hash_class @criteria_hash.class end |
#modifier?(key) ⇒ Boolean
78 79 80 |
# File 'lib/plucky/normalizers/criteria_hash_value.rb', line 78 def modifier?(key) Plucky.modifier?(key) end |
#nesting_operator?(key) ⇒ Boolean
Private: Returns true or false if key is a nesting operator
74 75 76 |
# File 'lib/plucky/normalizers/criteria_hash_value.rb', line 74 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
69 70 71 |
# File 'lib/plucky/normalizers/criteria_hash_value.rb', line 69 def object_id?(key) @criteria_hash.object_id?(key) end |
#options ⇒ Object
Private: Returns options of provided criteria hash
64 65 66 |
# File 'lib/plucky/normalizers/criteria_hash_value.rb', line 64 def @criteria_hash. end |
#to_object_id(value) ⇒ Object
Private: Ensures value is object id if possible
54 55 56 |
# File 'lib/plucky/normalizers/criteria_hash_value.rb', line 54 def to_object_id(value) Plucky.to_object_id(value) end |