Sha256: e4eb95366a2e2134da2c101ea71d9de27b47e122f4a3e710bee1ea32127f47ec

Contents?: true

Size: 972 Bytes

Versions: 5

Compression:

Stored size: 972 Bytes

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]})
        array_root = options[:array_root] || name.to_s.pluralize
        super(array_root, {}, [tag], options)
      end

      def to_s
        "#{prefix}<#{name} type=\\\"array\\\"#{loop_and_close_tag}"
      end

      private

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

      def loop_and_close_tag
        "\#{ #{objects_path}.empty? ? \"/>\n\" : \">\n#{tag_loop}#{tabs}</#{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

5 entries across 5 versions & 1 rubygems

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