Class: Teepee::StringToken

Inherits:
Token show all
Defined in:
lib/teepee/string-token.rb

Direct Known Subclasses

EmptyNewlinesToken, WhitespaceToken, WordToken

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ParserNode

#use_scope

Constructor Details

#initialize(text) ⇒ StringToken

Returns a new instance of StringToken.

Raises:

  • (ArgumentError)


50
51
52
53
54
# File 'lib/teepee/string-token.rb', line 50

def initialize(text)
  raise ArgumentError if not text.is_a? String
  raise ArgumentError if not text =~ self.class.full_match_regex
  @text = text
end

Instance Attribute Details

#textObject (readonly)

Returns the value of attribute text.



48
49
50
# File 'lib/teepee/string-token.rb', line 48

def text
  @text
end

Class Method Details

.count_regexObject



85
86
87
# File 'lib/teepee/string-token.rb', line 85

def count_regex
  self::COUNT_REGEX # Define this in a child class.
end

.front_match_regexObject



81
82
83
# File 'lib/teepee/string-token.rb', line 81

def front_match_regex
  self::FRONT_MATCH_REGEX # Define this in a child class.
end

.full_match_regexObject



77
78
79
# File 'lib/teepee/string-token.rb', line 77

def full_match_regex
  self::FULL_MATCH_REGEX # Define this in a child class.
end

.matches?(text) ⇒ Boolean

Returns:

  • (Boolean)


89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/teepee/string-token.rb', line 89

def matches? text
  if text =~ front_match_regex
    count = text.index count_regex
    if count.nil?
      return [self.new(text), ""]
    else
      return [self.new(text[0 ... count]), text[count .. -1]]
    end
  else
    return nil
  end
end

Instance Method Details

#number?Boolean

Returns:

  • (Boolean)


64
65
66
# File 'lib/teepee/string-token.rb', line 64

def number?
  not to_number.nil?
end

#to_htmlObject



60
61
62
# File 'lib/teepee/string-token.rb', line 60

def to_html
  @text
end

#to_numberObject



68
69
70
71
72
73
74
# File 'lib/teepee/string-token.rb', line 68

def to_number
  begin
    Float(to_html)
  rescue ArgumentError
    nil
  end
end

#to_sObject



56
57
58
# File 'lib/teepee/string-token.rb', line 56

def to_s
  @text
end