Sha256: fa5643ec1af24a01fd3a0dacc082b8e7e40c5b9789bd4735cd8c7249fceb7585

Contents?: true

Size: 1.36 KB

Versions: 12

Compression:

Stored size: 1.36 KB

Contents

require 'pump/xml/node'
require 'active_support/core_ext/string/inflections'

module Pump
  class Xml
    class TagArray < Node
      def initialize(name, attributes={}, nodes=[], options={})
        tag = Tag.new(name, attributes, nodes, {
          :level => 1, :extra_indent => options[:extra_indent],
          :xml_key_style => options[:xml_key_style]
        })
        array_root = options[:array_root] || name.to_s.pluralize
        super(array_root, {}, [tag], options)
      end

      def to_s
        if options.has_key?(:static_value)
          "#{prefix}<#{format_name name} type=\\\"array\\\"#{static_value_and_close_tag}"
        else
          "#{prefix}<#{format_name name} type=\\\"array\\\"#{loop_and_close_tag}"
        end
      end

      private

      def prefix
        options[:instruct] ? "#{Tag::INSTRUCT}" : tabs
      end

      def loop_and_close_tag
        "\#{ #{objects_path}.empty? ? \"/>\n\" : \">\n#{tag_loop}#{tabs}</#{format_name name}>\n\" }"
      end

      def static_value_and_close_tag
        return "/>\n" if options[:static_value].nil?
        ">#{options[:static_value]}</#{format_name name}>\n"
      end

      def objects_path
        options[:array_method] ? "object.#{options[:array_method]}" : "objects"
      end

      def tag_loop
        "\#{#{objects_path}.map{|object| \"#{nodes.first}\" }.join('')}"
      end
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
pump-0.11.0 lib/pump/xml/tag_array.rb
pump-0.10.0 lib/pump/xml/tag_array.rb
pump-0.9.1 lib/pump/xml/tag_array.rb
pump-0.9.0 lib/pump/xml/tag_array.rb
pump-0.8.0 lib/pump/xml/tag_array.rb
pump-0.7.0 lib/pump/xml/tag_array.rb
pump-0.6.6 lib/pump/xml/tag_array.rb
pump-0.6.5 lib/pump/xml/tag_array.rb
pump-0.6.4 lib/pump/xml/tag_array.rb
pump-0.6.3 lib/pump/xml/tag_array.rb
pump-0.6.2 lib/pump/xml/tag_array.rb
pump-0.6.1 lib/pump/xml/tag_array.rb