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.



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

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

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



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

def attributes
  @attributes
end

#closedObject

Returns the value of attribute closed.



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

def closed
  @closed
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

Instance Method Details

#[](key) ⇒ Object



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

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

#append(text) ⇒ Object



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

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

#to_hashObject



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

def to_hash
	@attributes
end

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



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

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

#to_sObject



48
49
50
51
52
# File 'lib/utopia/tag.rb', line 48

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



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

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