Class: MakeExportable::Excel

Inherits:
ExportableFormat show all
Defined in:
lib/make_exportable/exportable_formats/excel.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 = []) ⇒ Excel

Returns a new instance of Excel.



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

def initialize(data_set, data_headers=[])
  self.long      = 'Microsoft Excel'
  self.mime_type = 'application/vnd.ms-excel; charset=utf-8;'
  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/excel.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/excel.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
# File 'lib/make_exportable/exportable_formats/excel.rb', line 17

def generate
  generate_header_option(data_headers)
  output = "<table>\n"
  unless data_headers.blank?
    output << "\t<tr>\n"
    output << data_headers.map {|h| "\t\t<th>#{sanitize(h.humanize.titleize)}</th>\n" }.join
    output << "\t</tr>\n"
  end
  data_set.each do |row|
    output << "\t<tr>\n"
    output << row.map {|field| "\t\t<td>#{sanitize(field)}</td>\n"}.join
    output << "\t</tr>\n"
  end
  output << "</table>\n"
  return output
end

#sanitize(value) ⇒ Object



34
35
36
# File 'lib/make_exportable/exportable_formats/excel.rb', line 34

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