Sha256: e98216d6bdcd0429edb6732dbf8e390768ed1826d9ca5bb41e61ee4f82fcabe4

Contents?: true

Size: 1.23 KB

Versions: 2

Compression:

Stored size: 1.23 KB

Contents

require "pump/xml/tag"
require "pump/xml/value"
require "pump/xml/tag_array"

module Pump
  class Xml
    attr_reader :root_tag_name, :tag_config, :options

    def initialize(root_tag_name, tag_config, options={})
      @root_tag_name = root_tag_name
      @tag_config    = tag_config
      @options       = options

      compile
    end

    def encode(object)
      Array === object ? encode_array(object) : encode_single(object)
    end

    private

    def compile
      instance_eval(compile_string)
    end

    def compile_string
      <<-EOV
        def encode_single(object)
          "#{Tag.new(root_tag_name, {}, sub_tags, tag_options)}"
        end

        def encode_array(objects)
          "#{TagArray.new(root_tag_name, {}, sub_tags, tag_options)}"
        end
      EOV
    end

    def sub_tags
      tag_config.map do |config|
        tag_name, method_name = config.keys.first, config.values.first
        Tag.new(tag_name, config[:attributes], Value.new(method_name), config)
      end
    end

    def tag_options
      {:instruct => add_instruct?, :extra_indent => options[:extra_indent], :array_root => options[:array_root] }
    end

    def add_instruct?
      options.has_key?(:instruct) ? options[:instruct] : true
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
pump-0.0.3 lib/pump/xml.rb
pump-0.0.2 lib/pump/xml.rb