Class: Mongoid::Criteria::Queryable::Selector
- Defined in:
- lib/mongoid/criteria/queryable/selector.rb
Overview
The selector is a special kind of hash that knows how to serialize values coming into it as well as being alias and locale aware for key names.
Instance Attribute Summary
Attributes inherited from Smash
#aliases, #aliases The aliases., #serializers, #serializers The serializers.
Instance Method Summary collapse
-
#merge!(other) ⇒ Selector
Merges another selector into this one.
-
#store(key, value) ⇒ Object
(also: #[]=)
Store the value in the selector for the provided key.
-
#to_pipeline ⇒ Array<Hash>
Convert the selector to an aggregation pipeline entry.
Methods inherited from Smash
#[], #__deep_copy__, #initialize
Constructor Details
This class inherits a constructor from Mongoid::Criteria::Queryable::Smash
Instance Method Details
#merge!(other) ⇒ Selector
Merges another selector into this one.
20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/mongoid/criteria/queryable/selector.rb', line 20 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| _key == '$in' ? new_val & old_val : old_val | new_val end end if multi_selection?(key) value = (self[key.to_s] || []).concat(value) end store(key, value) end end |
#store(key, value) ⇒ Object Also known as: []=
Store the value in the selector for the provided key. The selector will handle all necessary serialization and localization in this step.
46 47 48 49 50 51 52 53 |
# File 'lib/mongoid/criteria/queryable/selector.rb', line 46 def store(key, value) name, serializer = storage_pair(key) if multi_selection?(name) super(name, evolve_multi(value)) else super(localized_key(name, serializer), evolve(serializer, value)) end end |
#to_pipeline ⇒ Array<Hash>
Convert the selector to an aggregation pipeline entry.
64 65 66 67 68 |
# File 'lib/mongoid/criteria/queryable/selector.rb', line 64 def to_pipeline pipeline = [] pipeline.push({ "$match" => self }) unless empty? pipeline end |