Module: Relix::Index::Ordering

Defined in:
lib/relix/index.rb

Instance Method Summary collapse

Instance Method Details

#initialize(*args) ⇒ Object



98
99
100
101
# File 'lib/relix/index.rb', line 98

def initialize(*args)
  super
  @order = @options[:order]
end

#range_from_options(r, options, value = nil) ⇒ Object



130
131
132
133
134
135
136
137
# File 'lib/relix/index.rb', line 130

def range_from_options(r, options, value=nil)
  start = (options[:offset] || 0)
  if f = options[:from]
    start = (position(r, f, value) + 1)
  end
  stop = (options[:limit] ? (start + options[:limit] - 1) : -1)
  [start, stop]
end

#score(object, value) ⇒ Object



103
104
105
106
107
108
109
# File 'lib/relix/index.rb', line 103

def score(object, value)
  if @order
    value = object.send(@order)
  end

  score_for_value(value)
end

#score_for_value(value) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/relix/index.rb', line 111

def score_for_value(value)
  case value
  when Numeric
    value
  when Time
    value.to_f
  else
    if value.respond_to?(:to_i)
      value.to_i
    elsif value.respond_to?(:to_time)
      value.to_time.to_f
    elsif @order
      raise UnorderableValueError.new("Unable to convert #{value} in to a number for ordering.")
    else
      0
    end
  end
end