class CSVUtils::CSVOptions
Constants
- BYTE_ORDER_MARKS
this list is from en.wikipedia.org/wiki/Byte_order_mark
- COL_SEPARATORS
- ROW_SEPARATORS
Attributes
byte_order_mark[R]
col_separator[R]
columns[R]
encoding[R]
row_separator[R]
Public Class Methods
new(io)
click to toggle source
# File lib/csv_utils/csv_options.rb, line 34 def initialize(io) line = if io.is_a?(String) File.open(io, 'rb', &:readline) else io.readline end @col_separator = auto_detect_col_sep(line) @row_separator = auto_detect_row_sep(line) @byte_order_mark = get_byte_order_mark(line) @encoding = get_character_encoding(@byte_order_mark) @columns = get_number_of_columns(line) if @col_separator end
Public Instance Methods
auto_detect_col_sep(line)
click to toggle source
# File lib/csv_utils/csv_options.rb, line 55 def auto_detect_col_sep(line) COL_SEPARATORS.detect { |sep| line.include?(sep) } end
auto_detect_row_sep(line)
click to toggle source
# File lib/csv_utils/csv_options.rb, line 59 def auto_detect_row_sep(line) ROW_SEPARATORS.detect { |sep| line.include?(sep) } end
get_byte_order_mark(line)
click to toggle source
# File lib/csv_utils/csv_options.rb, line 73 def get_byte_order_mark(line) BYTE_ORDER_MARKS.keys.detect do |bom| line =~ /\A#{bom}/ end end
get_character_encoding(bom)
click to toggle source
# File lib/csv_utils/csv_options.rb, line 79 def get_character_encoding(bom) BYTE_ORDER_MARKS[bom] || 'UTF-8' end
get_headers(line)
click to toggle source
# File lib/csv_utils/csv_options.rb, line 63 def get_headers(line) headers = line.split(col_separator) headers[0] = strip_byte_order_marks(headers[0]) headers end
get_number_of_columns(line)
click to toggle source
# File lib/csv_utils/csv_options.rb, line 69 def get_number_of_columns(line) get_headers(line).size end
strip_byte_order_marks(header)
click to toggle source
# File lib/csv_utils/csv_options.rb, line 83 def strip_byte_order_marks(header) @byte_order_marks ? header.sub(@byte_order_marks, '') : header end
valid?()
click to toggle source
# File lib/csv_utils/csv_options.rb, line 49 def valid? return false if @col_separator.nil? || @row_separator.nil? true end