lib/xml/mixup.rb in xml-mixup-0.1.9 vs lib/xml/mixup.rb in xml-mixup-0.1.10

- old
+ new

@@ -214,11 +214,11 @@ }.transform_keys(&:to_sym) # now we dispatch based on the name if name == '#comment' # first up, comments - node = doc.create_comment flatten(children, args) + node = doc.create_comment flatten(children, args).to_s # attach it ADJACENT[adj].call node, nodes[adj] elsif name == '#pi' or name == '#processing-instruction' @@ -228,15 +228,16 @@ end target = children[0] content = '' if (c = children[1..children.length]) and c.length > 0 #warn c.inspect - content = flatten(c, args) + content = flatten(c, args).to_s else content = attr.sort.map { |pair| - "#{pair[0].to_s}=\"#{flatten(pair[1], args)}\"" - }.join(' ') + v = flatten(pair[1], args) or next + "#{pair[0].to_s}=\"#{v}\"" + }.compact.join(' ') end node = Nokogiri::XML::ProcessingInstruction.new(doc, target, content) #warn node.inspect, content @@ -290,15 +291,16 @@ # next pull apart the namespaces and ordinary attributes ns = {} at = {} attr.each do |k, v| - v = flatten(v, args) - if (md = /^xmlns(?::(.*))?$/i.match(k.to_s)) + k = k.to_s + v = flatten(v, args) or next + if (md = /^xmlns(?::(.*))?$/i.match(k)) ns[md[1]] = v else - at[k.to_s] = v + at[k] = v end end # now go over the attributes and set any missing namespaces to nil at.keys.each do |k| @@ -543,29 +545,36 @@ ns.keys.sort { |a, b| a.to_s <=> b.to_s }.each do |p| elem.add_namespace((p.nil? ? p : p.to_s), ns[p].to_s) end attr.sort.each do |k, v| - elem[k.to_s] = flatten(v, args) + v = flatten(v, args) or next + elem[k.to_s] = v end elem end - ATOMS = [String, Symbol, Numeric, NilClass, FalseClass, TrueClass].freeze + ATOMS = [String, Symbol, Numeric, FalseClass, TrueClass].freeze # yo dawg def flatten obj, args + return if obj.nil? # early bailout for most likely condition if ATOMS.any? { |x| obj.is_a? x } obj.to_s elsif obj.is_a? Hash - obj.sort.map { |kv| "#{kv[0].to_s}: #{flatten(kv[1], args)}" }.join(' ') + tmp = obj.sort.map do |kv| + v = flatten(kv[1], args) + v.nil? ? nil : "#{kv[0].to_s}: #{v}" + end.compact + tmp.empty? ? nil : tmp.join(' ') elsif obj.respond_to? :call - obj.call(*args) + flatten(obj.call(*args), args) elsif obj.respond_to? :map - obj.map { |x| flatten(x, args) }.join(' ') + tmp = obj.map { |x| flatten(x, args) }.reject { |x| x.nil? || x == '' } + tmp.empty? ? nil : tmp.join(' ') else obj.to_s end end