Class: Mongoid::Criterion::Selector

Inherits:
Hash show all
Defined in:
lib/mongoid/criterion/selector.rb

Overview

The selector is a hash-like object that has special behaviour for merging mongoid criteria selectors.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Extensions::Hash::Scoping

#as_conditions

Methods included from Extensions::Hash::CriteriaHelpers

#expand_complex_criteria, #extract_id

Methods included from Extensions::Hash::DeepCopy

#_deep_copy

Constructor Details

#initialize(klass) ⇒ Selector

Create the new selector.

Examples:

Create the selector.

Selector.new(Person)

Parameters:

  • klass (Class)

    The class the selector is for.

Since:

  • 1.0.0



19
20
21
22
# File 'lib/mongoid/criterion/selector.rb', line 19

def initialize(klass)
  @aliased_fields, @fields, @klass =
    klass.aliased_fields, klass.fields.except("_id", "_type"), klass
end

Instance Attribute Details

#aliased_fieldsObject (readonly)

Returns the value of attribute aliased_fields.



9
10
11
# File 'lib/mongoid/criterion/selector.rb', line 9

def aliased_fields
  @aliased_fields
end

#fieldsObject (readonly)

Returns the value of attribute fields.



9
10
11
# File 'lib/mongoid/criterion/selector.rb', line 9

def fields
  @fields
end

#klassObject (readonly)

Returns the value of attribute klass.



9
10
11
# File 'lib/mongoid/criterion/selector.rb', line 9

def klass
  @klass
end

Instance Method Details

#[]=(key, value) ⇒ Object

Set the value for the supplied key, attempting to typecast the value.

Examples:

Set the value for the key.

selector["$ne"] = { :name => "Zorg" }

Parameters:

Since:

  • 2.0.0



33
34
35
36
# File 'lib/mongoid/criterion/selector.rb', line 33

def []=(key, value)
  key = "#{key}.#{::I18n.locale}" if klass.fields[key.to_s].try(:localized?)
  super(key, try_to_typecast(key, value))
end

#inspectString

Generate pretty inspection for old ruby versions.

Examples:

Inspect the selector.

selector.inspect

Returns:

  • (String)

    The inspected selector.



65
66
67
68
69
70
# File 'lib/mongoid/criterion/selector.rb', line 65

def inspect
  ret = self.keys.inject([]) do |ret, key|
    ret << "#{key.inspect}=>#{self[key].inspect}"
  end
  "{#{ret.sort.join(', ')}}"
end

#merge!(other) ⇒ Selector Also known as: update

Merge the selector with another hash.

Examples:

Merge the objects.

selector.merge!({ :key => "value" })

Parameters:

Returns:

Since:

  • 1.0.0



48
49
50
51
52
53
54
# File 'lib/mongoid/criterion/selector.rb', line 48

def merge!(other)
  tap do |selector|
    other.each_pair do |key, value|
      selector[key] = value
    end
  end
end