Class: String

Inherits:
Object show all
Defined in:
lib/ultrasphinx/core_extensions.rb

Overview

Filter type coercion methods

Instance Method Summary collapse

Instance Method Details

#_interpolate(*values) ⇒ Object

Interpolate SQL field names into functions



110
111
112
113
114
115
116
# File 'lib/ultrasphinx/core_extensions.rb', line 110

def _interpolate(*values)
  if values.size == 1
    self.gsub('?', values.first)
  else
    values.inject(self) { |result, value| result.sub('?', value.to_s) }
  end
end

#_to_numericObject

XXX Not used enough to justify such a strange abstraction



95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/ultrasphinx/core_extensions.rb', line 95

def _to_numeric
  zeroless = self.squeeze(" ").strip.sub(/^0+(\d)/, '\1')
  zeroless.sub!(/(\...*?)0+$/, '\1')
  if zeroless.to_i.to_s == zeroless
    zeroless.to_i
  elsif zeroless.to_f.to_s == zeroless
    zeroless.to_f
  elsif date = Chronic.parse(self.gsub(/(\d)([^\d\:\s])/, '\1 \2')) # Improve Chronic's flexibility a little
    date.to_i
  else
    raise Ultrasphinx::UsageError, "#{self.inspect} could not be coerced into a numeric value"
  end
end