Class: RuBB::Node::Styled

Inherits:
RuBB::Node show all
Defined in:
lib/rubb/node/styled.rb

Instance Attribute Summary collapse

Attributes inherited from RuBB::Node

#children

Instance Method Summary collapse

Methods inherited from RuBB::Node

#<<

Constructor Details

#initialize(options = {}) ⇒ Styled

Returns a new instance of Styled.



7
8
9
10
11
# File 'lib/rubb/node/styled.rb', line 7

def initialize(options={})
  super(options)
  @style_hash = options[:style_hash] || {}
  @html_tag_name = options[:html_tag_name] || 'span'
end

Instance Attribute Details

#html_tag_nameObject

Returns the value of attribute html_tag_name.



5
6
7
# File 'lib/rubb/node/styled.rb', line 5

def html_tag_name
  @html_tag_name
end

#style_hashObject

Returns the value of attribute style_hash.



4
5
6
# File 'lib/rubb/node/styled.rb', line 4

def style_hash
  @style_hash
end

Instance Method Details

#to_htmlObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/rubb/node/styled.rb', line 13

def to_html
  html = "<#{@html_tag_name} style=\""
  @style_hash.each do |k,v|
    if(v) # if v is not nil
      v = v[/\A([^;]*);/,1] if v.include?(';') # ignore semicolons and any trailing text
      html += "#{k}: #{v};"
    end
  end
  html += '">'
  @children.each do |child|
    html += child ? child.to_html : ''
  end
  html + "</#{@html_tag_name}>"
end