module Doing::StringQuery
Handling of search and regex strings
Public Instance Methods
Source
# File lib/doing/string/query.rb, line 24 def ignore? line = self line =~ /^#/ || line =~ /^\s*$/ end
Test if line should be ignored
@return [Boolean] line is empty or comment
Source
# File lib/doing/string/query.rb, line 15 def ignore_case(search, case_type) (case_type == :smart && search !~ /[A-Z]/) || case_type == :ignore end
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
Source
# File lib/doing/string/query.rb, line 34 def rx? self =~ %r{(^/.*?/$|^')} end
Determines if receiver is surrounded by slashes or starts with single quote
@return [Boolean] True if regex, False otherwise.
Source
# File lib/doing/string/query.rb, line 130 def to_bool case self when /^[yt1]/i true else false end end
Returns a bool representation of the string.
@return [Boolean] Bool representation of the object.
Source
# 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
Returns a phrase query (elastic search) representation of the object as a phrase parser.
@return Phrase query representation of the object.
Source
# 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
Returns a query (elastic search) representation of the object as a boolean term parser.
@return Query representation of the object.
Source
# 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
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
Source
# File lib/doing/string/query.rb, line 117 def truthy? if self =~ /^(0|f(alse)?|n(o)?)$/i false else true end end
Test string for truthiness (0, “f”, “false”, “n”, “no” all return false, case insensitive, otherwise true)
@return [Boolean] String
is truthy
Source
# File lib/doing/string/query.rb, line 44 def wildcard_to_rx gsub(/\?/, '\S').gsub(/\*/, '\S*?').gsub(/\]\]/, '--') end
Convert ? and * wildcards to regular expressions. Uses S (non-whitespace) instead of . (any character)
@return [String] Regular expression string