class Mongoid::Criteria::Queryable::Selector
coming into it as well as being alias and locale aware for key names.
The selector is a special kind of hash that knows how to serialize values
def evolve(serializer, value)
-
(Object)- The serialized object.
Parameters:
-
value(Object) -- The value to serialize. -
serializer(Object) -- The optional serializer for the field.
Other tags:
- Example: Evolve a simple selection. -
Other tags:
- Api: - private
def evolve(serializer, value) case value when Hash evolve_hash(serializer, value) when Array evolve_array(serializer, value) else (serializer || value.class).evolve(value) end end
def evolve_array(serializer, value)
-
(Object)- The serialized array.
Parameters:
-
value(Array) -- The array to serialize. -
serializer(Object) -- The optional serializer for the field.
Other tags:
- Example: Evolve a simple selection. -
Other tags:
- Api: - private
def evolve_array(serializer, value) value.map do |_value| evolve(serializer, _value) end end
def evolve_hash(serializer, value)
-
(Object)- The serialized hash.
Parameters:
-
value(Hash) -- The hash to serialize. -
serializer(Object) -- The optional serializer for the field.
Other tags:
- Example: Evolve a simple selection. -
Other tags:
- Api: - private
def evolve_hash(serializer, value) value.each_pair do |operator, _value| if operator =~ /exists|type|size/ value[operator] = _value else value[operator] = evolve(serializer, _value) end end end
def evolve_multi(specs)
- Api: - private
Returns:
-
(Array- The serialized values.)
Parameters:
-
specs(Array) -- The multi-selection.
Other tags:
- Example: Evolve the multi-selection. -
def evolve_multi(specs) unless specs.is_a?(Array) raise ArgumentError, "specs is not an array: #{specs.inspect}" end specs.map do |spec| Hash[spec.map do |key, value| # If an application nests conditionals, e.g. # {'$or' => [{'$or' => {...}}]}, # when evolve_multi is called for the top level hash, # this call recursively transforms the bottom level $or. if multi_selection?(key) value = evolve_multi(value) end # storage_pair handles field aliases but not localization for # some reason, although per its documentation Smash supposedly # owns both. name, serializer = storage_pair(key) final_key = localized_key(name, serializer) # This performs type conversions on the value and transformations # that depend on the type of the field that the value is stored # in, but not transformations that have to do with query shape. evolved_value = evolve(serializer, value) # This builds a query shape around the value, when the query # involves complex keys. For example, {:foo.lt => 5} produces # {'foo' => {'$lt' => 5}}. This step should be done after all # value-based processing is complete. if key.is_a?(Key) evolved_value = key.transform_value(evolved_value) end [ final_key, evolved_value ] end] end.uniq end
def merge!(other)
-
(Selector)- The selector.
Parameters:
-
other(Hash, Selector) -- The object to merge in.
Other tags:
- Example: Merge in another selector. -
def merge!(other) other.each_pair do |key, value| if value.is_a?(Hash) && self[key.to_s].is_a?(Hash) value = self[key.to_s].merge(value) do |_key, old_val, new_val| case _key when '$in' new_val & old_val when '$nin' (old_val + new_val).uniq else new_val end end end if multi_selection?(key) value = (self[key.to_s] || []).concat(value) end store(key, value) end end
def multi_selection?(key)
-
(true, false)- If the key is for a multi-select.
Parameters:
-
key(String) -- The key to check.
Other tags:
- Example: Is the selection a multi-select? -
Other tags:
- Api: - private
def multi_selection?(key) %w($and $or $nor).include?(key) end
def store(key, value)
-
(Object)- The stored object.
Parameters:
-
value(Object) -- The value to add. -
key(String, Symbol) -- The name of the attribute.
Other tags:
- Example: Store a value in the selector. -
def store(key, value) name, serializer = storage_pair(key) if multi_selection?(name) store_name = name store_value = evolve_multi(value) else store_name = localized_key(name, serializer) store_value = evolve(serializer, value) end super(store_name, store_value) end
def to_pipeline
-
(Array- The pipeline entry for the selector.)
Other tags:
- Example: Convert the selector to a pipeline. -
def to_pipeline pipeline = [] pipeline.push({ "$match" => self }) unless empty? pipeline end