class Rubypivot::HtmlTag

Public Class Methods

new(tag_name, options = {}) click to toggle source
# File lib/rubypivot/html_tag.rb, line 3
def initialize(tag_name, options = {})
  @tag_name = tag_name
  @options = options
  @class_strings = []
end
to_html(tag_name, options = {}) click to toggle source
# File lib/rubypivot/html_tag.rb, line 75
def self.to_html(tag_name, options = {})
  instance = self.new(tag_name, body, options = {})
  instance.build(body)
end

Public Instance Methods

add_class(class_string) click to toggle source
# File lib/rubypivot/html_tag.rb, line 21
def add_class(class_string)
  if class_string
    class_string.to_s.split(/ +/).each do |str|
      next if str.nil? || @class_strings.include?(str)
      @class_strings << str
    end
  end
  self
end
add_key(key, value) click to toggle source
# File lib/rubypivot/html_tag.rb, line 9
def add_key(key, value)
  @options[key] = value
  self
end
build(options = {}) { || ... } click to toggle source
# File lib/rubypivot/html_tag.rb, line 57
def build(options = {})
  res = open
  # res << "\n" unless options[:compact]
  if block_given?
    res << yield.to_s
    # res << "\n" unless options[:compact]
    res << close
    # res << "\n" unless options[:compact]
  elsif options[:body]
    res << options[:body]
    # res << "\n" unless options[:compact]
    res << close
    # res << "\n" unless options[:compact]
  end
  res
end
Also aliased as: to_html
build_class() click to toggle source
# File lib/rubypivot/html_tag.rb, line 31
def build_class
  return '' if @class_strings.empty?
  " class=\"#{@class_strings.join(' ')}\""
end
close() click to toggle source
# File lib/rubypivot/html_tag.rb, line 53
def close
  "</#{@tag_name}>"
end
open() click to toggle source
# File lib/rubypivot/html_tag.rb, line 36
def open
  add_class(@options[:class])
  res = "<#{@tag_name}"
  res << build_class
  @options.each do |key, value|
    next if value.nil?
    case key
    when :class
      # class was handled first
    else
      res << " #{key}=\"#{value}\""
    end
  end
  res << ">"
  res
end
sanitize(value) click to toggle source
# File lib/rubypivot/html_tag.rb, line 14
def sanitize(value)
  return nil unless value
  array = value.split(/ +/)
  array.uniq!
  array.join(" ")
end
to_html(options = {})
Alias for: build