diff --git a/CHANGELOG.md b/CHANGELOG.md index bce08fd..981fb3b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ Squib follows [semantic versioning](http://semver.org). Features: * Build groups! Simplify the process of building your deck different ways (e.g. one for color, one for PNP). Can be enabled explicitly or via command line. [See our shiny new docs for how these work](http://squib.readthedocs.org). +* The `csv` method now supports a `data` option to read CSV data directly. When set, it overrides the `file` option. Chores: * Switched to `require_relative` internally throughout the codebase to be more pry-friendly (#130) diff --git a/docs/dsl/csv.rst b/docs/dsl/csv.rst index ce06487..9542ef7 100644 --- a/docs/dsl/csv.rst +++ b/docs/dsl/csv.rst @@ -19,7 +19,12 @@ Options file default: ``'deck.csv'`` - the CSV-formatted file to open. Opens relative to the current directory. + 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`` diff --git a/lib/squib/api/data.rb b/lib/squib/api/data.rb index 516435e..42e3a06 100644 --- a/lib/squib/api/data.rb +++ b/lib/squib/api/data.rb @@ -70,9 +70,10 @@ module Squib # @return [Hash] a hash of arrays based on columns in the table # @api public def csv(opts = {}) - file = Args::InputFile.new(file: 'deck.csv').load!(opts).file[0] import = Args::Import.new.load!(opts) - table = CSV.read(file, headers: true, converters: :numeric) + file = Args::InputFile.new(file: 'deck.csv').load!(opts).file[0] + data = opts.key?(:data) ? opts[:data] : File.read(file) + table = CSV.parse(data, headers: true, converters: :numeric) check_duplicate_csv_headers(table) hash = Hash.new table.headers.each do |header| diff --git a/samples/csv_import.rb b/samples/csv_import.rb index df9d7ef..df3ce63 100644 --- a/samples/csv_import.rb +++ b/samples/csv_import.rb @@ -24,3 +24,10 @@ Squib::Deck.new(cards: num_cards) do text str: data['Name'], font: 'Arial 54' 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 diff --git a/spec/api/api_data_spec.rb b/spec/api/api_data_spec.rb index 79b4daf..c6ba017 100644 --- a/spec/api/api_data_spec.rb +++ b/spec/api/api_data_spec.rb @@ -50,6 +50,14 @@ describe Squib::Deck do }) end + it 'loads inline data' do + hash = Squib.csv(data: "h1,h2\n1,2\n3,4") + expect(hash).to eq({ + 'h1' => [1, 3], + 'h2' => [2, 4] + }) + end + end context '#xlsx' do