class Rhb::Builder

Constants

VOID_ELEMENTS

Public Class Methods

new() click to toggle source
# File lib/rhb/builder.rb, line 8
def initialize
  @buffer = ''
end

Public Instance Methods

doctype() click to toggle source
# File lib/rhb/builder.rb, line 17
def doctype
  @buffer << '<!DOCTYPE html>'
end
to_html()
Alias for: to_s
to_s() click to toggle source
# File lib/rhb/builder.rb, line 12
def to_s
  @buffer
end
Also aliased as: to_html

Private Instance Methods

cache_element(name) click to toggle source
# File lib/rhb/builder.rb, line 35
    def cache_element name
      self.class.class_eval <<-CODE, __FILE__, __LINE__ + 1
        def #{name} *args, &block
          element '#{name}', *args, #{void_element? name}, &block
        end
      CODE
    end
element(name, *args, void, &block) click to toggle source
# File lib/rhb/builder.rb, line 23
def element name, *args, void, &block
  attrs = html_attrs extract_options! args
  @buffer << "<#{name}#{' ' unless attrs.empty?}#{attrs}>"
  return if void
  if args.empty? and block_given?
    instance_exec &block
  else
    @buffer << args.first.to_s
  end
  @buffer << "</#{name}>"
end
extract_options!(array) click to toggle source
# File lib/rhb/builder.rb, line 53
def extract_options! array
  return [] unless array.last.kind_of? Hash
  array.pop
end
html_attrs(attrs, prefix = '') click to toggle source
# File lib/rhb/builder.rb, line 43
def html_attrs attrs, prefix = ''
  attrs.each_with_object('') do |(name, value), result|
    result << if value.kind_of? Hash
      html_attrs value, "#{prefix}#{name}-"
    else
      %Q{#{prefix}#{name}="#{value}" }
    end
  end.strip
end
method_missing(name, *args, &block) click to toggle source
# File lib/rhb/builder.rb, line 62
def method_missing name, *args, &block
  cache_element name
  public_send name, *args, &block
end
void_element?(name) click to toggle source
# File lib/rhb/builder.rb, line 58
def void_element? name
  VOID_ELEMENTS.include? name.to_s
end