yaml¶
Pulls deck data from a YAML files into a Squib::DataFrame
(essentially a hash of arrays).
Parsing uses Ruby’s built-in Yaml package.
The yaml
method is a member of Squib::Deck
, but it is also available outside of the Deck DSL with Squib.yaml()
. This allows a construction like:
data = Squib.yaml file: 'data.yml'
Squib::Deck.new(cards: data['name'].size) do
end
The Yaml file format assumes that the entire deck is an array, then each element of the array is a hash. Every key encountered in that hash will translate to a “column” in the data frame. If a key exists in one card and not in another, then it defaults to nil
.
Warning
Case matters in your Yaml keys.
Options¶
- file
default:
'deck.yml'
the YAML-formatted file to open. Opens relative to the current directory. If
data
is set, this option is overridden.- data
default:
nil
when set, method will parse this Yaml data instead of reading the file.
- 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.
Warning
Data import methods such as xlsx
and csv
will not consult your layout file or follow the Squib Thinks in Arrays feature.
Individual Pre-processing¶
The yaml
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.yaml(file: 'sample.yaml') 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 Yaml files, go to its source
1 2 3 4 5 6 7 8 9 10 11 12 | require 'squib'
Squib::Deck.new(cards: 2) do
background color: :white
# Outputs a hash of arrays with the header names as keys
data = yaml file: 'sample.yaml'
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_yaml_'
end
|
Here’s the sample.yaml
1 2 3 4 5 | - Type: Thief
Level: 1
- Type: Mastermind
Level: 2
|