class Tablerize::StructuredHtmlElement

A StructuredHtmlElement represents HTML tags enclosing an array of HtmlElements.

Attributes

attrs[R]
children[R]
classes[R]
tag[RW]

Public Class Methods

new(tag, opts = {}) click to toggle source
# File lib/tablerize/html_element.rb, line 24
def initialize(tag, opts = {})
  @tag = tag
  @attrs = {}
  @classes = []
  @children = []
  add_class opts[:class] unless opts[:class].nil?
  @attrs.update opts[:attrs] unless opts[:attrs].nil?
end

Public Instance Methods

add_class(classes) click to toggle source
# File lib/tablerize/html_element.rb, line 33
def add_class(classes)
  if classes.respond_to? :each
    classes.each do |klass|
      add_single_class klass
    end
  else
    add_single_class classes
  end
end
to_html() click to toggle source
# File lib/tablerize/html_element.rb, line 43
def to_html
  "<#{tag}#{attrs_html(@attrs)}>#{inner_html}</#{tag}>"
end

Private Instance Methods

add_single_class(klass) click to toggle source
# File lib/tablerize/html_element.rb, line 49
def add_single_class(klass)
  return if klass.nil? || klass.empty?
  return if @classes.include? klass
  @classes << klass
end
attrs_html(attrs) click to toggle source
# File lib/tablerize/html_element.rb, line 55
def attrs_html(attrs)
  out =  %( class="#{ERB::Util.h @classes.join(' ')}")  if @classes.length > 0
  attrs.each do |attr, value|
    out << %( #{attr}="#{h value}")
  end
  out
end
inner_html() click to toggle source
# File lib/tablerize/html_element.rb, line 63
def inner_html
  @children.map(&:to_html).join('')
end