Class: Pursuit::SearchTermParser
- Inherits:
-
Object
- Object
- Pursuit::SearchTermParser
- Defined in:
- lib/pursuit/search_term_parser.rb
Defined Under Namespace
Classes: KeyedTerm
Instance Attribute Summary collapse
-
#keyed_terms ⇒ Array<Pursuit::SearchTermParser::KeyedTerm>
readonly
The keys which are permitted for use as keyed terms.
-
#unkeyed_term ⇒ String
readonly
The unkeyed term.
Instance Method Summary collapse
-
#initialize(query, keys: []) ⇒ SearchTermParser
constructor
Create a new search term parser by parsing the specified query into an ‘unkeyed term’ and ‘keyed terms’.
Constructor Details
#initialize(query, keys: []) ⇒ SearchTermParser
Create a new search term parser by parsing the specified query into an ‘unkeyed term’ and ‘keyed terms’.
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# 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 |
Instance Attribute Details
#keyed_terms ⇒ Array<Pursuit::SearchTermParser::KeyedTerm> (readonly)
Returns The keys which are permitted for use as keyed terms.
11 12 13 |
# File 'lib/pursuit/search_term_parser.rb', line 11 def keyed_terms @keyed_terms end |
#unkeyed_term ⇒ String (readonly)
Returns The unkeyed term.
15 16 17 |
# File 'lib/pursuit/search_term_parser.rb', line 15 def unkeyed_term @unkeyed_term end |