Class: Utopia::Tag

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Tag.



15
16
17
18
19
# File 'lib/utopia/tag.rb', line 15

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

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



22
23
24
# File 'lib/utopia/tag.rb', line 22

def attributes
  @attributes
end

#closedObject

Returns the value of attribute closed.



23
24
25
# File 'lib/utopia/tag.rb', line 23

def closed
  @closed
end

#nameObject (readonly)

Returns the value of attribute name.



21
22
23
# File 'lib/utopia/tag.rb', line 21

def name
  @name
end

Class Method Details

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



8
9
10
11
12
13
# File 'lib/utopia/tag.rb', line 8

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

Instance Method Details

#[](key) ⇒ Object



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

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

#append(text) ⇒ Object



29
30
31
32
# File 'lib/utopia/tag.rb', line 29

def append(text)
	@content ||= StringIO.new
	@content.write text
end

#to_hashObject



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

def to_hash
	@attributes
end

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



34
35
36
37
38
# File 'lib/utopia/tag.rb', line 34

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

#to_sObject



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

def to_s
	buf = StringIO.new
	write_open_html(buf)
	return buf.string
end

#write_close_html(buf) ⇒ Object



69
70
71
# File 'lib/utopia/tag.rb', line 69

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

#write_full_html(buf, content = nil) ⇒ Object



73
74
75
76
77
78
79
80
81
# File 'lib/utopia/tag.rb', line 73

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

#write_open_html(buf, terminate = false) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/utopia/tag.rb', line 50

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

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