Class: TableImporter::Source
- Inherits:
-
Object
- Object
- TableImporter::Source
- Defined in:
- lib/table_importer/source.rb
Overview
require ‘import_sources/string_source’
Direct Known Subclasses
Constant Summary collapse
- SEPARATORS =
{comma: ",", space: " ", tab: "\t", newline_mac: "\n", semicolon: ";", pipe: "|", newline_windows: "\r\n", old_newline_mac: "\r"}
Instance Method Summary collapse
- #clean_chunks(chunks, compulsory_headers = {}, delete_empty_columns = false) ⇒ Object
- #default_headers(number = 100) ⇒ Object
- #get_chunks(chunk_size = 50) ⇒ Object
- #get_column_separator(first_line = "") ⇒ Object
- #get_headers ⇒ Object
- #get_lines(start_point = 0, number = -1)) ⇒ Object
- #get_preview_lines ⇒ Object
- #get_record_separator(first_line = "") ⇒ Object
- #get_sep_count(first_line) ⇒ Object
- #get_type ⇒ Object
-
#initialize(data) ⇒ Source
constructor
A new instance of Source.
- #sort_separators(separators) ⇒ Object
Constructor Details
#initialize(data) ⇒ Source
Returns a new instance of Source.
12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/table_importer/source.rb', line 12 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) else raise Exceptions::IncorrectFileError.new end @source end |
Instance Method Details
#clean_chunks(chunks, compulsory_headers = {}, delete_empty_columns = false) ⇒ Object
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/table_importer/source.rb', line 79 def clean_chunks(chunks, compulsory_headers = {}, delete_empty_columns = false) result = [] empty_headers = chunks.first.first.keys chunks.each do |chunk| new_chunk = { :lines => [], :errors => []} chunk.each_with_index do |line, index| line_empty = line_empty?(line) no_compulsory_headers, missing_header = check_compulsory_headers?(line, compulsory_headers) if line_empty || no_compulsory_headers new_chunk[:errors] << format_error(line, line_empty, no_compulsory_headers, compulsory_headers, missing_header) else if delete_empty_columns line.each do |key, value| if value.present? && value.to_s.gsub(/[^A-Za-z0-9]/, '').present? empty_headers.delete(key) end end end new_chunk[:lines] << line end end 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
54 55 56 57 58 59 |
# File 'lib/table_importer/source.rb', line 54 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
50 51 52 |
# File 'lib/table_importer/source.rb', line 50 def get_chunks(chunk_size = 50) @source.get_chunks(chunk_size) end |
#get_column_separator(first_line = "") ⇒ Object
30 31 32 |
# File 'lib/table_importer/source.rb', line 30 def get_column_separator(first_line = "") SEPARATORS.key(@source.get_column_separator(first_line)) end |
#get_headers ⇒ Object
38 39 40 |
# File 'lib/table_importer/source.rb', line 38 def get_headers @source.get_headers end |
#get_lines(start_point = 0, number = -1)) ⇒ Object
42 43 44 |
# File 'lib/table_importer/source.rb', line 42 def get_lines(start_point = 0, number = -1) @source.get_lines(start_point, number) end |
#get_preview_lines ⇒ Object
46 47 48 |
# File 'lib/table_importer/source.rb', line 46 def get_preview_lines @source.get_preview_lines end |
#get_record_separator(first_line = "") ⇒ Object
34 35 36 |
# File 'lib/table_importer/source.rb', line 34 def get_record_separator(first_line = "") SEPARATORS.key(@source.get_record_separator(first_line)) end |
#get_sep_count(first_line) ⇒ Object
61 62 63 64 65 |
# File 'lib/table_importer/source.rb', line 61 def get_sep_count(first_line) SEPARATORS.values.collect do |sep| {sep => first_line.scan(sep).count} end end |
#get_type ⇒ Object
26 27 28 |
# File 'lib/table_importer/source.rb', line 26 def get_type @source.get_type end |
#sort_separators(separators) ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/table_importer/source.rb', line 67 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 |