Class: Utopia::Tag
- Inherits:
-
Object
- Object
- Utopia::Tag
- Defined in:
- lib/utopia/tag.rb
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.
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.
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
#attributes ⇒ Object (readonly)
Returns the value of attribute attributes.
26 27 28 |
# File 'lib/utopia/tag.rb', line 26 def attributes @attributes end |
#closed ⇒ Object
Returns the value of attribute closed.
27 28 29 |
# File 'lib/utopia/tag.rb', line 27 def closed @closed end |
#name ⇒ Object (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_hash ⇒ Object
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_s ⇒ Object
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 |