Module: N::XmlBuilderMixin

Included in:
XhtmlBuilder, XhtmlString, XmlBuilder, XmlString
Defined in:
lib/nitro/builders/xml.rb

Overview

A helper mixin for programmatically building XML blocks.

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



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

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



69
70
71
72
73
# File 'lib/nitro/builders/xml.rb', line 69

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

	return self
end

#end_tag(tag) ⇒ Object



57
58
59
60
61
# File 'lib/nitro/builders/xml.rb', line 57

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

	return self
end

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



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

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



63
64
65
66
67
# File 'lib/nitro/builders/xml.rb', line 63

def text(str)
	self << str

	return self
end