Class: Origin::Selector
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 Origin::Smash
Instance Method Details
#merge!(other) ⇒ Selector
Merges another selector into this one.
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/origin/selector.rb', line 18 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| if in?(_key) new_val & old_val elsif nin?(_key) (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.
50 51 52 53 54 55 56 57 |
# File 'lib/origin/selector.rb', line 50 def store(key, value) name, serializer = storage_pair(key) if multi_selection?(name) super(name, evolve_multi(value)) else super(normalized_key(name, serializer), evolve(serializer, value)) end end |
#to_pipeline ⇒ Array<Hash>
Convert the selector to an aggregation pipeline entry.
68 69 70 71 72 |
# File 'lib/origin/selector.rb', line 68 def to_pipeline pipeline = [] pipeline.push({ "$match" => self }) unless empty? pipeline end |