Class: TableImporter::CopyAndPaste
- Inherits:
-
Source
- Object
- Source
- TableImporter::CopyAndPaste
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
Returns a new instance of CopyAndPaste.
5
6
7
8
9
10
11
|
# 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 = (data[:headers], data[:headers_present])
@compulsory_headers = data[:compulsory_headers]
@delete_empty_columns = @data.length < 50000
end
|
Instance Method Details
#assign_data(content) ⇒ Object
13
14
15
16
17
|
# File 'lib/table_importer/copy_and_paste.rb', line 13
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
|
42
43
44
45
|
# File 'lib/table_importer/copy_and_paste.rb', line 42
def (, )
= ? get_first_line : if .blank?
return ,
end
|
#assign_separators(col_sep, rec_sep) ⇒ Object
19
20
21
22
23
|
# File 'lib/table_importer/copy_and_paste.rb', line 19
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
|
98
99
100
101
102
103
104
105
106
107
|
# File 'lib/table_importer/copy_and_paste.rb', line 98
def (, , )
= ? :
= (.count)
.each do |key, value|
if value.to_i.to_s == value
[value.to_i] = key.to_sym
end
end
end
|
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
# File 'lib/table_importer/copy_and_paste.rb', line 25
def data_conforms_pattern(col_sep, rec_sep)
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
92
93
94
95
96
|
# File 'lib/table_importer/copy_and_paste.rb', line 92
def get_chunks(chunk_size)
@headers = (get_first_line, @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
109
110
111
112
113
114
|
# File 'lib/table_importer/copy_and_paste.rb', line 109
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_line ⇒ Object
47
48
49
|
# File 'lib/table_importer/copy_and_paste.rb', line 47
def get_first_line
@data.split(get_record_separator).first.split(get_column_separator).map(&:to_sym)
end
|
55
56
57
58
|
# File 'lib/table_importer/copy_and_paste.rb', line 55
def
return @headers if @headers.present?
(100)
end
|
#get_lines(start_point, number_of_lines) ⇒ Object
73
74
75
76
77
78
79
80
81
82
83
|
# File 'lib/table_importer/copy_and_paste.rb', line 73
def get_lines(start_point, number_of_lines)
number_of_lines = number_of_lines - 1 if number_of_lines != -1 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
60
61
62
63
64
65
66
67
68
69
70
71
|
# File 'lib/table_importer/copy_and_paste.rb', line 60
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
116
117
118
119
120
121
|
# File 'lib/table_importer/copy_and_paste.rb', line 116
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_type ⇒ Object
51
52
53
|
# File 'lib/table_importer/copy_and_paste.rb', line 51
def get_type
"copy_and_paste"
end
|
#remove_whitespace(column) ⇒ Object
85
86
87
88
89
90
|
# File 'lib/table_importer/copy_and_paste.rb', line 85
def remove_whitespace(column)
column.each do |column_item|
column_item.strip!
end
column
end
|