Module: Origin::Extensions::String

Defined in:
lib/origin/extensions/string.rb

Overview

This module contains additional object behaviour.

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__

Since:

  • 1.0.0



16
17
18
19
# File 'lib/origin/extensions/string.rb', line 16

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__

Since:

  • 1.0.0



29
30
31
# File 'lib/origin/extensions/string.rb', line 29

def __evolve_time__
  ::Time.parse(self).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)

Since:

  • 1.0.0



73
74
75
# File 'lib/origin/extensions/string.rb', line 73

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__

Since:

  • 2.0.0



41
42
43
# File 'lib/origin/extensions/string.rb', line 41

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__

Since:

  • 1.0.0



53
54
55
56
57
58
59
60
# File 'lib/origin/extensions/string.rb', line 53

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

#to_directionInteger

Get the string as a sort direction.

Examples:

Get the string as a sort direction.

"1".to_direction

Since:

  • 1.0.0



85
86
87
# File 'lib/origin/extensions/string.rb', line 85

def to_direction
  self =~ /desc/i ? -1 : 1
end