csv: allow all Ruby csv parsing options

Closes #149

Generalization on PR #146 by Qgel. Thanks for the contribution!
This commit is contained in:
Andy Meneely
2016-03-24 02:23:47 -04:00
parent 080bd7e934
commit d7943da750
6 changed files with 49 additions and 2 deletions
+3 -2
View File
@@ -2,6 +2,7 @@ require 'roo'
require 'csv'
require_relative '../args/input_file'
require_relative '../args/import'
require_relative '../args/csv_opts'
module Squib
@@ -34,8 +35,8 @@ module Squib
import = Args::Import.new.load!(opts)
file = Args::InputFile.new(file: 'deck.csv').load!(opts).file[0]
data = opts.key?(:data) ? opts[:data] : File.read(file)
sep = opts.key?(:sep) ? opts[:sep] : ','
table = CSV.parse(data, headers: true, converters: :numeric, col_sep: sep)
csv_opts = Args::CSV_Opts.new(opts)
table = CSV.parse(data, csv_opts.to_hash)
check_duplicate_csv_headers(table)
hash = Hash.new
table.headers.each do |header|
+25
View File
@@ -0,0 +1,25 @@
require 'csv'
module Squib
# @api private
module Args
class CSV_Opts
def initialize(opts)
opts = opts.keep_if { |k, _v| CSV::DEFAULT_OPTIONS.key? k}
@hash = CSV::DEFAULT_OPTIONS.merge(opts).merge(required)
end
def to_hash
@hash
end
private
def required
{ headers: true, converters: :numeric }
end
end
end
end