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