Class: TBMX::StringToken

Inherits:
Token show all
Defined in:
lib/tbmx.rb

Direct Known Subclasses

EmptyNewlinesToken, WhitespaceToken, WordToken

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(text) ⇒ StringToken

Returns a new instance of StringToken.

Raises:

  • (ArgumentError)


104
105
106
107
108
# File 'lib/tbmx.rb', line 104

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.



102
103
104
# File 'lib/tbmx.rb', line 102

def text
  @text
end

Class Method Details

.count_regexObject



127
128
129
# File 'lib/tbmx.rb', line 127

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

.front_match_regexObject



123
124
125
# File 'lib/tbmx.rb', line 123

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

.full_match_regexObject



119
120
121
# File 'lib/tbmx.rb', line 119

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

.matches?(text) ⇒ Boolean

Returns:

  • (Boolean)


131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/tbmx.rb', line 131

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

#to_htmlObject



114
115
116
# File 'lib/tbmx.rb', line 114

def to_html
  @text
end

#to_sObject



110
111
112
# File 'lib/tbmx.rb', line 110

def to_s
  @text
end