Class: TableImporter::CopyAndPaste

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

Constant Summary

Constants inherited from Source

Source::SEPARATORS

Instance Method Summary collapse

Methods inherited from Source

#clean_chunks, #default_headers, #get_sep_count, #sort_separators

Constructor Details

#initialize(data) ⇒ CopyAndPaste

Returns a new instance of CopyAndPaste.



5
6
7
8
9
10
11
12
# File 'lib/table_importer/copy_and_paste.rb', line 5

def initialize(data)
  @data = assign_data(data[:content])
  @column_separator, @record_separator = assign_separators(data[:column_separator], data[:record_separator])
  @headers, @headers_present = assign_headers(data[:headers_present])
  @mapping = data[:user_headers]
  @compulsory_headers = data[:compulsory_headers]
  @delete_empty_columns = @data.length < 50000
end

Instance Method Details

#assign_data(content) ⇒ Object



14
15
16
17
18
# File 'lib/table_importer/copy_and_paste.rb', line 14

def assign_data(content)
  raise TableImporter::EmptyFileImportError.new if content.blank? || content[0..100].gsub(/[^A-Za-z0-9]/, '').blank?
  content.gsub!(/\r\n|\r/, "\n")
  return content
end

#assign_headers(headers_present) ⇒ Object



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

def assign_headers(headers_present)
  headers = headers_present ? get_first_line : get_headers
  return headers, headers_present
end

#assign_separators(col_sep, rec_sep) ⇒ Object



20
21
22
23
24
# File 'lib/table_importer/copy_and_paste.rb', line 20

def assign_separators(col_sep, rec_sep)
  col_sep = SEPARATORS[col_sep.to_sym] if !col_sep.nil?
  rec_sep = SEPARATORS[rec_sep.to_sym] if !rec_sep.nil?
  col_sep, rec_sep = data_conforms_pattern(col_sep, rec_sep)
end

#convert_headers(provided_headers, mapped_headers, headers_present) ⇒ Object



99
100
101
102
103
104
105
106
107
108
# File 'lib/table_importer/copy_and_paste.rb', line 99

def convert_headers(provided_headers, mapped_headers, headers_present)
  new_headers = headers_present ? provided_headers : default_headers
  new_headers = default_headers(new_headers.count)
  mapped_headers.each do |key, value|
    if value.to_i.to_s == value
      new_headers[value.to_i] = key.to_sym
    end
  end
  new_headers
end

#data_conforms_pattern(col_sep, rec_sep) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/table_importer/copy_and_paste.rb', line 26

def data_conforms_pattern(col_sep, rec_sep)
  # Check to see if data is of bcc style
  first_item = @data.split("\n")[1]
  if first_item.present? && first_item.match(/\S@\S/) && !first_item.match(/;|,|\s/)
    rec_sep ||= "\n"
    col_sep ||= " "
  end
  first_item = @data.split(",").first
  if first_item.present? && first_item.match(/\S@\S/)
    if first_item.match(/<(\S+@\S+)/)
      rec_sep ||= ">, "
      col_sep ||= " <"
    end
  end
  return col_sep, rec_sep
end

#get_chunks(chunk_size) ⇒ Object



93
94
95
96
97
# File 'lib/table_importer/copy_and_paste.rb', line 93

def get_chunks(chunk_size)
  @headers = convert_headers(get_first_line, @mapping.present? ? @mapping : @headers, @headers_present)
  lines = get_lines(0, -1).in_groups_of(chunk_size, false)
  clean_chunks(lines, @compulsory_headers)
end

#get_column_separator(first_line = @data) ⇒ Object



110
111
112
113
114
115
# File 'lib/table_importer/copy_and_paste.rb', line 110

def get_column_separator(first_line = @data)
  return @column_separator if !@column_separator.nil? && @column_separator.length > 0
  separators = get_sep_count(first_line)
  separators.reject!{ |sep| sep.keys[0] == @record_separator} if @record_separator != nil
  @column_separator = sort_separators(separators)
end

#get_first_lineObject



48
49
50
# File 'lib/table_importer/copy_and_paste.rb', line 48

def get_first_line
  @data.split(get_record_separator).first.split(get_column_separator).map(&:to_sym)
end

#get_headersObject



56
57
58
59
# File 'lib/table_importer/copy_and_paste.rb', line 56

def get_headers
  return @headers if @headers.present?
  default_headers(100)
end

#get_lines(start_point, number_of_lines) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
# File 'lib/table_importer/copy_and_paste.rb', line 74

def get_lines(start_point, number_of_lines)
  number_of_lines = number_of_lines - 1 if number_of_lines != -1 # -1 means return all lines.
  mapped_lines = []
  get_column_separator
  @data.split(get_record_separator).each do |line|
    split_line = line.split(@column_separator)
    split_line = remove_whitespace(split_line)
    mapped_lines << Hash[@headers.zip split_line]
  end
  mapped_lines[start_point..(start_point+number_of_lines)]
end

#get_preview_lines(start_point = @headers_present ? 1 : 0, end_point = 10) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/table_importer/copy_and_paste.rb', line 61

def get_preview_lines(start_point = @headers_present ? 1 : 0, end_point = 10)
  begin
    lines = clean_chunks([get_lines(start_point, end_point)], {}, @delete_empty_columns)[0][:lines]
    if lines.first.nil?
      get_preview_lines(start_point+10, end_point+10)
    else
      lines[0..7]
    end
  rescue StandardError
    raise TableImporter::EmptyStringImportError.new
  end
end

#get_record_separator(first_line = @data) ⇒ Object



117
118
119
120
121
122
# File 'lib/table_importer/copy_and_paste.rb', line 117

def get_record_separator(first_line = @data)
  return @record_separator if !@record_separator.nil? && @record_separator.length > 0
  separators = get_sep_count(first_line)
  separators.reject!{ |sep| sep.keys[0] == get_column_separator}
  @record_separator = sort_separators(separators)
end

#get_typeObject



52
53
54
# File 'lib/table_importer/copy_and_paste.rb', line 52

def get_type
  "copy_and_paste"
end

#remove_whitespace(column) ⇒ Object



86
87
88
89
90
91
# File 'lib/table_importer/copy_and_paste.rb', line 86

def remove_whitespace(column)
  column.each do |column_item|
    column_item.strip!
  end
  column
end