Class: Utopia::Content::Tag
- Inherits:
-
Object
- Object
- Utopia::Content::Tag
- Defined in:
- lib/utopia/content/tag.rb
Overview
This represents an individual SGML tag, e.g. <a>, </a> or <a />, with attributes. Attribute values must be escaped.
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
- #==(other) ⇒ Object
- #[](key) ⇒ Object
-
#initialize(name, attributes = {}) ⇒ Tag
constructor
A new instance of Tag.
- #to_hash ⇒ Object
- #to_html(content = nil, buffer = StringIO.new) ⇒ Object
- #to_s(content = nil) ⇒ Object
- #write_close_html(buffer) ⇒ Object
- #write_full_html(buffer, content = nil) ⇒ Object
- #write_open_html(buffer, terminate = false) ⇒ Object
Constructor Details
#initialize(name, attributes = {}) ⇒ Tag
Returns a new instance of Tag.
38 39 40 41 42 |
# File 'lib/utopia/content/tag.rb', line 38 def initialize(name, attributes = {}) @name = name @attributes = attributes @closed = false end |
Instance Attribute Details
#attributes ⇒ Object (readonly)
Returns the value of attribute attributes.
45 46 47 |
# File 'lib/utopia/content/tag.rb', line 45 def attributes @attributes end |
#closed ⇒ Object
Returns the value of attribute closed.
46 47 48 |
# File 'lib/utopia/content/tag.rb', line 46 def closed @closed end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
44 45 46 |
# File 'lib/utopia/content/tag.rb', line 44 def name @name end |
Class Method Details
Instance Method Details
#==(other) ⇒ Object
25 26 27 28 29 |
# File 'lib/utopia/content/tag.rb', line 25 def == other if Tag === other [@name, @attributes, @closed] == [other.name, other.attributes, other.closed] end end |
#[](key) ⇒ Object
48 49 50 |
# File 'lib/utopia/content/tag.rb', line 48 def [](key) @attributes[key] end |
#to_hash ⇒ Object
58 59 60 |
# File 'lib/utopia/content/tag.rb', line 58 def to_hash @attributes end |
#to_html(content = nil, buffer = StringIO.new) ⇒ Object
52 53 54 55 56 |
# File 'lib/utopia/content/tag.rb', line 52 def to_html(content = nil, buffer = StringIO.new) write_full_html(buffer, content) return buffer.string end |
#to_s(content = nil) ⇒ Object
62 63 64 65 66 |
# File 'lib/utopia/content/tag.rb', line 62 def to_s(content = nil) buffer = StringIO.new write_full_html(buffer, content) return buffer.string end |
#write_close_html(buffer) ⇒ Object
87 88 89 |
# File 'lib/utopia/content/tag.rb', line 87 def write_close_html(buffer) buffer.write "</#{name}>" end |
#write_full_html(buffer, content = nil) ⇒ Object
91 92 93 94 95 96 97 98 99 |
# File 'lib/utopia/content/tag.rb', line 91 def write_full_html(buffer, content = nil) if @closed && content == nil write_open_html(buffer, true) else write_open_html(buffer) buffer.write(content) write_close_html(buffer) end end |
#write_open_html(buffer, terminate = false) ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/utopia/content/tag.rb', line 68 def write_open_html(buffer, terminate = false) buffer ||= StringIO.new buffer.write "<#{name}" @attributes.each do |key, value| if value buffer.write " #{key}=\"#{value}\"" else buffer.write " #{key}" end end if terminate buffer.write "/>" else buffer.write ">" end end |