From 563f800ba7e61643204428cc4e9f829ed7ed53b6 Mon Sep 17 00:00:00 2001 From: Adam Blinkinsop Date: Fri, 17 Mar 2017 11:49:32 -0700 Subject: [PATCH] Add a yaml DSL method. I find the yaml format easier to read than csv (when working entirely in text, at least). This loader works on this array-formatted yaml, but not (for whatever reason, possibly library brokenness) on multi-doc format: ``` - title: Foo rank: 2 - title: Bar rank: 3 ``` --- lib/squib/api/data.rb | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/lib/squib/api/data.rb b/lib/squib/api/data.rb index c26b744..96d727c 100644 --- a/lib/squib/api/data.rb +++ b/lib/squib/api/data.rb @@ -1,5 +1,6 @@ require 'roo' require 'csv' +require 'yaml' require_relative '../args/input_file' require_relative '../args/import' require_relative '../args/csv_opts' @@ -66,6 +67,22 @@ module Squib return explode_quantities(hash, import.explode) end module_function :csv + + # DSL method. See http://squib.readthedocs.io + def yaml(opts = {}) + input = Args::InputFile.new(file: 'deck.yml').load!(opts) + import = Args::Import.new.load!(opts) + s = YAML.load_file(input.file[0]) + data = Squib::DataFrame.new + # Get a universal list of keys to ensure everything is covered. + keys = s.map {|c| c.keys}.flatten.uniq + # Initialize the data frame; why is [] not the default value? + keys.each {|k| data[k] = [] } + # Load all cards into the frame, nil value if key isn't set. + s.each {|card| keys.each {|k| data[k] << card[k] } } + explode_quantities(data, import.explode) + end + module_function :yaml # Check if the given CSV table has duplicate columns, and throw a warning # @api private