Class: Builder::XmlMarkup
- Inherits:
-
Object
- Object
- Builder::XmlMarkup
show all
- Defined in:
- lib/cgi-spa/builder.rb,
lib/cgi-spa/builder.rb
Instance Method Summary
collapse
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(sym, *args, &block) ⇒ Object
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
|
# File 'lib/cgi-spa/builder.rb', line 59
def method_missing(sym, *args, &block)
text = nil
attrs = nil
sym = "#{sym}:#{args.shift}" if args.first.kind_of?(Symbol)
args.each do |arg|
case arg
when Hash
attrs ||= {}
attrs.merge!(arg)
else
text ||= ''
text << arg.to_s
end
end
if block
unless text.nil?
raise ArgumentError, "XmlMarkup cannot mix a text argument with a block"
end
_indent
_start_tag(sym, attrs)
_newline
begin
_nested_structures(block)
ensure
_indent
_end_tag(sym)
_newline
end
elsif text.nil?
_indent
_start_tag(sym, attrs, true)
_newline
else
_indent
_start_tag(sym, attrs)
text! text
_end_tag(sym)
_newline
end
@target
end
|
Instance Method Details
#indented_data!(data) ⇒ Object
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
# File 'lib/cgi-spa/builder.rb', line 11
def indented_data!(data)
return if data.strip.length == 0
lines = data.gsub(/\n\s*\n/,"\n")
unindent = lines.scan(/^ */).map {|str| str.length}.min
before = Regexp.new('^'.ljust(unindent+1))
after = " " * (@level * @indent)
data = data.gsub(before, after)
if block_given?
yield data
else
self << data
end
_newline unless data =~ /\n\Z/
end
|
#indented_text!(text) ⇒ Object
5
6
7
|
# File 'lib/cgi-spa/builder.rb', line 5
def indented_text!(text)
indented_data!(text) {|data| text! data}
end
|
#traceback!(exception = $!, klass = 'traceback') ⇒ Object
31
32
33
34
35
36
37
|
# File 'lib/cgi-spa/builder.rb', line 31
def traceback!(exception=$!, klass='traceback')
pre :class=>klass do
text! exception.inspect
_newline
exception.backtrace.each {|frame| text!((' '*@indent)+frame + "\n")}
end
end
|