Module: Raw::XmlHelper

Included in:
Glue::XmlBuilder
Defined in:
lib/raw/helper/xml.rb

Overview

A helper mixin for programmatically building XML blocks. – gmosx, INVESTIGATE: is this used or deprecated? ++

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(tag, *args, &block) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/raw/helper/xml.rb', line 11

def method_missing(tag, *args, &block)
  self.class.module_eval <<-"end_eval", __FILE__, __LINE__
    def #{tag}(*args)
      attrs = args.last.is_a?(Hash) ? args.pop : nil

      if block_given?
        start_tag!('#{tag}', attrs)
        yield
        end_tag!('#{tag}')
      elsif (!args.empty?)
        start_tag!('#{tag}', attrs)
        self << args.first 
        end_tag!('#{tag}')
      else
        start_tag!('#{tag}', attrs, false)
        self << ' />'
      end
    end
  end_eval

  self.send(tag, *args, &block)
end

Instance Method Details

#comment!(str) ⇒ Object

Emit a comment.



77
78
79
80
81
# File 'lib/raw/helper/xml.rb', line 77

def comment!(str)
  self << "<!-- #{str} -->" 

  return self
end

#end_tag!(tag) ⇒ Object

Emit the end (closing) tag of an element.



60
61
62
63
64
# File 'lib/raw/helper/xml.rb', line 60

def end_tag!(tag)
  self << "</#{tag}>" 

  return self
end

#processing_instruction!(name, attributes = nil) ⇒ Object Also known as: pi!

Emit a processing instruction.



85
86
87
88
89
90
91
92
93
94
95
# File 'lib/raw/helper/xml.rb', line 85

def processing_instruction!(name, attributes = nil)
  unless attributes
    self << "<?#{name} ?>"
  else
    self << "<?#{name} "
    attributes.each do |a, v|
      self << %[#{a}="#{v}" ]
    end
    self << "?>"
  end
end

#start_tag!(tag, attributes = nil, close = true) ⇒ Object

Emit the start (opening) tag of an element.



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/raw/helper/xml.rb', line 36

def start_tag!(tag, attributes = nil, close = true)
  unless attributes
    if close
      self << "<#{tag}>"
    else
      self << "<#{tag}"
    end
  else
    self << "<#{tag}"
    for name, value in attributes
      if value
        self << %| #{name}="#{value}"|
      else
        self << %| #{name}="1"|
      end
    end
    self << ">" if close
  end

  return self
end

#text!(str) ⇒ Object Also known as: print

Emit a text string.



68
69
70
71
72
# File 'lib/raw/helper/xml.rb', line 68

def text!(str)
  self << str

  return self
end