class Pursuit::SearchTermParser
Constants
- KeyedTerm
@return [Struct] Represents a single keyed term extracted from a query.
Attributes
keyed_terms[R]
@return [Array<Pursuit::SearchTermParser::KeyedTerm>] The keys which are permitted for use as keyed terms.
unkeyed_term[R]
@return [String] The unkeyed term.
Public Class Methods
new(query, keys: [])
click to toggle source
Create a new search term parser by parsing the specified query into an ‘unkeyed term’ and ‘keyed terms’.
@param query [String] The query to parse. @param keys [Array<String>] The keys which are permitted for use as keyed terms.
# File lib/pursuit/search_term_parser.rb, line 22 def initialize(query, keys: []) @keyed_terms = [] @unkeyed_term = query.gsub(/(\s+)?(\w+)(==|\*=|!=|!\*=|<=|>=|<|>)("([^"]+)?"|'([^']+)?'|[^\s]+)(\s+)?/) do |term| key = Regexp.last_match(2) next term unless keys.include?(key) operator = Regexp.last_match(3) value = Regexp.last_match(4) value = value[1..-2] if value =~ /^(".*"|'.*')$/ @keyed_terms << KeyedTerm.new(key, operator, value) # Both the starting and ending spaces surrounding the keyed term can be removed, so in this case we'll need to # replace with a single space to ensure the unkeyed term's words are separated correctly. if term =~ /^\s.*\s$/ ' ' else '' end end @unkeyed_term = @unkeyed_term.strip end