Class: TableImporter::Source

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

Direct Known Subclasses

CSV, CopyAndPaste, RooSpreadsheetSource

Constant Summary collapse

SEPARATORS =
{comma: ",", space: " ", tab: "\t", newline_mac: "\n", semicolon: ";", newline_windows: "\r\n", old_newline_mac: "\r"}

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ Source

Returns a new instance of Source.



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

def initialize (data)
  case data[:type]
  when 'copy_and_paste'
    @source = CopyAndPaste.new(data)
  when 'csv'
    @source = CSV.new(data)
  when 'xls', 'xlsx'
    @source = Excel.new(data)
  when 'google'
    @source = Google.new(data)
  else
    raise TableImporter::IncorrectFileError.new
  end
  @source
end

Instance Method Details

#clean_chunks(chunks, compulsory_headers = {}, delete_empty_columns = false) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/table_importer/source.rb', line 78

def clean_chunks(chunks, compulsory_headers = {}, delete_empty_columns = false)
  result = []
  empty_headers = chunks.first.first.keys
  chunks.each do |chunk|
    new_chunk, empty_headers = process_line(chunk, empty_headers, compulsory_headers, delete_empty_columns)
    result << new_chunk unless new_chunk[:lines] == [] && new_chunk[:errors] == []
  end
  if delete_empty_columns
    remove_empty_columns(result, empty_headers)
  end
  result
end

#default_headers(number = 100) ⇒ Object



53
54
55
56
57
58
# File 'lib/table_importer/source.rb', line 53

def default_headers(number = 100)
  return @default_headers if @default_headers
  @default_headers = 1.upto(number).collect do |n|
    "column_#{n}".to_sym
  end
end

#get_chunks(chunk_size = 50) ⇒ Object



49
50
51
# File 'lib/table_importer/source.rb', line 49

def get_chunks(chunk_size = 50)
  @source.get_chunks(chunk_size)
end

#get_column_separator(first_line = "") ⇒ Object



29
30
31
# File 'lib/table_importer/source.rb', line 29

def get_column_separator(first_line = "")
  SEPARATORS.key(@source.get_column_separator(first_line))
end

#get_headersObject



37
38
39
# File 'lib/table_importer/source.rb', line 37

def get_headers
  @source.get_headers
end

#get_lines(start_point = 0, number = -1)) ⇒ Object



41
42
43
# File 'lib/table_importer/source.rb', line 41

def get_lines(start_point = 0, number = -1)
  @source.get_lines(start_point, number)
end

#get_preview_linesObject



45
46
47
# File 'lib/table_importer/source.rb', line 45

def get_preview_lines
  @source.get_preview_lines
end

#get_record_separator(first_line = "") ⇒ Object



33
34
35
# File 'lib/table_importer/source.rb', line 33

def get_record_separator(first_line = "")
  SEPARATORS.key(@source.get_record_separator(first_line))
end

#get_sep_count(first_line) ⇒ Object



60
61
62
63
64
# File 'lib/table_importer/source.rb', line 60

def get_sep_count(first_line)
  SEPARATORS.values.collect do |sep|
    {sep => first_line.scan(sep).count}
  end
end

#get_typeObject



25
26
27
# File 'lib/table_importer/source.rb', line 25

def get_type
  @source.get_type
end

#sort_separators(separators) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
# File 'lib/table_importer/source.rb', line 66

def sort_separators(separators)
  highest_value = 0
  highest_key = ""
  separators.each do |sep|
    if sep.values[0] >= highest_value
      highest_value = sep.values[0]
      highest_key = sep.keys[0]
    end
  end
  highest_key
end