Class: SampleManifestExcel::Upload::Data

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Model, Enumerable, Converters
Defined in:
app/sample_manifest_excel/sample_manifest_excel/upload/data.rb

Overview

An object to store the data for a particular manifest upload. The data is split into a header row (column names) and the actual data.

Constant Summary

Constants included from Converters

Converters::BLANK_CHARS

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Converters

#strip_all_blanks

Constructor Details

#initialize(file, start_row) ⇒ Data

The file is opened as a Roo spreadsheet. If it is valid it is split by the start row. Start row of column headers and data put into separate rows.


22
23
24
25
26
27
28
29
30
31
32
33
# File 'app/sample_manifest_excel/sample_manifest_excel/upload/data.rb', line 22

def initialize(file, start_row)
  @file = file
  @start_row = start_row
  if valid?
    @sheet = Roo::Spreadsheet.open(file).sheet(0)
    @header_row = sheet.row(start_row)
    @data = sheet.drop(start_row)
  else
    @header_row = []
    @data = []
  end
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data


13
14
15
# File 'app/sample_manifest_excel/sample_manifest_excel/upload/data.rb', line 13

def data
  @data
end

#fileObject (readonly)

Returns the value of attribute file


13
14
15
# File 'app/sample_manifest_excel/sample_manifest_excel/upload/data.rb', line 13

def file
  @file
end

#header_rowObject (readonly)

Returns the value of attribute header_row


13
14
15
# File 'app/sample_manifest_excel/sample_manifest_excel/upload/data.rb', line 13

def header_row
  @header_row
end

#sheetObject (readonly)

Returns the value of attribute sheet


13
14
15
# File 'app/sample_manifest_excel/sample_manifest_excel/upload/data.rb', line 13

def sheet
  @sheet
end

#start_rowObject (readonly)

Returns the value of attribute start_row


13
14
15
# File 'app/sample_manifest_excel/sample_manifest_excel/upload/data.rb', line 13

def start_row
  @start_row
end

Instance Method Details

#cell(row, column) ⇒ Object

Find a cell of data based on the column and row


47
48
49
50
# File 'app/sample_manifest_excel/sample_manifest_excel/upload/data.rb', line 47

def cell(row, column)
  val = data.try(:fetch, row - 1).try(:fetch, column - 1)
  strip_all_blanks(val)
end

#column(col_num) ⇒ Object

Return a column of data for a particular column number


54
55
56
57
58
59
# File 'app/sample_manifest_excel/sample_manifest_excel/upload/data.rb', line 54

def column(col_num)
  data.map do |row|
    val = row[col_num - 1]
    strip_all_blanks(val)
  end
end

#each(&block) ⇒ Object


35
36
37
# File 'app/sample_manifest_excel/sample_manifest_excel/upload/data.rb', line 35

def each(&block)
  data.each(&block)
end

#file_extensionObject


39
40
41
42
43
# File 'app/sample_manifest_excel/sample_manifest_excel/upload/data.rb', line 39

def file_extension
  return nil if file.nil?

  File.extname(file.path)
end

#inspectObject


61
62
63
# File 'app/sample_manifest_excel/sample_manifest_excel/upload/data.rb', line 61

def inspect
  "<#{self.class}: @header_row=#{header_row}, @data=#{data}, @start_row=#{start_row}, @file=#{file}>"
end