Class: Mongoid::Criterion::Selector
- 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
-
#fields ⇒ Object
readonly
Returns the value of attribute fields.
-
#klass ⇒ Object
readonly
Returns the value of attribute klass.
Instance Method Summary collapse
-
#[]=(key, value) ⇒ Object
Set the value for the supplied key, attempting to typecast the value.
-
#initialize(klass) ⇒ Selector
constructor
Create the new selector.
-
#inspect ⇒ String
Generate pretty inspection for old ruby versions.
-
#merge!(other) ⇒ Selector
(also: #update)
Merge the selector with another hash.
Methods included from Extensions::Hash::Scoping
Methods included from Extensions::Hash::CriteriaHelpers
#expand_complex_criteria, #extract_id
Constructor Details
#initialize(klass) ⇒ Selector
Create the new selector.
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
#fields ⇒ Object (readonly)
Returns the value of attribute fields.
9 10 11 |
# File 'lib/mongoid/criterion/selector.rb', line 9 def fields @fields end |
#klass ⇒ Object (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.
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 |
#inspect ⇒ String
Generate pretty inspection for old ruby versions.
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.
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 |