Module: Mongoid::Criteria::Queryable::Extensions::String

Defined in:
lib/mongoid/criteria/queryable/extensions/string.rb

Overview

Adds query type-casting behavior to String class.

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#__evolve_date__Time

Evolve the string into a mongodb friendly date.

Examples:

Evolve the string.

"2012-1-1".__evolve_date__

Returns:

  • (Time)

    The time at UTC midnight.



18
19
20
21
# File 'lib/mongoid/criteria/queryable/extensions/string.rb', line 18

def __evolve_date__
  time = ::Time.parse(self)
  ::Time.utc(time.year, time.month, time.day, 0, 0, 0, 0)
end

#__evolve_time__Time

Evolve the string into a mongodb friendly time.

Examples:

Evolve the string.

"2012-1-1".__evolve_time__

Returns:

  • (Time)

    The string as a time.



29
30
31
# File 'lib/mongoid/criteria/queryable/extensions/string.rb', line 29

def __evolve_time__
  __mongoize_time__.utc
end

#__expr_part__(value, negating = false) ⇒ Hash

Get the string as a specification.

Examples:

Get the string as a criteria.

"field".__expr_part__(value)

Parameters:

  • value (Object)

    The value of the criteria.

  • negating (true | false) (defaults to: false)

    If the selection should be negated.

Returns:

  • (Hash)

    The selection.



67
68
69
# File 'lib/mongoid/criteria/queryable/extensions/string.rb', line 67

def __expr_part__(value, negating = false)
  ::String.__expr_part__(self, value, negating)
end

#__mongo_expression__String

Get the string as a mongo expression, adding $ to the front.

Examples:

Get the string as an expression.

"test".__mongo_expression__

Returns:

  • (String)

    The string with $ at the front.



39
40
41
# File 'lib/mongoid/criteria/queryable/extensions/string.rb', line 39

def __mongo_expression__
  start_with?("$") ? self : "$#{self}"
end

#__sort_option__Hash

Get the string as a sort option.

Examples:

Get the string as a sort option.

"field ASC".__sort_option__

Returns:

  • (Hash)

    The string as a sort option hash.



49
50
51
52
53
54
55
56
# File 'lib/mongoid/criteria/queryable/extensions/string.rb', line 49

def __sort_option__
  split(/,/).inject({}) do |hash, spec|
    hash.tap do |_hash|
      field, direction = spec.strip.split(/\s/)
      _hash[field.to_sym] = Mongoid::Criteria::Translator.to_direction(direction)
    end
  end
end