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.
166 167 168 |
# File 'lib/thamble.rb', line 166 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
151 152 153 |
# File 'lib/thamble.rb', line 151 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.
156 157 158 159 160 161 162 |
# File 'lib/thamble.rb', line 156 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.
186 187 188 |
# File 'lib/thamble.rb', line 186 def close "</#{@type}>\n" end |
#content ⇒ Object
A string for the inner HTML for the tag.
181 182 183 |
# File 'lib/thamble.rb', line 181 def content h @content end |
#open ⇒ Object
A string for the opening HTML for the tag.
176 177 178 |
# File 'lib/thamble.rb', line 176 def open "<#{@type}#{' ' unless @attr.empty?}#{attr}>" end |
#to_s ⇒ Object
A string for the HTML to use for this tag.
171 172 173 |
# File 'lib/thamble.rb', line 171 def to_s "#{open}#{content}#{close}" end |