Class: Htmlrb::Tag

Inherits:
Object
  • Object
show all
Defined in:
lib/htmlrb/tag.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTag



9
10
11
# File 'lib/htmlrb/tag.rb', line 9

def initialize
  @html_parts = []
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, content_or_options = "", options = {}, &block) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/htmlrb/tag.rb', line 17

def method_missing(method_name, content_or_options = "", options = {}, &block)
  options = content_or_options if content_or_options.is_a?(Hash)
  elm_name = method_name.to_s.gsub('_', '-')

  @html_parts << "<#{elm_name}"

  @html_parts << " " unless options.empty?
  @html_parts << options.map do |key, val|
    attr_name = key.to_s.gsub('_', '-')
    "#{attr_name}=\"#{CGI.escapeHTML(val)}\""
  end.join(" ")

  @html_parts << ">"

  if block
    content = block.call(self)
    @html_parts << CGI.escapeHTML(content) if content
  elsif content_or_options.is_a?(String)
    @html_parts << CGI.escapeHTML(content_or_options)
  end

  if block || content_or_options.is_a?(String)
    @html_parts << "</#{elm_name}>"
  end

  nil
end

Instance Attribute Details

#html_partsObject (readonly)

Returns the value of attribute html_parts.



7
8
9
# File 'lib/htmlrb/tag.rb', line 7

def html_parts
  @html_parts
end

Instance Method Details

#doctypeObject



13
14
15
# File 'lib/htmlrb/tag.rb', line 13

def doctype
  @html_parts << "<!DOCTYPE html>"
end