lib/search_lingo/token.rb in search_lingo-2.0.0.pre2 vs lib/search_lingo/token.rb in search_lingo-2.0.0.pre3

- old
+ new

@@ -1,9 +1,24 @@ +# frozen-string-literal: true + require 'delegate' require 'search_lingo/constants' module SearchLingo + ## + # Single token from a query string. A token consists of a term an an optional + # modifier. The term may be a word or multiple words contained within double + # quotes. The modifier is one or more alphanumeric characters. The modifier + # and term and separated by a colon followed by zero or more whitespace + # characters. + # + # The following are examples of tokens: + # + # Token.new('foo') + # Token.new('"foo bar"') + # Token.new('foo: bar') + # Token.new('foo: "bar baz"') class Token < DelegateClass(String) ## # Pattern for decomposing a token into a modifier and a term. STRUCTURE = /\A(?:(#{MODIFIER}):[[:space:]]*)?"?(.+?)"?\z/ @@ -15,11 +30,11 @@ # Token.new('bar').modifier # => nil def modifier self[STRUCTURE, 1] end - alias_method :operator, :modifier + alias operator modifier ## # Returns the term portion of the token. If the term is wrapped in quotes, # they are removed. # @@ -34,14 +49,17 @@ # Returns +true+ if token has a modifier and +false+ otherwise. # # Token.new('foo: bar').compound? # => true # Token.new('bar').compound? # => false def compound? - !!modifier + !modifier.nil? && !modifier.empty? end def inspect # :nodoc: - '#<%s String(%s) modifier=%s term=%s>' % - [self.class, super, modifier.inspect, term.inspect] + format '#<%<cls>s String(%<str>s) modifier=%<mod>s term=%<term>s>', + cls: self.class, + str: super, + mod: modifier.inspect, + term: term.inspect end end end