Class: Utopia::Tag
Instance Attribute Summary collapse
-
#attributes ⇒ Object
readonly
Returns the value of attribute attributes.
-
#closed ⇒ Object
Returns the value of attribute closed.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Class Method Summary collapse
Instance Method Summary collapse
- #[](key) ⇒ Object
- #append(text) ⇒ Object
-
#initialize(name, attributes = {}) ⇒ Tag
constructor
A new instance of Tag.
- #to_hash ⇒ Object
- #to_html(content = nil, buf = StringIO.new) ⇒ Object
- #to_s ⇒ Object
- #write_close_html(buf) ⇒ Object
- #write_full_html(buf, content = nil) ⇒ Object
- #write_open_html(buf, terminate = false) ⇒ Object
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
#attributes ⇒ Object (readonly)
Returns the value of attribute attributes.
22 23 24 |
# File 'lib/utopia/tag.rb', line 22 def attributes @attributes end |
#closed ⇒ Object
Returns the value of attribute closed.
23 24 25 |
# File 'lib/utopia/tag.rb', line 23 def closed @closed end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
21 22 23 |
# File 'lib/utopia/tag.rb', line 21 def name @name end |
Class Method Details
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_hash ⇒ Object
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_s ⇒ Object
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 |