Class: TableImporter::CopyAndPaste

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

Constant Summary

Constants inherited from Source

Source::NIL_VALUES, Source::SEPARATORS

Instance Attribute Summary collapse

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.



7
8
9
10
11
12
13
14
15
# File 'lib/table_importer/copy_and_paste.rb', line 7

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
  @remove_nil_values = data[:remove_nil_values] == true
end

Instance Attribute Details

#remove_nil_valuesObject

Returns the value of attribute remove_nil_values.



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

def remove_nil_values
  @remove_nil_values
end

Instance Method Details

#assign_data(content) ⇒ Object



17
18
19
20
21
# File 'lib/table_importer/copy_and_paste.rb', line 17

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



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

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



23
24
25
26
27
# File 'lib/table_importer/copy_and_paste.rb', line 23

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



102
103
104
105
106
107
108
109
110
111
# File 'lib/table_importer/copy_and_paste.rb', line 102

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



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

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



96
97
98
99
100
# File 'lib/table_importer/copy_and_paste.rb', line 96

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



113
114
115
116
117
118
# File 'lib/table_importer/copy_and_paste.rb', line 113

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



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

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

#get_headersObject



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

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

#get_lines(start_point, number_of_lines) ⇒ Object



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

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



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

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



120
121
122
123
124
125
# File 'lib/table_importer/copy_and_paste.rb', line 120

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



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

def get_type
  "copy_and_paste"
end

#remove_whitespace(column) ⇒ Object



89
90
91
92
93
94
# File 'lib/table_importer/copy_and_paste.rb', line 89

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