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. , or , 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



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

def to_hash
	@attributes
end

#to_s(content = nil) ⇒ Object Also known as: to_html



64
65
66
67
68
69
70
# File 'lib/utopia/content/tag.rb', line 64

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

#write_close_html(buffer) ⇒ Object



92
93
94
# File 'lib/utopia/content/tag.rb', line 92

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

#write_full_html(buffer, content = nil) ⇒ Object



96
97
98
99
100
101
102
103
104
# File 'lib/utopia/content/tag.rb', line 96

def write_full_html(buffer, content = nil)
	if @closed and content.nil?
		write_open_html(buffer, true)
	else
		write_open_html(buffer)
		buffer << content if content
		write_close_html(buffer)
	end
end

#write_open_html(buffer, terminate = false) ⇒ Object



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

def write_open_html(buffer, terminate = false)
	buffer << "<#{name}"

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