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)


88
89
90
91
92
# File 'lib/tbmx.rb', line 88

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.



86
87
88
# File 'lib/tbmx.rb', line 86

def text
  @text
end

Class Method Details

.count_regexObject



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

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

.front_match_regexObject



107
108
109
# File 'lib/tbmx.rb', line 107

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

.full_match_regexObject



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

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

.matches?(text) ⇒ Boolean

Returns:

  • (Boolean)


115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/tbmx.rb', line 115

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



98
99
100
# File 'lib/tbmx.rb', line 98

def to_html
  @text
end

#to_sObject



94
95
96
# File 'lib/tbmx.rb', line 94

def to_s
  @text
end