csv

Pulls CSV data from .csv files into a hash of arrays keyed by the headers. First row is assumed to be the header row.

Parsing uses Ruby’s CSV, with options {headers: true, converters: :numeric} http://www.ruby-doc.org/stdlib-2.0/libdoc/csv/rdoc/CSV.html

The csv method is a member of Squib::Deck, but it is also available outside of the Deck DSL with Squib.csv(). This allows a construction like:

data = Squib.csv file: 'data.csv'
Squib::Deck.new(cards: data['name'].size) do
end

Options

file

default: 'deck.csv'

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

When true, strips leading and trailing whitespace on values and headers

explode

default: 'qty'

Quantity explosion will be applied to the column this name. For example, rows in the csv with a 'qty' of 3 will be duplicated 3 times.

col_sep

default: ','

Column separator. One of the CSV custom options in Ruby. See next option below.

CSV custom options in Ruby standard lib.
All of the options in Ruby’s std lib version of CSV are supported except headers is always true and converters is always set to :numeric. See the Ruby Docs for information on the options.

Individual Pre-processing

The xlsx method also takes in a block that will be executed for each cell in your data. This is useful for processing individual cells, like putting a dollar sign in front of dollars, or converting from a float to an integer. The value of the block will be what is assigned to that cell. For example:

resource_data = Squib.csv(file: 'sample.xlsx') do |header, value|
  case header
  when 'Cost'
    "$#{value}k" # e.g. "3" becomes "$3k"
  else
    value # always return the original value if you didn't do anything to it
  end
end

Examples

To get the sample Excel files, go to its source

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
require 'squib'

Squib::Deck.new(cards: 2) do
  background color: :white

  # Outputs a hash of arrays with the header names as keys
  data = csv file: 'sample.csv'
  text str: data['Type'], x: 250, y: 55, font: 'Arial 18'
  text str: data['Level'], x: 65, y: 65, font: 'Arial 24'

  save format: :png, prefix: 'sample_csv_'

  # You can also specify the sheet, starting at 0
  data = xlsx file: 'sample.xlsx', sheet: 2
end

# CSV is also a Squib-module-level function, so this also works:
data      = Squib.csv file: 'quantity_explosion.csv' # 2 rows...
num_cards = data['Name'].size                        #          ...but 4 cards!

Squib::Deck.new(cards: num_cards) do
  background color: :white
  rect # card border
  text str: data['Name'], font: 'Arial 18'
  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

Here’s the sample.csv

1
2
3
Type,"Level"
Thief,1
Mastermind,2

Here’s the quantity_explosion.csv

1
2
3
Name,Qty
Basilisk,3
High Templar,1