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
#aliased_associations, #aliased_associations The aliased_associations., #aliases, #aliases The aliases., #associations, #associations The associations., #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 33 34 35 36 37 38 39 |
# 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| case _key.to_s 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 |
#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.
51 52 53 54 55 56 57 58 59 60 |
# File 'lib/mongoid/criteria/queryable/selector.rb', line 51 def store(key, value) name, serializer = storage_pair(key) if multi_selection?(name) store_name = name store_value = evolve_multi(value) else store_name, store_value = store_creds(name, serializer, value) end super(store_name, store_value) end |
#to_pipeline ⇒ Array<Hash>
Convert the selector to an aggregation pipeline entry.
69 70 71 72 73 |
# File 'lib/mongoid/criteria/queryable/selector.rb', line 69 def to_pipeline pipeline = [] pipeline.push({ "$match" => self }) unless empty? pipeline end |