Class: MakeExportable::XML

Inherits:
ExportableFormat show all
Defined in:
lib/make_exportable/exportable_formats/xml.rb

Overview

:nodoc:

Instance Attribute Summary collapse

Attributes inherited from ExportableFormat

#long, #mime_type

Instance Method Summary collapse

Methods inherited from ExportableFormat

#generate_header_option, register_format

Constructor Details

#initialize(data_set, data_headers = []) ⇒ XML

Returns a new instance of XML.



10
11
12
13
14
15
# File 'lib/make_exportable/exportable_formats/xml.rb', line 10

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

Instance Attribute Details

#data_headersObject

Returns the value of attribute data_headers.



8
9
10
# File 'lib/make_exportable/exportable_formats/xml.rb', line 8

def data_headers
  @data_headers
end

#data_setObject

Returns the value of attribute data_set.



8
9
10
# File 'lib/make_exportable/exportable_formats/xml.rb', line 8

def data_set
  @data_set
end

Instance Method Details

#generateObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/make_exportable/exportable_formats/xml.rb', line 17

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

#sanitize(value) ⇒ Object



37
38
39
40
# File 'lib/make_exportable/exportable_formats/xml.rb', line 37

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