Sha256: d4e405796970b54494d4b10e8642a4ec3ff46166f1ea17d7ec0eb522cdf17085
Contents?: true
Size: 1.88 KB
Versions: 1
Compression:
Stored size: 1.88 KB
Contents
require 'cgi' module AlpacaBuildTool ## # XmlNode represents simple xml node that can contain attributes and other # nodes or string as a content class XmlNode attr_reader :name, :content ## # Creates an instance # # +name+:: name of node # +content+:: content of node (if set, block is ignored) # accepts &block to set content to array of nodes def initialize(name, content = nil, &block) @name = name return @content = content unless content.nil? return unless block_given? @arity = block.arity if @arity <= 0 @context = eval('self', block.binding) instance_eval(&block) else yield self end end ## # Adds new node into content # # +name+:: node name # +content+:: node content # accepts &block for this new node # # XmlNode.new 'a' do # node 'b' do # node 'c' do # ... # end # end # end def node(name, content = nil, &block) (@content ||= []) << XmlNode.new(name, content, &block) end ## # Adds attribute to node # # +name+:: attribute name # +value+:: attribute value def attribute(name, value) (@attributes ||= {})[name] = value end ## # Overrides string representation to populate xml pretty formatted content def to_s return "<#{@name}#{attributes_to_s}/>" if @content.nil? content = @content.is_a?(Array) ? array_to_s : CGI.escape_html(@content) "<#{@name}#{attributes_to_s}>#{content}</#{@name}>" end private def attributes_to_s (@attributes || {}).map { |n, v| " #{n}=\"#{v}\"" }.reduce(&:+) end def array_to_s nodes = @content.map { |n| "\t#{n}".gsub(/\n/, "\n\t") } "\n#{nodes.join("\n")}\n" end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
alpacabuildtool-1.0.0 | lib/alpacabuildtool/entities/xml_node.rb |