Class: Utopia::Content::Tag

Inherits:
Object
  • Object
show all
Defined in:
lib/utopia/content/tag.rb

Overview

This represents an individual SGML tag, e.g. <a>, </a> or <a />, with attributes. Attribute values must be escaped.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, attributes = {}) ⇒ Tag

Returns a new instance of Tag.



38
39
40
41
42
# File 'lib/utopia/content/tag.rb', line 38

def initialize(name, attributes = {})
	@name = name
	@attributes = attributes
	@closed = false
end

Instance Attribute Details

#attributesObject

Returns the value of attribute attributes.



53
54
55
# File 'lib/utopia/content/tag.rb', line 53

def attributes
  @attributes
end

#closedObject

Returns the value of attribute closed.



54
55
56
# File 'lib/utopia/content/tag.rb', line 54

def closed
  @closed
end

#nameObject

Returns the value of attribute name.



52
53
54
# File 'lib/utopia/content/tag.rb', line 52

def name
  @name
end

Class Method Details

.closed(name, attributes = {}) ⇒ Object



31
32
33
34
35
36
# File 'lib/utopia/content/tag.rb', line 31

def self.closed(name, attributes = {})
	tag = Tag.new(name, attributes)
	tag.closed = true
	
	return tag
end

Instance Method Details

#==(other) ⇒ Object



25
26
27
28
29
# File 'lib/utopia/content/tag.rb', line 25

def == other
	if Tag === other
		[@name, @attributes, @closed] == [other.name, other.attributes, other.closed]
	end
end

#[](key) ⇒ Object



56
57
58
# File 'lib/utopia/content/tag.rb', line 56

def [](key)
	@attributes[key]
end

#freezeObject



44
45
46
47
48
49
50
# File 'lib/utopia/content/tag.rb', line 44

def freeze
	@name.freeze
	@attributes.freeze
	@closed.freeze
	
	super
end

#to_hashObject



66
67
68
# File 'lib/utopia/content/tag.rb', line 66

def to_hash
	@attributes
end

#to_html(content = nil, buffer = StringIO.new) ⇒ Object



60
61
62
63
64
# File 'lib/utopia/content/tag.rb', line 60

def to_html(content = nil, buffer = StringIO.new)
	write_full_html(buffer, content)
	
	return buffer.string
end

#to_s(content = nil) ⇒ Object



70
71
72
73
74
# File 'lib/utopia/content/tag.rb', line 70

def to_s(content = nil)
	buffer = StringIO.new
	write_full_html(buffer, content)
	return buffer.string
end

#write_close_html(buffer) ⇒ Object



95
96
97
# File 'lib/utopia/content/tag.rb', line 95

def write_close_html(buffer)
	buffer.write "</#{name}>"
end

#write_full_html(buffer, content = nil) ⇒ Object



99
100
101
102
103
104
105
106
107
# File 'lib/utopia/content/tag.rb', line 99

def write_full_html(buffer, content = nil)
	if @closed && content == nil
		write_open_html(buffer, true)
	else
		write_open_html(buffer)
		buffer.write(content)
		write_close_html(buffer)
	end
end

#write_open_html(buffer, terminate = false) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/utopia/content/tag.rb', line 76

def write_open_html(buffer, terminate = false)
	buffer ||= StringIO.new 
	buffer.write "<#{name}"

	@attributes.each do |key, value|
		if value
			buffer.write " #{key}=\"#{value}\""
		else
			buffer.write " #{key}"
		end
	end
	
	if terminate
		buffer.write "/>"
	else
		buffer.write ">"
	end
end