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.



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

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

#closeObject

A string for the closing HTML for the tag.



186
187
188
# File 'lib/thamble.rb', line 186

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

#contentObject

A string for the inner HTML for the tag.



181
182
183
# File 'lib/thamble.rb', line 181

def content
  h @content
end

#openObject

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_sObject

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