Class: Dima::Html::Node
Instance Attribute Summary collapse
-
#attributes ⇒ Object
Returns the value of attribute attributes.
-
#children ⇒ Object
Returns the value of attribute children.
-
#tag ⇒ Object
Returns the value of attribute tag.
-
#text ⇒ Object
Returns the value of attribute text.
Instance Method Summary collapse
-
#<<(node) ⇒ Object
Add child to the node.
-
#[](k) ⇒ Object
Get attribute value for the node.
-
#[]=(k, v) ⇒ Object
Set attribute for the node.
- #add_class(k) ⇒ Object
-
#html ⇒ Object
Generate HTML for this node.
Methods included from Init
Instance Attribute Details
#attributes ⇒ Object
Returns the value of attribute attributes.
6 7 8 |
# File 'lib/dima/html/node.rb', line 6 def attributes @attributes end |
#children ⇒ Object
Returns the value of attribute children.
7 8 9 |
# File 'lib/dima/html/node.rb', line 7 def children @children end |
#tag ⇒ Object
Returns the value of attribute tag.
6 7 8 |
# File 'lib/dima/html/node.rb', line 6 def tag @tag end |
#text ⇒ Object
Returns the value of attribute text.
6 7 8 |
# File 'lib/dima/html/node.rb', line 6 def text @text end |
Instance Method Details
#<<(node) ⇒ Object
Add child to the node.
20 21 22 23 |
# File 'lib/dima/html/node.rb', line 20 def << (node) @children ||= [] @children << node if node end |
#[](k) ⇒ Object
Get attribute value for the node.
32 33 34 |
# File 'lib/dima/html/node.rb', line 32 def [] (k) @attributes[k] if @attributes end |
#[]=(k, v) ⇒ Object
Set attribute for the node.
26 27 28 29 |
# File 'lib/dima/html/node.rb', line 26 def []= (k, v) @attributes ||= {} @attributes[k] = v end |
#add_class(k) ⇒ Object
36 37 38 39 40 |
# File 'lib/dima/html/node.rb', line 36 def add_class(k) clazz = self[:class] || '' clazz += ' ' + k self[:class] = clazz.strip end |
#html ⇒ Object
Generate HTML for this node.
10 11 12 13 14 15 16 17 |
# File 'lib/dima/html/node.rb', line 10 def html h = '' h << '<' << self.tag.to_s << generate_attributes << '>' h << self.text.to_s if self.text self.children.each { |c| h << c.html } if self.children h << '</' << self.tag << '>' h end |