Class: Thamble::Tag
- Inherits:
-
Object
- Object
- Thamble::Tag
- Defined in:
- lib/thamble.rb
Overview
Tags represent an individual HTML tag.
Instance Attribute Summary collapse
-
#type ⇒ Object
readonly
The type of the tag.
Class Method Summary collapse
-
.tag(type, content = '', attr = nil) ⇒ Object
If the given content is already a Tag instance with the same type, return it directly, otherwise create a new Tag instance with the given arguments.
Instance Method Summary collapse
-
#close ⇒ Object
A string for the closing HTML for the tag.
-
#content ⇒ Object
A string for the inner HTML for the tag.
-
#initialize(type, content = '', attr = nil) ⇒ Tag
constructor
Create a new instance.
-
#open ⇒ Object
A string for the opening HTML for the tag.
-
#to_s ⇒ Object
A string for the HTML to use for this tag.
Constructor Details
#initialize(type, content = '', attr = nil) ⇒ Tag
Create a new instance. Usually not called directly, but through Tag.tag.
173 174 175 |
# File 'lib/thamble.rb', line 173 def initialize(type, content='', attr=nil) @type, @content, @attr = type, content, attr||OPTS end |
Instance Attribute Details
#type ⇒ Object (readonly)
The type of the tag
158 159 160 |
# File 'lib/thamble.rb', line 158 def type @type end |
Class Method Details
.tag(type, content = '', attr = nil) ⇒ Object
If the given content is already a Tag instance with the same type, return it directly, otherwise create a new Tag instance with the given arguments.
163 164 165 166 167 168 169 |
# File 'lib/thamble.rb', line 163 def self.tag(type, content='', attr=nil) if content.is_a?(Tag) && content.type.to_s == type.to_s content else new(type, content, attr) end end |
Instance Method Details
#close ⇒ Object
A string for the closing HTML for the tag.
193 194 195 |
# File 'lib/thamble.rb', line 193 def close "</#{@type}>\n" end |
#content ⇒ Object
A string for the inner HTML for the tag.
188 189 190 |
# File 'lib/thamble.rb', line 188 def content h @content end |
#open ⇒ Object
A string for the opening HTML for the tag.
183 184 185 |
# File 'lib/thamble.rb', line 183 def open "<#{@type}#{' ' unless @attr.empty?}#{attr}>" end |
#to_s ⇒ Object
A string for the HTML to use for this tag.
178 179 180 |
# File 'lib/thamble.rb', line 178 def to_s "#{open}#{content}#{close}" end |