class RediSearch::Search::Term

Attributes

options[RW]
term[RW]

Public Class Methods

new(term, **options) click to toggle source
# File lib/redi_search/search/term.rb, line 15
def initialize(term, **options)
  @term = term
  @options = options

  validate!
end

Public Instance Methods

to_s() click to toggle source
# File lib/redi_search/search/term.rb, line 22
def to_s
  if @term.is_a? Range
    stringify_range
  else
    stringify_query
  end
end

Private Instance Methods

fuzziness() click to toggle source
# File lib/redi_search/search/term.rb, line 34
def fuzziness
  @fuzziness ||= options[:fuzziness]
end
fuzzy_operator() click to toggle source
# File lib/redi_search/search/term.rb, line 50
def fuzzy_operator
  "%" * fuzziness.to_i
end
option() click to toggle source
# File lib/redi_search/search/term.rb, line 71
def option
  options.keys.first&.to_sym
end
optional_operator() click to toggle source
# File lib/redi_search/search/term.rb, line 38
def optional_operator
  return unless options[:optional]

  "~"
end
prefix_operator() click to toggle source
# File lib/redi_search/search/term.rb, line 44
def prefix_operator
  return unless options[:prefix]

  "*"
end
stringify_query() click to toggle source
# File lib/redi_search/search/term.rb, line 54
def stringify_query
  @term.to_s.
    tr("`", "\`").
    yield_self { |str| "#{fuzzy_operator}#{str}#{fuzzy_operator}" }.
    yield_self { |str| "#{optional_operator}#{str}" }.
    yield_self { |str| "#{str}#{prefix_operator}" }.
    yield_self { |str| "`#{str}`" }
end
stringify_range() click to toggle source
# File lib/redi_search/search/term.rb, line 63
def stringify_range
  first, last = @term.first, @term.last
  first = "-inf" if first == -Float::INFINITY
  last = "+inf" if last == Float::INFINITY

  "[#{first} #{last}]"
end