Class: TableImporter::RooSpreadsheetSource

Inherits:
Source
  • Object
show all
Defined in:
lib/table_importer/roo_spreadsheet_source.rb

Direct Known Subclasses

Excel, Google

Constant Summary

Constants inherited from Source

Source::SEPARATORS

Instance Method Summary collapse

Methods inherited from Source

#clean_chunks, #default_headers, #get_column_separator, #get_record_separator, #get_sep_count, #get_type, #initialize, #sort_separators

Constructor Details

This class inherits a constructor from TableImporter::Source

Instance Method Details

#convert_headersObject



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/table_importer/roo_spreadsheet_source.rb', line 35

def convert_headers
  new_headers = @headers_present ? @file.row(1) : default_headers
  new_headers = default_headers(new_headers.count)
  return new_headers unless @mapping
  @mapping.each do |key, value|
    if value.to_i.to_s == value.to_s
      new_headers[value.to_i] = key.to_sym
    end
  end
  new_headers
end

#get_chunks(chunk_size) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/table_importer/roo_spreadsheet_source.rb', line 47

def get_chunks(chunk_size)
  @headers = convert_headers
  @last_row ||= @file.last_row
  chunks = []
  start_point = @headers_present ? 2 : 1
  while chunks.count <= @last_row/chunk_size
    chunks << get_lines(start_point, chunk_size)
    start_point += chunk_size
  end
  chunks.last << Hash[@headers.zip(@file.row(@last_row))]
  clean_chunks(chunks, @compulsory_headers)
end

#get_headersObject



5
6
7
# File 'lib/table_importer/roo_spreadsheet_source.rb', line 5

def get_headers
  @headers
end

#get_lines(start, number_of_lines, preview_lines = false) ⇒ Object



24
25
26
27
28
29
30
31
32
33
# File 'lib/table_importer/roo_spreadsheet_source.rb', line 24

def get_lines(start, number_of_lines, preview_lines = false)
  @last_row ||= @file.last_row
  finish = [@last_row, start + number_of_lines].min
  mapped_lines = []
  range = preview_lines ? (start..finish) : (start...finish)
  range.each do |row_number|
    mapped_lines << Hash[@headers.zip(@file.row(row_number))]
  end
  mapped_lines
end

#get_preview_lines(start_point = 1, end_point = 10) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/table_importer/roo_spreadsheet_source.rb', line 9

def get_preview_lines(start_point = 1, end_point = 10)
  begin
    @headers = @mapping.present? && @mapping != false ? convert_headers : @headers
    start_point += 1 if @headers_present == true
    lines = clean_chunks([get_lines(start_point, end_point, true)], @compulsory_headers)[0][:lines]
    if lines.first.nil?
      get_preview_lines(start_point+10, end_point+10)
    else
      lines[0..8]
    end
  rescue SystemStackError, NoMethodError
    raise TableImporter::EmptyFileImportError.new
  end
end