lib/search_lingo/token.rb in search_lingo-1.0.1 vs lib/search_lingo/token.rb in search_lingo-1.0.2
- old
+ new
@@ -1,25 +1,47 @@
require 'delegate'
require 'search_lingo/constants'
module SearchLingo
class Token < DelegateClass(String)
- STRUCTURE = /\A(?:(#{OPERATOR}):[[:space:]]*)?"?(.+?)"?\z/
+ ##
+ # Pattern for decomposing a token into a modifier and a term.
+ STRUCTURE = /\A(?:(#{MODIFIER}):[[:space:]]*)?"?(.+?)"?\z/
- def operator
+ ##
+ # Returns the modifier portion of the token. Returns +nil+ if token does
+ # not have a modifier.
+ #
+ # Token.new('foo: bar').modifier # => 'foo'
+ # Token.new('bar').modifier # => nil
+ def modifier
self[STRUCTURE, 1]
end
+ alias_method :operator, :modifier
+
+ ##
+ # Returns the term portion of the token. If the term is wrapped in quotes,
+ # they are removed.
+ #
+ # Token.new('foo: bar').term # => 'bar'
+ # Token.new('bar').term # => 'bar'
+ # Token.new('"bar baz"').term # => 'bar baz'
def term
self[STRUCTURE, 2]
end
+ ##
+ # Returns +true+ if token has a modifier and +false+ otherwise.
+ #
+ # Token.new('foo: bar').compound? # => true
+ # Token.new('bar').compound? # => false
def compound?
- !!operator
+ !!modifier
end
- def inspect
- '#<%s String(%s) operator=%s term=%s>' %
- [self.class, super, operator.inspect, term.inspect]
+ def inspect # :nodoc:
+ '#<%s String(%s) modifier=%s term=%s>' %
+ [self.class, super, modifier.inspect, term.inspect]
end
end
end