Class: Dima::Html::Node

Inherits:
Object
  • Object
show all
Includes:
Init
Defined in:
lib/dima/html/node.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Init

#initialize

Instance Attribute Details

#attributesObject

Returns the value of attribute attributes.



6
7
8
# File 'lib/dima/html/node.rb', line 6

def attributes
  @attributes
end

#childrenObject

Returns the value of attribute children.



7
8
9
# File 'lib/dima/html/node.rb', line 7

def children
  @children
end

#tagObject

Returns the value of attribute tag.



6
7
8
# File 'lib/dima/html/node.rb', line 6

def tag
  @tag
end

#textObject

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

#htmlObject

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