| Class | ServerSide::XML |
| In: |
lib/serverside/xml.rb
|
| Parent: | Object |
| TAG_LEFT_OPEN | = | '<'.freeze |
| TAG_LEFT_CLOSE | = | '</'.freeze |
| TAG_RIGHT | = | '>'.freeze |
| INSTRUCT_LEFT | = | '<?xml'.freeze |
| INSTRUCT_RIGHT | = | '?>'.freeze |
| INSTRUCT_DEFAULT | = | {:version => "1.0", :encoding => "UTF-8"}.freeze |
# File lib/serverside/xml.rb, line 44
44: def initialize(tag = nil, atts = nil, &block)
45: @doc = ''
46: __open_tag(tag, atts) if tag
47: block.call(self) if block
48: __close_tag(tag) if tag
49: end
# File lib/serverside/xml.rb, line 20
20: def __close_tag(tag)
21: @doc << TAG_LEFT_CLOSE
22: @doc << tag.to_s
23: @doc << TAG_RIGHT
24: end
# File lib/serverside/xml.rb, line 39
39: def __fmt_atts(atts)
40: atts.inject('') {|m, i| m << " #{i[0]}=#{i[1].to_s.inspect}"}
41: end
# File lib/serverside/xml.rb, line 33
33: def __instruct(arg)
34: @doc << INSTRUCT_LEFT
35: @doc << __fmt_atts(arg)
36: @doc << INSTRUCT_RIGHT
37: end
# File lib/serverside/xml.rb, line 13
13: def __open_tag(tag, atts)
14: @doc << TAG_LEFT_OPEN
15: @doc << tag.to_s
16: @doc << __fmt_atts(atts) if atts
17: @doc << TAG_RIGHT
18: end
# File lib/serverside/xml.rb, line 26
26: def __value(value)
27: @doc << value.to_s.html_escape
28: end
# File lib/serverside/xml.rb, line 74
74: def instruct!(atts = nil)
75: __instruct(atts || INSTRUCT_DEFAULT)
76: end
# File lib/serverside/xml.rb, line 51
51: def method_missing(tag, *args, &block)
52: if block
53: __open_tag(tag, args.first)
54: block.call(self)
55: __close_tag(tag)
56: else
57: value, atts = args.pop, args.pop
58: subtags, atts = atts, nil if atts.is_a?(Array)
59: if subtags
60: __open_tag(tag, atts)
61: subtags.each {|k| __send__(k, value[k])}
62: __close_tag(tag)
63: else
64: __open_tag(tag, atts)
65: __value(value)
66: __close_tag(tag)
67: end
68: end
69: self
70: end