Module: Mongoid::Criteria::Queryable::Extensions::Hash

Defined in:
build/mongoid-7.0/lib/mongoid/criteria/queryable/extensions/hash.rb

Overview

This module contains additional hash behaviour.

Instance Method Summary collapse

Instance Method Details

#__add__(object) ⇒ Hash

Add an object to a hash using the merge strategies.

Examples:

Add an object to a hash.

{ field: value }.__add__({ field: other_value })

Parameters:

  • object (Hash)

    The other hash to add.

Returns:

  • (Hash)

    The hash with object added.

Since:

  • 1.0.0



20
21
22
# File 'build/mongoid-7.0/lib/mongoid/criteria/queryable/extensions/hash.rb', line 20

def __add__(object)
  apply_strategy(:__add__, object)
end

#__add_from_array__(array) ⇒ Hash

Merge this hash into the provided array.

Examples:

Merge the hash into the array.

{ field: value }.__add_from_array__([ 1, 2 ])

Parameters:

  • array (Array)

    The array to add to.

Returns:

  • (Hash)

    The merged hash.

Since:

  • 1.0.0



34
35
36
# File 'build/mongoid-7.0/lib/mongoid/criteria/queryable/extensions/hash.rb', line 34

def __add_from_array__(array)
  { keys.first => array.__add__(values.first) }
end

#__deep_copy__Hash

Make a deep copy of this hash.

Examples:

Make a deep copy of the hash.

{ field: value }.__deep_copy__

Returns:

  • (Hash)

    The copied hash.

Since:

  • 1.0.0



116
117
118
119
120
121
122
# File 'build/mongoid-7.0/lib/mongoid/criteria/queryable/extensions/hash.rb', line 116

def __deep_copy__
  {}.tap do |copy|
    each_pair do |key, value|
      copy.store(key, value.__deep_copy__)
    end
  end
end

#__expand_complex__Hash

Get the object as expanded.

Examples:

Get the object expanded.

obj.__expand_complex__

Returns:

  • (Hash)

    The expanded hash.

Since:

  • 1.0.5



148
149
150
151
152
153
154
# File 'build/mongoid-7.0/lib/mongoid/criteria/queryable/extensions/hash.rb', line 148

def __expand_complex__
  replacement = {}
  each_pair do |key, value|
    replacement.merge!(key.__expr_part__(value.__expand_complex__))
  end
  replacement
end

#__intersect__(object) ⇒ Hash

Add an object to a hash using the merge strategies.

Examples:

Add an object to a hash.

{ field: value }.__intersect__({ field: other_value })

Parameters:

  • object (Hash)

    The other hash to intersect.

Returns:

  • (Hash)

    The hash with object intersected.

Since:

  • 1.0.0



48
49
50
# File 'build/mongoid-7.0/lib/mongoid/criteria/queryable/extensions/hash.rb', line 48

def __intersect__(object)
  apply_strategy(:__intersect__, object)
end

#__intersect_from_array__(array) ⇒ Hash

Merge this hash into the provided array.

Examples:

Merge the hash into the array.

{ field: value }.__intersect_from_array__([ 1, 2 ])

Parameters:

  • array (Array)

    The array to intersect to.

Returns:

  • (Hash)

    The merged hash.

Since:

  • 1.0.0



62
63
64
# File 'build/mongoid-7.0/lib/mongoid/criteria/queryable/extensions/hash.rb', line 62

def __intersect_from_array__(array)
  { keys.first => array.__intersect__(values.first) }
end

#__intersect_from_object__(object) ⇒ Hash

Merge this hash into the provided object.

Examples:

Merge the hash into the object.

{ field: value }.__intersect_from_object__([ 1, 2 ])

Parameters:

  • object (Object)

    The object to intersect to.

Returns:

  • (Hash)

    The merged hash.

Since:

  • 1.0.0



76
77
78
# File 'build/mongoid-7.0/lib/mongoid/criteria/queryable/extensions/hash.rb', line 76

def __intersect_from_object__(object)
  { keys.first => object.__intersect__(values.first) }
end

#__sort_option__Hash

Get the hash as a sort option.

Examples:

Get the hash as a sort option.

{ field: 1 }.__sort_option__

Returns:

  • (Hash)

    The hash as sort option.

Since:

  • 1.0.0



132
133
134
135
136
137
138
# File 'build/mongoid-7.0/lib/mongoid/criteria/queryable/extensions/hash.rb', line 132

def __sort_option__
  tap do |hash|
    hash.each_pair do |key, value|
      hash.store(key, value.to_direction)
    end
  end
end

#__union__(object) ⇒ Hash

Add an object to a hash using the merge strategies.

Examples:

Add an object to a hash.

{ field: value }.__union__({ field: other_value })

Parameters:

  • object (Hash)

    The other hash to union.

Returns:

  • (Hash)

    The hash with object unioned.

Since:

  • 1.0.0



90
91
92
# File 'build/mongoid-7.0/lib/mongoid/criteria/queryable/extensions/hash.rb', line 90

def __union__(object)
  apply_strategy(:__union__, object)
end

#__union_from_object__(object) ⇒ Hash

Merge this hash into the provided object.

Examples:

Merge the hash into the object.

{ field: value }.__union_from_object__([ 1, 2 ])

Parameters:

  • object (Object)

    The object to union to.

Returns:

  • (Hash)

    The merged hash.

Since:

  • 1.0.0



104
105
106
# File 'build/mongoid-7.0/lib/mongoid/criteria/queryable/extensions/hash.rb', line 104

def __union_from_object__(object)
  { keys.first => object.__union__(values.first) }
end

#update_values(&block) ⇒ Hash

Update all the values in the hash with the provided block.

Examples:

Update the values in place.

{ field: "1" }.update_values(&:to_i)

Parameters:

  • block (Proc)

    The block to execute on each value.

Returns:

  • (Hash)

    the hash.

Since:

  • 1.0.0



166
167
168
169
170
# File 'build/mongoid-7.0/lib/mongoid/criteria/queryable/extensions/hash.rb', line 166

def update_values(&block)
  each_pair do |key, value|
    store(key, block[value])
  end
end