Sha256: a6f1dba30b6d82a2657b8e3db7a597fd2bff31db41e36ba2f4b787b490d616c2

Contents?: true

Size: 1.77 KB

Versions: 4

Compression:

Stored size: 1.77 KB

Contents

require 'pump/xml/node'

module Pump
  class Xml
    class Tag < Node
      INSTRUCT = "<?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\"?>\n"

      def initialize(*args)
        super
        if value_nodes?
          nodes.first.options = options
        end
      end

      def to_s
        if !value_nodes? || options[:never_nil]
          "#{condition_start}#{open_tag}#{value_and_close_tag}#{condition_end}"
        else
          "#{condition_start}#{open_tag}\#{v = #{nodes.first.plain};''}\#{#{value_and_close_tag_with_nil_check}}#{condition_end}"
        end
      end

      private

      def value_nodes?
        Value === nodes.first
      end

      def open_tag
        "#{prefix}<#{name}#{attributes_string}"
      end

      def prefix
        if level == 0
          options[:instruct] ? INSTRUCT : (tabs)
        else
          "#{tabs}"
        end
      end

      def value_and_close_tag(path=nil)
        value = value_nodes? ? nodes.first.to_s(path) : ("\n" << nodes.map(&:to_s).join)
        ">#{value}#{tabs unless value_nodes?}</#{name}>\n"
      end

      def value_and_close_tag_with_nil_check
        "v.nil? ? \" nil=\\\"true\\\"/>\n\" : \"#{value_and_close_tag('v')}\""
      end

      def attributes_string
        return "" if !attributes || attributes.size == 0
        attributes.inject('') do |str, (key, value)|
          str << " #{key}=\\\"#{value}\\\""
        end
      end

      def condition_start
        "\#{\"" if conditional?
      end

      def condition_end
        return unless conditional?

        if options[:if]
          "\" if object.#{options[:if]} }"
        elsif options[:unless]
          "\" unless object.#{options[:unless]} }"
        end
      end

      def conditional?
        !!(options[:if] || options[:unless])
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
pump-0.5.0 lib/pump/xml/tag.rb
pump-0.4.0 lib/pump/xml/tag.rb
pump-0.3.0 lib/pump/xml/tag.rb
pump-0.2.0 lib/pump/xml/tag.rb