Class: Origin::Selector

Inherits:
Smash
  • Object
show all
Defined in:
lib/origin/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

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.

Examples:

Merge in another selector.

selector.merge!(name: "test")

Parameters:

  • other (Hash, Selector)

    The object to merge in.

Returns:

Since:

  • 1.0.0



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.

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.

Since:

  • 1.0.0



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_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.

Since:

  • 2.0.0



68
69
70
71
72
# File 'lib/origin/selector.rb', line 68

def to_pipeline
  pipeline = []
  pipeline.push({ "$match" => self }) unless empty?
  pipeline
end