123456789_123456789_123456789_123456789_123456789_

Class: Mongoid::Criteria::Queryable::Options

Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
Instance Chain:
Inherits: Mongoid::Criteria::Queryable::Smash
Defined in: lib/mongoid/criteria/queryable/options.rb

Overview

The options is a hash representation of options passed to MongoDB queries, such as skip, limit, and sorting criteria.

Class Attribute Summary

::Mongoid::Extensions::Hash::ClassMethods - Extended

resizable?

Can the size of this object change?

Class Method Summary

Smash - Inherited

.new

Initialize the new selector.

::Mongoid::Extensions::Hash::ClassMethods - Extended

mongoize

Turn the object from the ruby type we deal with to a Mongo friendly type.

Instance Attribute Summary

Instance Method Summary

Smash - Inherited

#[]

Get an item from the smart hash by the provided key.

#__deep_copy__

Perform a deep copy of the smash.

#get_serializer

Retrieves the serializer for the given name.

#localized_key

Get the localized value for the key if needed.

#storage_pair

Get the pair of objects needed to store the value in a hash by the provided key.

::Mongoid::Extensions::Hash - Included

#__consolidate__

Consolidate the key/values in the hash under an atomic $set.

#__evolve_object_id__

Evolves each value in the hash to an object id if it is convertable.

#__mongoize_object_id__

Mongoizes each value in the hash to an object id if it is convertable.

#delete_id

Deletes an id value from the hash.

#extract_id

Get the id attribute from this hash, whether it’s prefixed with an underscore or is a symbol.

#mongoize

Turn the object from the ruby type we deal with to a Mongo friendly type.

#to_criteria

Convert this hash to a criteria.

Constructor Details

This class inherits a constructor from Mongoid::Criteria::Queryable::Smash

Instance Method Details

#[]=(key, value, localize = true)

Alias for #store.

[ GitHub ]

  
# File 'lib/mongoid/criteria/queryable/options.rb', line 65

alias :[]= :store

#__deep_copy__Options

Perform a deep copy of the options.

Examples:

Perform a deep copy.

options.__deep_copy__

Returns:

  • (Options)

    The copied options.

[ GitHub ]

  
# File 'lib/mongoid/criteria/queryable/options.rb', line 87

def __deep_copy__
  self.class.new(aliases, serializers, associations, aliased_associations) do |copy|
    each_pair do |key, value|
      copy.merge!(key => value.__deep_copy__)
    end
  end
end

#evolve(value, localize = true) ⇒ Object (private)

This method is for internal use only.

Evolve a single key selection with various types of values.

Examples:

Evolve a simple selection.

options.evolve(field, 5)

Parameters:

  • value (Object)

    The value to serialize.

Returns:

  • (Object)

    The serialized object.

[ GitHub ]

  
# File 'lib/mongoid/criteria/queryable/options.rb', line 107

def evolve(value, localize = true)
  case value
  when Hash
    evolve_hash(value, localize)
  else
    value
  end
end

#evolve_hash(value, localize = true) ⇒ Object (private)

This method is for internal use only.

Evolve a single key selection with hash values.

Examples:

Evolve a simple selection.

options.evolve(field, { "$gt" => 5 })

Parameters:

  • value (Hash)

    The hash to serialize.

Returns:

  • (Object)

    The serialized hash.

[ GitHub ]

  
# File 'lib/mongoid/criteria/queryable/options.rb', line 126

def evolve_hash(value, localize = true)
  value.inject({}) do |hash, (field, _value)|
    name, serializer = storage_pair(field)
    name = localized_key(name, serializer) if localize
    hash[name] = _value
    hash
  end
end

#fieldsHash

Convenience method for getting the field options.

Examples:

Get the fields options.

options.fields

Returns:

  • (Hash)

    The fields options.

[ GitHub ]

  
# File 'lib/mongoid/criteria/queryable/options.rb', line 18

def fields
  self[:fields]
end

#limitInteger

Convenience method for getting the limit option.

Examples:

Get the limit option.

options.limit

Returns:

[ GitHub ]

  
# File 'lib/mongoid/criteria/queryable/options.rb', line 28

def limit
  self[:limit]
end

#skipInteger

Convenience method for getting the skip option.

Examples:

Get the skip option.

options.skip

Returns:

[ GitHub ]

  
# File 'lib/mongoid/criteria/queryable/options.rb', line 38

def skip
  self[:skip]
end

#sortHash

Convenience method for getting the sort options.

Examples:

Get the sort options.

options.sort

Returns:

  • (Hash)

    The sort options.

[ GitHub ]

  
# File 'lib/mongoid/criteria/queryable/options.rb', line 48

def sort
  self[:sort]
end

#store(key, value, localize = true) ⇒ Object Also known as: #[]=

Store the value in the options for the provided key. The options will handle all necessary serialization and localization in this step.

Examples:

Store a value in the options.

options.store(:key, "testing")

Parameters:

  • key (String | Symbol)

    The name of the attribute.

  • value (Object)

    The value to add.

Returns:

  • (Object)

    The stored object.

[ GitHub ]

  
# File 'lib/mongoid/criteria/queryable/options.rb', line 62

def store(key, value, localize = true)
  super(key, evolve(value, localize))
end

#to_pipelineArray<Hash>

Convert the options to aggregation pipeline friendly options.

Examples:

Convert the options to a pipeline.

options.to_pipeline

Returns:

  • (Array<Hash>)

    The options in pipeline form.

[ GitHub ]

  
# File 'lib/mongoid/criteria/queryable/options.rb', line 73

def to_pipeline
  pipeline = []
  pipeline.push({ "$skip" => skip }) if skip
  pipeline.push({ "$limit" => limit }) if limit
  pipeline.push({ "$sort" => sort }) if sort
  pipeline
end