csv: allow all Ruby csv parsing options
Closes #149 Generalization on PR #146 by Qgel. Thanks for the contribution!
This commit is contained in:
@@ -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|
|
||||
|
||||
@@ -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
|
||||
Reference in New Issue
Block a user