csv: data option

Close #137
dev
Andy Meneely 2016-03-15 17:45:39 -04:00
parent e51b99af85
commit f408b7a88f
5 changed files with 25 additions and 3 deletions

View File

@ -5,6 +5,7 @@ Squib follows [semantic versioning](http://semver.org).
Features: Features:
* Build groups! Simplify the process of building your deck different ways (e.g. one for color, one for PNP). Can be enabled explicitly or via command line. [See our shiny new docs for how these work](http://squib.readthedocs.org). * Build groups! Simplify the process of building your deck different ways (e.g. one for color, one for PNP). Can be enabled explicitly or via command line. [See our shiny new docs for how these work](http://squib.readthedocs.org).
* The `csv` method now supports a `data` option to read CSV data directly. When set, it overrides the `file` option.
Chores: Chores:
* Switched to `require_relative` internally throughout the codebase to be more pry-friendly (#130) * Switched to `require_relative` internally throughout the codebase to be more pry-friendly (#130)

View File

@ -19,7 +19,12 @@ Options
file file
default: ``'deck.csv'`` default: ``'deck.csv'``
the CSV-formatted file to open. Opens relative to the current directory. the CSV-formatted file to open. Opens relative to the current directory. If ``data`` is set, this option is overridden.
data
default: ``nil``
when set, CSV will parse this data instead of reading the file.
strip strip
default: ``true`` default: ``true``

View File

@ -70,9 +70,10 @@ module Squib
# @return [Hash] a hash of arrays based on columns in the table # @return [Hash] a hash of arrays based on columns in the table
# @api public # @api public
def csv(opts = {}) def csv(opts = {})
file = Args::InputFile.new(file: 'deck.csv').load!(opts).file[0]
import = Args::Import.new.load!(opts) import = Args::Import.new.load!(opts)
table = CSV.read(file, headers: true, converters: :numeric) file = Args::InputFile.new(file: 'deck.csv').load!(opts).file[0]
data = opts.key?(:data) ? opts[:data] : File.read(file)
table = CSV.parse(data, headers: true, converters: :numeric)
check_duplicate_csv_headers(table) check_duplicate_csv_headers(table)
hash = Hash.new hash = Hash.new
table.headers.each do |header| table.headers.each do |header|

View File

@ -24,3 +24,10 @@ Squib::Deck.new(cards: num_cards) do
text str: data['Name'], font: 'Arial 54' text str: data['Name'], font: 'Arial 54'
save_sheet prefix: 'sample_csv_qty_', columns: 4 save_sheet prefix: 'sample_csv_qty_', columns: 4
end end
# Additionally, CSV supports inline data specifically
data = Squib.csv data: <<-EOCSV
Name,Cost
Knight,3
Orc,1
EOCSV

View File

@ -50,6 +50,14 @@ describe Squib::Deck do
}) })
end end
it 'loads inline data' do
hash = Squib.csv(data: "h1,h2\n1,2\n3,4")
expect(hash).to eq({
'h1' => [1, 3],
'h2' => [2, 4]
})
end
end end
context '#xlsx' do context '#xlsx' do