Class: Utopia::Tag

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Tag.



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

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

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



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

def attributes
  @attributes
end

#closedObject

Returns the value of attribute closed.



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

def closed
  @closed
end

#nameObject (readonly)

Returns the value of attribute name.



14
15
16
# File 'lib/utopia/tag.rb', line 14

def name
  @name
end

Instance Method Details

#[](key) ⇒ Object



18
19
20
# File 'lib/utopia/tag.rb', line 18

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

#append(text) ⇒ Object



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

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

#to_hashObject



33
34
35
# File 'lib/utopia/tag.rb', line 33

def to_hash
	@attributes
end

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



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

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

#to_sObject



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

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

#write_close_html(buf) ⇒ Object



58
59
60
# File 'lib/utopia/tag.rb', line 58

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

#write_full_html(buf, content = nil) ⇒ Object



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

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



43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/utopia/tag.rb', line 43

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

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