public
Description: Ambition adapter for Sphinx
Clone URL: git://github.com/technicalpickles/ambitious-sphinx.git
100644 110 lines (96 sloc) 3.579 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
module Ambition #:nodoc:
  module Adapters #:nodoc:
    module AmbitiousSphinx #:nodoc:
      # Select is responsible for taking pure Ruby, and mangling it until it resembles
      # the syntax that Ultrasphinx[http://blog.evanweaver.com/files/doc/fauna/ultrasphinx/files/README.html] uses.
      class Select < Base
        
        # Handles method calls, like
        #
        # u.name
        def call(method)
          "#{method.to_s}:"
        end
 
        # Should we be supporting chained calls like:
        #
        # u.name.downcase
        #
        # ?
        #
        # I don't think Sphinx would be able to handle this.
        def chained_call(*methods)
          raise "Not implemented yet."
        end
 
        # Handles generating an Ultrasphinx query for code like:
        #
        # 'foo' && 'bar'
        def both(left, right)
          "#{quotify left} AND #{quotify right}"
        end
 
        # Handles generating an Ultrasphinx query for code like:
        #
        # 'foo' || 'bar'
        def either(left, right)
          "#{quotify left} OR #{quotify right}"
        end
 
        # Sphinx doesn't support equality.
        def ==(left, right)
          raise "Not applicable to sphinx."
        end
 
        # Sphinx doesn't support inequality.
        def not_equal(left, right)
          raise "Not applicable to sphinx."
        end
 
        # Handles generating an Ultrasphinx query for code like:
        #
        # u.name =~ 'bob'
        #
        # Some cavaets:
        # * left hand side _must_ be a field, like u.name
        # * right hand side _must not_ be a regular expression. Pattern matching is generally not
        # supported by full text search engines
        def =~(left, right)
          raise if right.is_a? Regexp
          "#{left}#{quotify right}"
        end
 
        # Handles generating an Ultrasphinx query for code like:
        #
        # u.name !~ 'bob'
        #
        # Some cavaets:
        # * left hand side _must_ be a field, like u.name
        # * right hand side _must not_ be a regular expression. Pattern matching is generally not
        # supported by full text search engines
        def not_regexp(left, right)
          # could be DRYer, but this is more readable than: "NOT #{self.=~(left,right)}"
          raise if right.is_a? Regexp
          "NOT #{left}#{quotify right}"
        end
 
        # Not supported by Sphinx. If you need this kind of comparison, you probably should be
        # using ambitious-activerecord instead.
        def <(left, right)
          raise "Not applicable to sphinx."
        end
 
        # Not supported by Sphinx. If you need this kind of comparison, you probably should be
        # using ambitious-activerecord instead.
        def >(left, right)
          raise "Not applicable to sphinx."
        end
 
        # Not supported by Sphinx. If you need this kind of comparison, you probably should be
        # using ambitious-activerecord instead.
        def >=(left, right)
          raise "Not applicable to sphinx."
        end
 
        # Not supported by Sphinx. If you need this kind of comparison, you probably should be
        # using ambitious-activerecord instead.
        def <=(left, right)
          raise "Not applicable to sphinx."
        end
 
        # Not supported by Sphinx. If you need this kind of comparison, you probably should be
        # using ambitious-activerecord instead.
        def include?(left, right)
          raise "Not applicable to sphinx."
        end
      end
    end
  end
end