module Doing::StringQuery

Handling of search and regex strings

Public Instance Methods

ignore?() click to toggle source

Test if line should be ignored

@return [Boolean] line is empty or comment

# File lib/doing/string/query.rb, line 24
def ignore?
  line = self
  line =~ /^#/ || line =~ /^\s*$/
end
ignore_case(search, case_type) click to toggle source

Determine whether case should be ignored for string

@param search The search string @param case_type The case type, :smart, :sensitive, :ignore

@return [Boolean] true if case should be ignored

# File lib/doing/string/query.rb, line 15
def ignore_case(search, case_type)
  (case_type == :smart && search !~ /[A-Z]/) || case_type == :ignore
end
rx?() click to toggle source

Determines if receiver is surrounded by slashes or starts with single quote

@return [Boolean] True if regex, False otherwise.

# File lib/doing/string/query.rb, line 34
def rx?
  self =~ %r{(^/.*?/$|^')}
end
to_bool() click to toggle source

Returns a bool representation of the string.

@return [Boolean] Bool representation of the object.

# File lib/doing/string/query.rb, line 130
def to_bool
  case self
  when /^[yt1]/i
    true
  else
    false
  end
end
to_phrase_query() click to toggle source

Returns a phrase query (elastic search) representation of the object as a phrase parser.

@return Phrase query representation of the object.

# File lib/doing/string/query.rb, line 93
def to_phrase_query
  parser = PhraseParser::QueryParser.new
  transformer = PhraseParser::QueryTransformer.new
  parse_tree = parser.parse(self)
  transformer.apply(parse_tree).to_elasticsearch
end
to_query() click to toggle source

Returns a query (elastic search) representation of the object as a boolean term parser.

@return Query representation of the object.

# File lib/doing/string/query.rb, line 105
def to_query
  parser = BooleanTermParser::QueryParser.new
  transformer = BooleanTermParser::QueryTransformer.new
  parse_tree = parser.parse(self)
  transformer.apply(parse_tree).to_elasticsearch
end
to_rx(distance: nil, case_type: nil) click to toggle source

Convert string to fuzzy regex. Characters in words can be separated by up to distance characters in haystack, spaces indicate unlimited distance.

@example “this word”.to_rx(3) # => /t.{0,3}h.{0,3}i.{0,3}s.{0,3}.*?w.{0,3}o.{0,3}r.{0,3}d/

@param distance [Integer] Allowed distance between characters @param case_type The case type

@return [Regexp] Regex pattern

# File lib/doing/string/query.rb, line 63
def to_rx(distance: nil, case_type: nil)
  distance ||= Doing.config.fetch('search', 'distance', 3).to_i
  case_type ||= Doing.config.fetch('search', 'case', 'smart')&.normalize_case
  case_sensitive = case case_type
                   when :smart
                     self =~ /[A-Z]/ ? true : false
                   when :sensitive
                     true
                   else
                     false
                   end

  pattern = case dup.strip
            when %r{^/.*?/$}
              sub(%r{/(.*?)/}, '\1')
            when /^'/
              sub(/^'(.*?)'?$/, '\1')
            else
              split(/ +/).map do |w|
                w.split('').join(".{0,#{distance}}").gsub(/\+/, '\+').wildcard_to_rx
              end.join('.*?')
            end
  Regexp.new(pattern, !case_sensitive)
end
truthy?() click to toggle source

Test string for truthiness (0, “f”, “false”, “n”, “no” all return false, case insensitive, otherwise true)

@return [Boolean] String is truthy

# File lib/doing/string/query.rb, line 117
def truthy?
  if self =~ /^(0|f(alse)?|n(o)?)$/i
    false
  else
    true
  end
end
wildcard_to_rx() click to toggle source

Convert ? and * wildcards to regular expressions. Uses S (non-whitespace) instead of . (any character)

@return [String] Regular expression string

# File lib/doing/string/query.rb, line 44
def wildcard_to_rx
  gsub(/\?/, '\S').gsub(/\*/, '\S*?').gsub(/\]\]/, '--')
end