Module: Doing::StringQuery
- Included in:
- String
- Defined in:
- lib/doing/string/query.rb
Overview
Handling of search and regex strings
Instance Method Summary collapse
-
#ignore? ⇒ Boolean
Test if line should be ignored.
-
#ignore_case(search, case_type) ⇒ Boolean
Determine whether case should be ignored for string.
-
#rx? ⇒ Boolean
Determines if receiver is surrounded by slashes or starts with single quote.
-
#to_bool ⇒ Boolean
Returns a bool representation of the string.
-
#to_phrase_query ⇒ Object
Returns a phrase query (elastic search) representation of the object as a phrase parser.
-
#to_query ⇒ Object
Returns a query (elastic search) representation of the object as a boolean term parser.
-
#to_rx(distance: nil, case_type: nil) ⇒ Regexp
Convert string to fuzzy regex.
-
#truthy? ⇒ Boolean
Test string for truthiness (0, "f", "false", "n", "no" all return false, case insensitive, otherwise true).
-
#wildcard_to_rx ⇒ String
Convert ? and * wildcards to regular expressions.
Instance Method Details
#ignore? ⇒ Boolean
Test if line should be ignored
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
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
34 35 36 |
# File 'lib/doing/string/query.rb', line 34 def rx? self =~ %r{(^/.*?/$|^')} end |
#to_bool ⇒ Boolean
Returns a bool representation of the string.
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_query ⇒ Object
Returns a phrase query (elastic search) representation of the object as a phrase parser.
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_query ⇒ Object
Returns a query (elastic search) representation of the object as a boolean term parser.
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.
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)
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_rx ⇒ String
Convert ? and * wildcards to regular expressions. Uses \S (non-whitespace) instead of . (any character)
44 45 46 |
# File 'lib/doing/string/query.rb', line 44 def wildcard_to_rx gsub(/\?/, '\S').gsub(/\*/, '\S*?').gsub(/\]\]/, '--') end |