class Seahorse::Client::Xml::Builder::XmlDoc
Attributes
target[R]
Public Class Methods
new(options = {})
click to toggle source
@option options [#<<] :target ('') @option options [String] :pad ('') @option options [String] :indent ('')
# File lib/seahorse/client/xml/builder.rb, line 11 def initialize(options = {}) @target = options[:target] || '' @indent = options[:indent] || '' @pad = options[:pad] || '' @end_of_line = @indent == '' ? '' : "\n" end
Public Instance Methods
node(name, *args) { || ... }
click to toggle source
@overload node(name, attributes = {})
Adds a self closing element without any content.
@overload node(name, value, attributes = {})
Adds an element that opens and closes on the same line with simple text content.
@overload node(name, attributes = {}, &block)
Adds a wrapping element. Calling {#node} from inside the yielded block creates nested elements.
@return [void]
# File lib/seahorse/client/xml/builder.rb, line 33 def node(name, *args, &block) attrs = args.last.is_a?(Hash) ? args.pop : {} if block_given? @target << open_el(name, attrs) @target << @end_of_line increase_pad { yield } @target << @pad @target << close_el(name) elsif args.empty? @target << empty_element(name, attrs) else @target << inline_element(name, args.first, attrs) end end
Private Instance Methods
attributes(attr)
click to toggle source
# File lib/seahorse/client/xml/builder.rb, line 70 def attributes(attr) if attr.empty? '' else ' ' + attr.map do |key, value| "#{key}=\"#{escape(value).gsub('"', '"')}\"" end.join(' ') end end
close_el(name)
click to toggle source
# File lib/seahorse/client/xml/builder.rb, line 62 def close_el(name) "</#{name}>#{@end_of_line}" end
empty_element(name, attrs)
click to toggle source
# File lib/seahorse/client/xml/builder.rb, line 50 def empty_element(name, attrs) "#{@pad}<#{name}#{attributes(attrs)}/>#{@end_of_line}" end
escape(string)
click to toggle source
# File lib/seahorse/client/xml/builder.rb, line 66 def escape(string) string.to_s end
increase_pad() { || ... }
click to toggle source
# File lib/seahorse/client/xml/builder.rb, line 80 def increase_pad(&block) pre_increase = @pad @pad = @pad + @indent yield @pad = pre_increase end
inline_element(name, value, attrs)
click to toggle source
# File lib/seahorse/client/xml/builder.rb, line 54 def inline_element(name, value, attrs) "#{open_el(name, attrs)}#{escape(value)}#{close_el(name)}" end
open_el(name, attrs)
click to toggle source
# File lib/seahorse/client/xml/builder.rb, line 58 def open_el(name, attrs) "#{@pad}<#{name}#{attributes(attrs)}>" end