Add quantity explosion to xlsx and csv

Closes #78
This commit is contained in:
Andy Meneely
2015-10-20 18:23:25 -04:00
parent dd1148d68f
commit 08381b86f0
12 changed files with 528 additions and 6 deletions
+18 -4
View File
@@ -22,7 +22,7 @@ module Squib
# @option opts file [String] the file to open. Must end in `.xlsx`. Opens relative to the current directory.
# @option opts sheet [Integer] (0) The zero-based index of the sheet from which to read.
# @option opts strip [Boolean] (true) When true, strips leading and trailing whitespace on values and headers
# @option opts qty_header [String] ('qty']) Quantity explosion will be applied to the column this name
# @option opts explode [String] ('qty') Quantity explosion will be applied to the column this name. See README for example.
# @return [Hash] a hash of arrays based on columns in the spreadsheet
# @api public
def xlsx(opts = {})
@@ -44,7 +44,7 @@ module Squib
data[header] << cell
end#row
end#col
data
explode_quantities(data, import.explode)
end#xlsx
module_function :xlsx
@@ -66,7 +66,7 @@ module Squib
#
# @option opts file [String] the CSV-formatted file to open. Opens relative to the current directory.
# @option opts strip [Boolean] (true) When true, strips leading and trailing whitespace on values and headers
# @option opts qty_header [String] ('qty']) Quantity explosion will be applied to the column this name
# @option opts explode [String] ('qty') Quantity explosion will be applied to the column this name. See README for example.
# @return [Hash] a hash of arrays based on columns in the table
# @api public
def csv(opts = {})
@@ -87,7 +87,7 @@ module Squib
end
hash = new_hash
end
return hash
return explode_quantities(hash, import.explode)
end
module_function :csv
@@ -101,6 +101,20 @@ module Squib
end
module_function :check_duplicate_csv_headers
def explode_quantities(data, qty)
return data unless data.key? qty.to_s.strip
qtys = data[qty]
new_data = {}
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
end
return new_data
end
module_function :explode_quantities
class Deck
# Convenience call on deck goes to the module function
+7 -1
View File
@@ -8,7 +8,9 @@ module Squib
include ArgLoader
def self.parameters
{ strip: true }
{ strip: true,
explode: 'Qty'
}
end
def self.expanding_parameters
@@ -24,6 +26,10 @@ module Squib
arg
end
def validate_explode(arg)
arg
end
def strip?
strip
end