Class: Riddle::Client::Filter

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

Overview

Used for querying Sphinx.

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.



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

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

Instance Attribute Details

#attributeObject

Returns the value of attribute attribute.



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

def attribute
  @attribute
end

#excludeObject

Returns the value of attribute exclude.



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

def exclude
  @exclude
end

#valuesObject

Returns the value of attribute values.



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

def values
  @values
end

Instance Method Details

#exclude?Boolean

Returns:

  • (Boolean)


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

def exclude?
  self.exclude
end

#query_messageObject

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



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
44
45
46
47
48
49
50
# File 'lib/riddle/client/filter.rb', line 18

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]
      message.append_ints self.values.first, self.values.last
    end
  when Array
    message.append_int FilterTypes[:values]
    message.append_int self.values.length
    # using to_f is a hack from the php client - to workaround 32bit
    # signed ints on x32 platforms
    message.append_ints *self.values.collect { |val|
      case val
      when TrueClass
        1.0
      when FalseClass
        0.0
      else
        val.to_f
      end
    }
  end
  message.append_int self.exclude? ? 1 : 0
  
  message.to_s
end