class ThinkingSphinx::Search::Query

Constants

DEFAULT_TOKEN

Attributes

conditions[R]
keywords[R]
star[R]

Public Class Methods

new(keywords = '', conditions = {}, star = false) click to toggle source
# File lib/thinking_sphinx/search/query.rb, line 7
def initialize(keywords = '', conditions = {}, star = false)
  @keywords, @conditions, @star = keywords, conditions, star
end

Public Instance Methods

to_s() click to toggle source
# File lib/thinking_sphinx/search/query.rb, line 11
def to_s
  (star_keyword(keywords || '') + ' ' + conditions.keys.collect { |key|
     next if conditions[key].blank?

    "@#{key} #{star_keyword conditions[key], key}"
  }.join(' ')).strip
end

Private Instance Methods

star_keyword(keyword, key = nil) click to toggle source
# File lib/thinking_sphinx/search/query.rb, line 21
def star_keyword(keyword, key = nil)
  unless star && (key.nil? || key.to_s != 'sphinx_internal_class_name')
    return keyword.to_s
  end

  token = star.is_a?(Regexp) ? star : DEFAULT_TOKEN
  keyword.gsub(/("#{token}(.*?#{token})?"|(?![!-])#{token})/) do
    pre, proper, post = $`, $&, $'
    # E.g. "@foo", "/2", "~3", but not as part of a token
    is_operator = pre.match(%r{\A(\W|^)[@~/]\Z}) ||
                  pre.match(%r{(\W|^)@\([^\)]*$})
    # E.g. "foo bar", with quotes
    is_quote    = proper[/^".*"$/]
    has_star    = post[/\*$/] || pre[/^\*/]
    if is_operator || is_quote || has_star
      proper
    else
      "*#{proper}*"
    end
  end
end