Class: Mongoid::Criteria::Queryable::Selector

Inherits:
Smash
  • Object
show all
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

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.

Examples:

Merge in another selector.

selector.merge!(name: "test")

Parameters:

  • other (Hash | Selector)

    The object to merge in.

Returns:



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.

Examples:

Store a value in the selector.

selector.store(:key, "testing")

Parameters:

  • key (String | Symbol)

    The name of the attribute.

  • value (Object)

    The value to add.

Returns:

  • (Object)

    The stored object.



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_pipelineArray<Hash>

Convert the selector to an aggregation pipeline entry.

Examples:

Convert the selector to a pipeline.

selector.to_pipeline

Returns:

  • (Array<Hash>)

    The pipeline entry for the selector.



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