lib/squib/api/data.rb in squib-0.13.4 vs lib/squib/api/data.rb in squib-0.14.beta1

- old
+ new

@@ -1,7 +1,8 @@ require 'roo' require 'csv' +require 'yaml' require_relative '../args/input_file' require_relative '../args/import' require_relative '../args/csv_opts' require_relative '../import/data_frame' @@ -65,10 +66,34 @@ end 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) + yml = YAML.load_file(input.file[0]) + data = Squib::DataFrame.new + # Get a universal list of keys to ensure everything is covered. + keys = yml.map { |c| c.keys}.flatten.uniq + keys.each { |k| data[k] = [] } #init arrays + yml.each do |card| + # nil value if key isn't set. + keys.each { |k| data[k] << card[k] } + end + if block_given? + data.each do |header, col| + col.map! do |val| + yield(header, val) + end + end + end + explode_quantities(data, import.explode) + end + module_function :yaml + # Check if the given CSV table has duplicate columns, and throw a warning # @api private def check_duplicate_csv_headers(table) if table.headers.size != table.headers.uniq.size dups = table.headers.select{|e| table.headers.count(e) > 1 } @@ -102,7 +127,11 @@ # DSL method. See http://squib.readthedocs.io def csv(opts = {}) Squib.csv(opts) end + # DSL method. See http://squib.readthedocs.io + def yaml(opts = {}) + Squib.yaml(opts) + end end end