123456789_123456789_123456789_123456789_123456789_

Class: Mongoid::Criteria::Queryable::Smash

Relationships & Source Files
Extension / Inclusion / Inheritance Descendants
Subclasses:
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
Instance Chain:
Inherits: Hash
  • Object
Defined in: lib/mongoid/criteria/queryable/smash.rb

Overview

This is a smart hash for use with options and selectors.

Class Attribute Summary

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

resizable?

Can the size of this object change?

Class Method Summary

::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

::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

.new(aliases = {}, serializers = {}, associations = {}, aliased_associations = {}) {|_self| ... } ⇒ Smash

Initialize the new selector.

Examples:

Initialize the new selector.

Queryable::Smash.new(aliases, serializers)

Parameters:

  • aliases (Hash) (defaults to: {})

    A hash of mappings from aliases to the actual field names in the database.

  • serializers (Hash) (defaults to: {})

    An optional hash of objects that are responsible for serializing values. The keys of the hash must be strings that match the field name, and the values must respond to #localized? and #evolve(object).

  • associations (Hash) (defaults to: {})

    An optional hash of names to association objects.

  • aliased_associations (Hash) (defaults to: {})

    An optional hash of mappings from aliases for associations to their actual field names in the database.

Yields:

  • (_self)

Yield Parameters:

  • _self (Smash)

    the object that the method was called on

[ GitHub ]

  
# File 'lib/mongoid/criteria/queryable/smash.rb', line 52

def initialize(aliases = {}, serializers = {}, associations = {}, aliased_associations = {})
  @aliases = aliases
  @serializers = serializers
  @associations = associations
  @aliased_associations = aliased_associations
  yield(self) if block_given?
end

Instance Attribute Details

#aliased_associations (readonly)

[ GitHub ]

  
# File 'lib/mongoid/criteria/queryable/smash.rb', line 21

attr_reader :aliased_associations

#aliased_associations The aliased_associations.(The aliased_associations.) (readonly)

[ GitHub ]

  
# File 'lib/mongoid/criteria/queryable/smash.rb', line 21

attr_reader :aliased_associations

#aliases (readonly)

[ GitHub ]

  
# File 'lib/mongoid/criteria/queryable/smash.rb', line 12

attr_reader :aliases

#aliases The aliases.(The aliases.) (readonly)

[ GitHub ]

  
# File 'lib/mongoid/criteria/queryable/smash.rb', line 12

attr_reader :aliases

#associations (readonly)

[ GitHub ]

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

attr_reader :associations

#associations The associations.(The associations.) (readonly)

[ GitHub ]

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

attr_reader :associations

#serializers (readonly)

[ GitHub ]

  
# File 'lib/mongoid/criteria/queryable/smash.rb', line 15

attr_reader :serializers

#serializers The serializers.(The serializers.) (readonly)

[ GitHub ]

  
# File 'lib/mongoid/criteria/queryable/smash.rb', line 15

attr_reader :serializers

Instance Method Details

#[](key) ⇒ Object

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

Examples:

Get an item by the key.

smash["test"]

Parameters:

Returns:

  • (Object)

    The found object.

[ GitHub ]

  
# File 'lib/mongoid/criteria/queryable/smash.rb', line 68

def [](key)
  fetch(aliases[key]) { super }
end

#__deep_copy__Smash

Perform a deep copy of the smash.

Examples:

Perform a deep copy.

smash.__deep_copy__

Returns:

  • (Smash)

    The copied hash.

[ GitHub ]

  
# File 'lib/mongoid/criteria/queryable/smash.rb', line 29

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

#get_serializer(name) ⇒ Object (private)

Retrieves the serializer for the given name. If the name exists in the serializers hash then return that immediately, otherwise recursively look through the associations and find the appropriate field.

Parameters:

  • name (String)

    The name of the db field.

Returns:

  • (Object)

    The serializer.

[ GitHub ]

  
# File 'lib/mongoid/criteria/queryable/smash.rb', line 119

def get_serializer(name)
  if s = serializers[name]
    s
  else
    Fields.traverse_association_tree(name, serializers, associations, aliased_associations)
  end
end

#localized_key(name, serializer) ⇒ String (private)

This method is for internal use only.

Get the localized value for the key if needed. If the field uses localization the current locale will be appended to the key in MongoDB dot notation.

Examples:

Get the normalized key name.

smash.localized_key("field", serializer)

Parameters:

  • name (String)

    The name of the field.

  • serializer (Object)

    The optional field serializer.

Returns:

  • (String)

    The normalized key.

[ GitHub ]

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

def localized_key(name, serializer)
  serializer && serializer.localized? ? "#{name}.#{::I18n.locale}" : name
end

#storage_pair(key) ⇒ Array<String, Object> (private)

This method is for internal use only.

Get the pair of objects needed to store the value in a hash by the provided key. This is the database field name and the serializer.

Examples:

Get the name and serializer.

smash.storage_pair("id")

Parameters:

  • key (Symbol | String)

    The key provided to the selection.

Returns:

  • (Array<String, Object>)

    The name of the db field and serializer.

[ GitHub ]

  
# File 'lib/mongoid/criteria/queryable/smash.rb', line 103

def storage_pair(key)
  field = key.to_s
  name = Fields.database_field_name(field, associations, aliases, aliased_associations)
  [ name, get_serializer(name) ]
end