Module: Doing::StringQuery

Included in:
String
Defined in:
lib/doing/string/query.rb

Overview

Handling of search and regex strings

Instance Method Summary collapse

Instance Method Details

#ignore?Boolean

Test if line should be ignored

Returns:

  • (Boolean)

    line is empty or comment



24
25
26
27
# File 'lib/doing/string/query.rb', line 24

def ignore?
  line = self
  line =~ /^#/ || line =~ /^\s*$/
end

#ignore_case(search, case_type) ⇒ Boolean

Determine whether case should be ignored for string

Parameters:

  • search

    The search string

  • case_type

    The case type, :smart, :sensitive, :ignore

Returns:

  • (Boolean)

    true if case should be ignored



15
16
17
# 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?Boolean

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

Returns:

  • (Boolean)

    True if regex, False otherwise.



34
35
36
# File 'lib/doing/string/query.rb', line 34

def rx?
  self =~ %r{(^/.*?/$|^')}
end

#to_boolBoolean

Returns a bool representation of the string.

Returns:

  • (Boolean)

    Bool representation of the object.



130
131
132
133
134
135
136
137
# File 'lib/doing/string/query.rb', line 130

def to_bool
  case self
  when /^[yt1]/i
    true
  else
    false
  end
end

#to_phrase_queryObject

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

Returns:

  • Phrase query representation of the object.



93
94
95
96
97
98
# 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_queryObject

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

Returns:

  • Query representation of the object.



105
106
107
108
109
110
# 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) ⇒ Regexp

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

Examples:

"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/

Parameters:

  • distance (Integer) (defaults to: nil)

    Allowed distance between characters

  • case_type (defaults to: nil)

    The case type

Returns:

  • (Regexp)

    Regex pattern



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# 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?Boolean

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

Returns:

  • (Boolean)

    String is truthy



117
118
119
120
121
122
123
# 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_rxString

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

Returns:

  • (String)

    Regular expression string



44
45
46
# File 'lib/doing/string/query.rb', line 44

def wildcard_to_rx
  gsub(/\?/, '\S').gsub(/\*/, '\S*?').gsub(/\]\]/, '--')
end