Class: Riddle::Client::Filter

Inherits:
Object
  • Object
show all
Defined in:
lib/riddle/client/filter.rb,
lib/riddle/0.9.9/client/filter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attribute, values, exclude = false) ⇒ Filter

Attribute name, values (which can be an array or a range), and whether the filter should be exclusive.


8
9
10
# File 'lib/riddle/client/filter.rb', line 8

def initialize(attribute, values, exclude=false)
  @attribute, @values, @exclude = attribute, values, exclude
end

Instance Attribute Details

#attributeObject

Returns the value of attribute attribute


4
5
6
# File 'lib/riddle/client/filter.rb', line 4

def attribute
  @attribute
end

#excludeObject

Returns the value of attribute exclude


4
5
6
# File 'lib/riddle/client/filter.rb', line 4

def exclude
  @exclude
end

#valuesObject

Returns the value of attribute values


4
5
6
# File 'lib/riddle/client/filter.rb', line 4

def values
  @values
end

Instance Method Details

#exclude?Boolean

Returns:

  • (Boolean)

12
13
14
# File 'lib/riddle/client/filter.rb', line 12

def exclude?
  self.exclude
end

#query_messageObject

Returns the message for this filter to send to the Sphinx service


17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/riddle/client/filter.rb', line 17

def query_message
  message = Message.new

  message.append_string self.attribute.to_s
  case self.values
  when Range
    if self.values.first.is_a?(Float) && self.values.last.is_a?(Float)
      message.append_int FilterTypes[:float_range]
      message.append_floats self.values.first, self.values.last
    else
      message.append_int FilterTypes[:range]
      append_integer_range message, self.values
    end
  when Array
    if self.values.first.is_a?(Float) && self.values.length == 1
      message.append_int FilterTypes[:float_range]
      message.append_floats self.values.first, self.values.first
    else
      message.append_int FilterTypes[:values]
      message.append_int self.values.length
      append_array message, self.values
    end
  end
  message.append_int self.exclude? ? 1 : 0

  message.to_s
end