Module: Rind::Html

Defined in:
lib/rind/html.rb

Overview

Rind::Html will dynamically create any standard (or not) HTML element.

Constant Summary collapse

@@self_closing =
['br','hr','img','input','meta','link']

Class Method Summary collapse

Class Method Details

.const_missing(full_class_name, options = {}) ⇒ Object

:nodoc:



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/rind/html.rb', line 6

def self.const_missing(full_class_name, options={}) # :nodoc:
  klass = Class.new(Element) do
    # <b>Parent:</b> Element
    # === Example
    #  Rind::Html::A.new(
    #    :attributes => {:href => "http://github.com"},
    #    :children   => "GitHub"
    #  )
    def initialize(options={})
      super(options)
    end

    def expanded_name # :nodoc:
      if @namespace_name.nil? or @namespace_name == '' or @namespace_name =~ /^(?:rind:)?html/
        @local_name
      else
        [@namespace_name, @local_name].join(':')
      end
    end

    def to_s # :nodoc:
      attrs = @attributes.collect{|k,v| "#{k}=\"#{v}\""}.join(' ')
      attrs = " #{attrs}" if not attrs.empty?

      if @@self_closing.include? @local_name
        "<#{self.expanded_name}#{attrs} />"
      else
        "<#{self.expanded_name}#{attrs}>#{@children}</#{self.expanded_name}>"
      end
    end
  end
  const_set full_class_name, klass
  klass
end