Class: Labimotion::ExportDataset

Inherits:
Object
  • Object
show all
Defined in:
lib/labimotion/libs/export_dataset.rb

Overview

ExportDataset

Constant Summary collapse

DEFAULT_ROW_WIDTH =
100
DEFAULT_ROW_HEIGHT =
20

Instance Method Summary collapse

Constructor Details

#initialize(id) ⇒ ExportDataset

Returns a new instance of ExportDataset.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/labimotion/libs/export_dataset.rb', line 11

def initialize(id)
  @xfile = Axlsx::Package.new
  @xfile.workbook.styles.fonts.first.name = 'Calibri'
  @file_extension = 'xlsx'

  @id = id
  @dataset = Labimotion::Dataset.find_by(element_id: @id, element_type: 'Container')
  return if @dataset.nil?

  @klass = @dataset.dataset_klass
  @ols_term_id = @klass.ols_term_id
  @label = @klass.label
  @analysis = @dataset&.element&.parent
  @element = @analysis&.root&.containable if @analysis.present?
  @element_type = @element.class.name if @element.present?
end

Instance Method Details

#exportObject



72
73
74
75
76
77
78
79
80
81
# File 'lib/labimotion/libs/export_dataset.rb', line 72

def export
  return if @dataset.nil? || @analysis.nil? || @element.nil?

  description
  dataset_info
  # element_info
  chemwiki_info
rescue StandardError => e
  Labimotion.log_exception(e)
end

#readObject



28
29
30
# File 'lib/labimotion/libs/export_dataset.rb', line 28

def read
  @xfile.to_stream.read
end

#res_nameObject



32
33
34
35
36
37
38
# File 'lib/labimotion/libs/export_dataset.rb', line 32

def res_name
  element_name = Container.find(@id)&.root_element&.short_label
  ols = ols_name
  "#{element_name}_#{ols.gsub(' ', '_')}.xlsx"
rescue StandardError => e
  Labimotion.log_exception(e)
end

#spectraObject



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/labimotion/libs/export_dataset.rb', line 40

def spectra
  name_mapping = []
  ## gds = Labimotion::Dataset.find_by(element_id: id, element_type: 'Container')
  cds = Container.find(@id)
  cds_csv = cds.attachments.where(aasm_state: 'csv').order(:filename)
  csv_length = cds_csv.length
  return if csv_length.zero?

  cds_csv.each_with_index do |att, idx|
    sheet_name = "Sheet#{idx+1}"
    sheet = @xfile.workbook.add_worksheet(name: sheet_name)
    name_mapping.push([sheet_name, att.filename])
    File.open(att.attachment_url) do |fi|
      fi.each_line do |line|
        sheet.add_row(line.split(','))
      end
    end
  end
  return unless name_mapping.length > 1

  first_sheet = @xfile.workbook.worksheets&.first
  header_style = first_sheet&.styles.add_style(sz: 12, fg_color: 'FFFFFF', bg_color: '00008B', border: { style: :thick, color: 'FF777777', edges: [:bottom] })
  first_sheet&.add_row(['Sheet name', 'File name'], style: header_style)
  name_mapping&.each do |mapping|
    next if mapping.length < 2

    @xfile.workbook.worksheets&.first&.add_row([mapping[0].to_s, mapping[1].to_s])
  end
rescue StandardError => e
  Labimotion.log_exception(e)
end