Class: SearchLingo::Token

Inherits:
String
  • Object
show all
Defined in:
lib/search_lingo/token.rb

Constant Summary collapse

STRUCTURE =

Pattern for decomposing a token into a modifier and a term.

/\A(?:(#{MODIFIER}):[[:space:]]*)?"?(.+?)"?\z/

Instance Method Summary collapse

Instance Method Details

#compound?Boolean

Returns true if token has a modifier and false otherwise.

Token.new('foo: bar').compound? # => true
Token.new('bar').compound?      # => false

Returns:

  • (Boolean)


38
39
40
# File 'lib/search_lingo/token.rb', line 38

def compound?
  !!modifier
end

#inspectObject

:nodoc:



42
43
44
45
# File 'lib/search_lingo/token.rb', line 42

def inspect # :nodoc:
  '#<%s String(%s) modifier=%s term=%s>' %
    [self.class, super, modifier.inspect, term.inspect]
end

#modifierObject Also known as: 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


16
17
18
# File 'lib/search_lingo/token.rb', line 16

def modifier
  self[STRUCTURE, 1]
end

#termObject

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'


29
30
31
# File 'lib/search_lingo/token.rb', line 29

def term
  self[STRUCTURE, 2]
end