Class: ThinkingSphinx::Wildcard

Inherits:
Object
  • Object
show all
Defined in:
lib/thinking_sphinx/wildcard.rb

Constant Summary collapse

DEFAULT_TOKEN =
/\p{Word}+/

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(query, pattern = DEFAULT_TOKEN) ⇒ Wildcard

Returns a new instance of Wildcard.



10
11
12
13
# File 'lib/thinking_sphinx/wildcard.rb', line 10

def initialize(query, pattern = DEFAULT_TOKEN)
  @query   = query || ''
  @pattern = pattern.is_a?(Regexp) ? pattern : DEFAULT_TOKEN
end

Class Method Details

.call(query, pattern = DEFAULT_TOKEN) ⇒ Object



6
7
8
# File 'lib/thinking_sphinx/wildcard.rb', line 6

def self.call(query, pattern = DEFAULT_TOKEN)
  new(query, pattern).call
end

Instance Method Details

#callObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/thinking_sphinx/wildcard.rb', line 15

def call
  query.gsub(extended_pattern) do
    pre, proper, post = $`, $&, $'
    # E.g. "@foo", "/2", "~3", but not as part of a token pattern
    is_operator = pre.match(%r{@$}) ||
                  pre.match(%r{([^\\]+|\A)[~/]\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