Sha256: ae5a5b03128e5c75104e95d5fa30327f39c0a1fbf603ada4f99b4f4cb39373a5

Contents?: true

Size: 1.11 KB

Versions: 4

Compression:

Stored size: 1.11 KB

Contents

module MakeExportable #:nodoc:
  class XML < ExportableFormat #:nodoc:

    self.reference = :xml
    self.name      = 'XML'
    self.register_format

    attr_accessor :data_set, :data_headers

    def initialize(data_set, data_headers=[])
      self.long      = 'XML'
      self.mime_type = 'application/xml;'
      self.data_set = data_set
      self.data_headers = data_headers
    end

    def generate
      generate_header_option(data_headers)
      xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
      xml << "<records>\n"
      data_set.each do |row|
        xml << "\t<record>\n"
        row.each_with_index do |field,i|
          if !data_headers.blank?
            attr_name = sanitize(data_headers[i].dasherize)
          else
            attr_name = "attribute_#{i}"
          end
          xml << "\t\t<#{attr_name}>#{sanitize(field)}</#{attr_name}>\n"
        end
        xml << "\t</record>\n"
      end
      xml << "</records>\n"
      return xml
    end

    def sanitize(value)
      value.gsub(/&/, '&amp;').gsub(/</, '&lt;').gsub(/>/, '&gt;').
        gsub(/"/, '&quot;').gsub(/'/, '&apos;')
    end
  end

end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
make_exportable-1.1.0 lib/make_exportable/exportable_formats/xml.rb
make_exportable-1.0.3 lib/make_exportable/exportable_formats/xml.rb
make_exportable-1.0.2 lib/make_exportable/exportable_formats/xml.rb
make_exportable-1.0.1 lib/make_exportable/exportable_formats/xml.rb