Class: Thamble::Tag

Inherits:
Object
  • Object
show all
Defined in:
lib/thamble.rb

Overview

Tags represent an individual HTML tag.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#typeObject (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

#closeObject

A string for the closing HTML for the tag.



193
194
195
# File 'lib/thamble.rb', line 193

def close
  "</#{@type}>\n"
end

#contentObject

A string for the inner HTML for the tag.



188
189
190
# File 'lib/thamble.rb', line 188

def content
  h @content
end

#openObject

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_sObject

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