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

Constructor Details

#initialize(text) ⇒ StringToken

Returns a new instance of StringToken.

Raises:

  • (ArgumentError)


44
45
46
47
48
# File 'lib/teepee/string-token.rb', line 44

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.



42
43
44
# File 'lib/teepee/string-token.rb', line 42

def text
  @text
end

Class Method Details

.count_regexObject



67
68
69
# File 'lib/teepee/string-token.rb', line 67

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

.front_match_regexObject



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

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

.full_match_regexObject



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

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

.matches?(text) ⇒ Boolean



71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/teepee/string-token.rb', line 71

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



54
55
56
# File 'lib/teepee/string-token.rb', line 54

def to_html
  @text
end

#to_sObject



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

def to_s
  @text
end