lib/squib/api/data.rb in squib-0.11.0 vs lib/squib/api/data.rb in squib-0.12.0
- old
+ new
@@ -1,20 +1,21 @@
require 'roo'
require 'csv'
require_relative '../args/input_file'
require_relative '../args/import'
require_relative '../args/csv_opts'
+require_relative '../import/data_frame'
module Squib
# DSL method. See http://squib.readthedocs.io
def xlsx(opts = {})
input = Args::InputFile.new(file: 'deck.xlsx').load!(opts)
import = Args::Import.new.load!(opts)
s = Roo::Excelx.new(input.file[0])
s.default_sheet = s.sheets[input.sheet[0]]
- data = {}
+ data = Squib::DataFrame.new
s.first_column.upto(s.last_column) do |col|
header = s.cell(s.first_row, col).to_s
header.strip! if import.strip?
data[header] = []
(s.first_row + 1).upto(s.last_row) do |row|
@@ -37,18 +38,18 @@
file = Args::InputFile.new(file: 'deck.csv').load!(opts).file[0]
data = opts.key?(:data) ? opts[:data] : File.read(file)
csv_opts = Args::CSV_Opts.new(opts)
table = CSV.parse(data, csv_opts.to_hash)
check_duplicate_csv_headers(table)
- hash = Hash.new
+ hash = Squib::DataFrame.new
table.headers.each do |header|
new_header = header.to_s
new_header = new_header.strip if import.strip?
hash[new_header] ||= table[header]
end
if import.strip?
- new_hash = Hash.new
+ new_hash = Squib::DataFrame.new
hash.each do |header, col|
new_hash[header] = col.map do |str|
str = str.strip if str.respond_to?(:strip)
str
end
@@ -76,12 +77,12 @@
end
module_function :check_duplicate_csv_headers
# @api private
def explode_quantities(data, qty)
- return data unless data.key? qty.to_s.strip
+ return data unless data.col? qty.to_s.strip
qtys = data[qty]
- new_data = {}
+ new_data = Squib::DataFrame.new
data.each do |col, arr|
new_data[col] = []
qtys.each_with_index do |qty, index|
qty.to_i.times { new_data[col] << arr[index] }
end