Class: TableImporter::Source

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

Direct Known Subclasses

CSV, CopyAndPaste, Excel, Google

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.



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

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



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

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



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

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



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

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

#get_column_separator(first_line = "") ⇒ Object



27
28
29
# File 'lib/table_importer/source.rb', line 27

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

#get_headersObject



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

def get_headers
  @source.get_headers
end

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



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

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

#get_preview_linesObject



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

def get_preview_lines
  @source.get_preview_lines
end

#get_record_separator(first_line = "") ⇒ Object



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

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

#get_sep_count(first_line) ⇒ Object



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

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

#get_typeObject



23
24
25
# File 'lib/table_importer/source.rb', line 23

def get_type
  @source.get_type
end

#sort_separators(separators) ⇒ Object



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

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