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

#scoped

Methods included from Extensions::Hash::CriteriaHelpers

#expand_complex_criteria, #extract_id

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
# File 'lib/mongoid/criterion/selector.rb', line 19

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

Instance Attribute Details

#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



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

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.



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

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



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

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