+18
-4
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user