Browse Source

csv: data option

Close #137
dev
Andy Meneely 10 years ago
parent
commit
f408b7a88f
  1. 1
      CHANGELOG.md
  2. 7
      docs/dsl/csv.rst
  3. 5
      lib/squib/api/data.rb
  4. 7
      samples/csv_import.rb
  5. 8
      spec/api/api_data_spec.rb

1
CHANGELOG.md

@ -5,6 +5,7 @@ Squib follows [semantic versioning](http://semver.org).
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).
* The `csv` method now supports a `data` option to read CSV data directly. When set, it overrides the `file` option.
Chores:
* Switched to `require_relative` internally throughout the codebase to be more pry-friendly (#130)

7
docs/dsl/csv.rst

@ -19,7 +19,12 @@ Options
file
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
default: ``true``

5
lib/squib/api/data.rb

@ -70,9 +70,10 @@ module Squib
# @return [Hash] a hash of arrays based on columns in the table
# @api public
def csv(opts = {})
file = Args::InputFile.new(file: 'deck.csv').load!(opts).file[0]
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)
hash = Hash.new
table.headers.each do |header|

7
samples/csv_import.rb

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

8
spec/api/api_data_spec.rb

@ -50,6 +50,14 @@ describe Squib::Deck do
})
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
context '#xlsx' do

Loading…
Cancel
Save