Sha256: 2c3df42628631ca7cd9a60e4963cd0e4795e39613010223be29a3cb190c39dc1

Contents?: true

Size: 779 Bytes

Versions: 2

Compression:

Stored size: 779 Bytes

Contents

require 'builder'
require 'active_support/inflector'

module Leap
  module XmlSerializer
    def to_xml(options = {})
      options[:indent] ||= 2
      xml = options[:builder] ||= Builder::XmlMarkup.new(options)
      xml.instruct! unless options[:skip_instruct]
      yield xml
      xml.to_s
    end

    def array_to_xml(xml, name, array, singular_name = name.to_s.singularize)
      xml.send name, :type => 'array' do |subblock|
        array.each do |item|
          if item.is_a? Symbol or !item.respond_to?(:to_xml)
            item = item.to_s if item.is_a?(Symbol)
            subblock.send singular_name, item, :type => 'string'
          else
            item.to_xml(:builder => subblock, :skip_instruct => true)
          end
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
leap-0.4.5 lib/leap/xml_serializer.rb
leap-0.4.4 lib/leap/xml_serializer.rb