Class: DataImportService

Inherits:
Object
  • Object
show all
Defined in:
app/models/concerns/data_import_service.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config, sheet_file) ⇒ DataImportService

Returns a new instance of DataImportService.



5
6
7
8
9
10
11
12
13
14
15
16
# File 'app/models/concerns/data_import_service.rb', line 5

def initialize(config, sheet_file)
  if File.extname(sheet_file.path) == '.xls'
    require 'roo-xls'
    xlsx = Roo::Excel.new(sheet_file)
  else
    xlsx = Roo::Excelx.new(sheet_file)
  end
  @sheet = xlsx.sheet(xlsx.sheets[0])
  @config = config

  @column_index = init_header.compact
end

Instance Attribute Details

#column_indexObject (readonly)

Returns the value of attribute column_index.



3
4
5
# File 'app/models/concerns/data_import_service.rb', line 3

def column_index
  @column_index
end

#sheetObject (readonly)

Returns the value of attribute sheet.



3
4
5
# File 'app/models/concerns/data_import_service.rb', line 3

def sheet
  @sheet
end

Instance Method Details

#init_headerObject



18
19
20
21
22
23
24
25
# File 'app/models/concerns/data_import_service.rb', line 18

def init_header
  headers = @config.columns.map { |_, v| v[:header] }
  file_header = @sheet.row(1)

  headers.map do |header|
    file_header.find_index(header)
  end
end

#resultsObject



27
28
29
30
31
32
33
# File 'app/models/concerns/data_import_service.rb', line 27

def results
  results = []
  @sheet.each do |row|
    results << column_index.map { |index| row[index] }
  end
  results
end