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.



10
11
12
# File 'lib/riddle/client/filter.rb', line 10

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

Instance Attribute Details

#attributeObject

Returns the value of attribute attribute.



6
7
8
# File 'lib/riddle/client/filter.rb', line 6

def attribute
  @attribute
end

#excludeObject

Returns the value of attribute exclude.



6
7
8
# File 'lib/riddle/client/filter.rb', line 6

def exclude
  @exclude
end

#valuesObject

Returns the value of attribute values.



6
7
8
# File 'lib/riddle/client/filter.rb', line 6

def values
  @values
end

Instance Method Details

#exclude?Boolean

Returns:

  • (Boolean)


14
15
16
# File 'lib/riddle/client/filter.rb', line 14

def exclude?
  self.exclude
end

#query_messageObject

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



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
44
45
# File 'lib/riddle/client/filter.rb', line 19

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