module Mongoid::Criteria::Queryable::Extensions::String

Adds query type-casting behavior to String class.

Public Instance Methods

__evolve_date__() click to toggle source

Evolve the string into a mongodb friendly date.

@example Evolve the string.

"2012-1-1".__evolve_date__

@return [ Time ] The time at UTC midnight.

# 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__() click to toggle source

Evolve the string into a mongodb friendly time.

@example Evolve the string.

"2012-1-1".__evolve_time__

@return [ Time ] The string as a time.

# File lib/mongoid/criteria/queryable/extensions/string.rb, line 29
def __evolve_time__
  __mongoize_time__.utc
end
__expr_part__(value, negating = false) click to toggle source

Get the string as a specification.

@example Get the string as a criteria.

"field".__expr_part__(value)

@param [ Object ] value The value of the criteria. @param [ true | false ] negating If the selection should be negated.

@return [ Hash ] The selection.

# 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__() click to toggle source

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

@example Get the string as an expression.

"test".__mongo_expression__

@return [ String ] The string with $ at the front.

# File lib/mongoid/criteria/queryable/extensions/string.rb, line 39
def __mongo_expression__
  start_with?("$") ? self : "$#{self}"
end
__sort_option__() click to toggle source

Get the string as a sort option.

@example Get the string as a sort option.

"field ASC".__sort_option__

@return [ Hash ] The string as a sort option hash.

# 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