Class: TableImporter::Source

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

Direct Known Subclasses

CSV, CopyAndPaste, RooSpreadsheetSource

Constant Summary collapse

NIL_VALUES =
%w( NULL null nil undefined )
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.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/table_importer/source.rb', line 14

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



83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/table_importer/source.rb', line 83

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



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

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



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

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

#get_column_separator(first_line = "") ⇒ Object



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

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

#get_headersObject



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

def get_headers
  @source.get_headers
end

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



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

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

#get_preview_linesObject



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

def get_preview_lines
  @source.get_preview_lines
end

#get_record_separator(first_line = "") ⇒ Object



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

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

#get_sep_count(first_line) ⇒ Object



65
66
67
68
69
# File 'lib/table_importer/source.rb', line 65

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

#get_typeObject



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

def get_type
  @source.get_type
end

#sort_separators(separators) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
# File 'lib/table_importer/source.rb', line 71

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