Created
April 23, 2011 06:46
-
-
Save gmgent/938419 to your computer and use it in GitHub Desktop.
data_grid_exporter.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class DataGridExporter | |
class << self | |
def export(data_grid) | |
returning(StringIO.new) do |buffer| | |
DataGridExporter.new(data_grid).build_workbook.write buffer | |
end.string | |
end | |
def quantity_unit_map | |
{"dosing_cumulative_max_quantity" => "dosing_cumulative_max_unit" } | |
end | |
def unit_attribute_name(quantity_attr_name, section) | |
DataGridExporter.quantity_unit_map[quantity_attr_name] || "#{section.name.underscore}_unit" | |
end | |
end | |
def initialize(data_grid) | |
@data_grid = DrugRecordDataGridPresenter.new :view => self, :grid => data_grid | |
end | |
def build_workbook | |
configure_formats | |
returning(Spreadsheet::Workbook.new) do |book| | |
sheet = book.create_worksheet :name => "Drug Records" | |
assemble_headers sheet | |
assemble_data_rows sheet | |
end | |
end | |
protected | |
def configure_formats | |
@data_grid.configure_unit_field_formatters do |drug_record, section, attribute_name| | |
quantity = drug_record.send(attribute_name) | |
unit = drug_record.send DataGridExporter.unit_attribute_name(attribute_name, section) | |
"#{quantity} #{unit.try(:abbreviation)}" | |
end | |
end | |
def assemble_headers(sheet) | |
subheader_row = sheet.row(1) | |
header_row = sheet.row(0) | |
header_index = 0 | |
@data_grid.each_header_group do |group| | |
header_row[header_index] = group.heading | |
header_index += group.column_count | |
group.columns.each do |column| | |
subheader_row.push column.heading | |
end | |
end | |
end | |
def assemble_data_rows(sheet) | |
row_index = 2 | |
@data_grid.each_row do |row| | |
data_row = sheet.row(row_index) | |
row.each_column { |column| data_row.push row.value(column) } | |
row_index += 1 | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class DataGridExporter | |
class << self | |
def export(data_grid) | |
returning(StringIO.new) do |buffer| | |
DataGridExporter.new(data_grid).build_workbook.write buffer | |
end.string | |
end | |
def quantity_unit_map | |
{"dosing_cumulative_max_quantity" => "dosing_cumulative_max_unit" } | |
end | |
def unit_attribute_name(quantity_attr_name, section) | |
DataGridExporter.quantity_unit_map[quantity_attr_name] || "#{section.name.underscore}_unit" | |
end | |
end | |
def initialize(data_grid) | |
@data_grid = DrugRecordDataGridPresenter.new :view => self, :grid => data_grid | |
end | |
def build_workbook | |
configure_formats | |
returning(Spreadsheet::Workbook.new) do |book| | |
sheet = book.create_worksheet :name => "Drug Records" | |
assemble_headers sheet | |
assemble_data_rows sheet | |
end | |
end | |
protected | |
def configure_formats | |
@data_grid.configure_unit_field_formatters do |drug_record, section, attribute_name| | |
quantity = drug_record.send(attribute_name) | |
unit = drug_record.send DataGridExporter.unit_attribute_name(attribute_name, section) | |
"#{quantity} #{unit.try(:abbreviation)}" | |
end | |
end | |
def assemble_headers(sheet) | |
subheader_row = sheet.row(1) | |
header_row = sheet.row(0) | |
header_index = 0 | |
@data_grid.each_header_group do |group| | |
header_row[header_index] = group.heading | |
header_index += group.column_count | |
group.columns.each do |column| | |
subheader_row.push column.heading | |
end | |
end | |
end | |
def assemble_data_rows(sheet) | |
row_index = 2 | |
@data_grid.each_row do |row| | |
data_row = sheet.row(row_index) | |
row.each_column { |column| data_row.push row.value(column) } | |
row_index += 1 | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment