From f60e3721c80bc8b7ed810f4dc096c15b1cfa8368 Mon Sep 17 00:00:00 2001 From: Andy Meneely Date: Wed, 26 Nov 2014 00:13:45 -0500 Subject: [PATCH] Half-finished csv command --- lib/squib/api/data.rb | 36 +++++++++++++++++++++++++++++++++++- samples/csv.rb | 17 +++++++++++++++++ samples/sample.csv | 3 +++ spec/api/api_data_spec.rb | 12 ++++++++++++ spec/data/csv/basic.csv | 3 +++ spec/spec_helper.rb | 4 ++++ 6 files changed, 74 insertions(+), 1 deletion(-) create mode 100644 samples/csv.rb create mode 100644 samples/sample.csv create mode 100644 spec/api/api_data_spec.rb create mode 100644 spec/data/csv/basic.csv diff --git a/lib/squib/api/data.rb b/lib/squib/api/data.rb index 8f092ee..e22f1d8 100644 --- a/lib/squib/api/data.rb +++ b/lib/squib/api/data.rb @@ -1,4 +1,5 @@ require 'roo' +require 'csv' module Squib @@ -40,12 +41,45 @@ module Squib end#xlsx module_function :xlsx + # Pulls CSV data from `.csv` files into a column-based hash + # + # Pulls the data into a Hash of arrays based on the columns. First row is assumed to be the header row. + # See the example `samples/csv.rb` in the [source repository](https://github.com/andymeneely/squib/tree/master/samples) + # + # @example + # # File data.csv looks like this (without the comment symbols) + # # h1,h2 + # # 1,2 + # # 3,4 + # data = csv file: 'data.csv' + # => {'h1' => [1,3], 'h2' => [2,4]} + # + # Parsing uses Ruby's CSV, options: {headers: true, converters: :numeric} + # http://www.ruby-doc.org/stdlib-2.0/libdoc/csv/rdoc/CSV.html + # + # @option opts file [String] the CSV-formatted file to open. Opens relative to the current directory. + # @return [Hash] a hash of arrays based on columns in the spreadsheet + # @api public + def csv(opts = {}) + opts = Squib::SYSTEM_DEFAULTS.merge(opts) + opts = Squib::InputHelpers.fileify(opts) + hash = {} + csv = CSV.open(opts[:file], headers: true, converters: :numeric).read + + return hash + end + module_function :csv + class Deck - # Convenience call for Squib.xlsx + # Convenience call on deck goes to the module function def xlsx(opts = {}) Squib.xlsx(opts) end + def csv(opts = {}) + Squib.csv(opts) + end + end end diff --git a/samples/csv.rb b/samples/csv.rb new file mode 100644 index 0000000..6491e9f --- /dev/null +++ b/samples/csv.rb @@ -0,0 +1,17 @@ +require 'squib' + +Squib::Deck.new(cards: 2) do + background color: :white + + # Outputs a hash of arrays with the header names as keys + data = csv file: 'sample.csv' + + text str: data['Name'], x: 250, y: 55, font: 'Arial 54' + text str: data['Level'], x: 65, y: 65, font: 'Arial 72' + text str: data['Description'], x: 65, y: 600, font: 'Arial 36' + + # You can also specify the sheet, starting at 0 + data = xlsx file: 'sample.xlsx', sheet: 2 + + save format: :png, prefix: 'sample_excel_' +end diff --git a/samples/sample.csv b/samples/sample.csv new file mode 100644 index 0000000..334d668 --- /dev/null +++ b/samples/sample.csv @@ -0,0 +1,3 @@ +Type,"Quantity" +Thief,2 +Mastermind,1 \ No newline at end of file diff --git a/spec/api/api_data_spec.rb b/spec/api/api_data_spec.rb new file mode 100644 index 0000000..c029630 --- /dev/null +++ b/spec/api/api_data_spec.rb @@ -0,0 +1,12 @@ +require 'spec_helper' + +describe Squib::Deck do + context '#csv' do + # it 'loads basic csv data' do + # expect(Squib.csv(file: csv_file('basic.csv'))).to eq({ + # 'h1' => [1, 3], + # 'h2' => [2, 4] + # }) + # end + end +end \ No newline at end of file diff --git a/spec/data/csv/basic.csv b/spec/data/csv/basic.csv new file mode 100644 index 0000000..4a01598 --- /dev/null +++ b/spec/data/csv/basic.csv @@ -0,0 +1,3 @@ +h1,h2 +1,2 +3,4 \ No newline at end of file diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index d0313b4..1ae67dc 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -27,6 +27,10 @@ def sample_regression_file(file) "#{File.expand_path(File.dirname(__FILE__))}/data/samples/#{file}.txt" end +def csv_file(file) + "#{File.expand_path(File.dirname(__FILE__))}/data/csv/#{file}" +end + def overwrite_sample(sample_name, log) # Use this to overwrite the regression with current state File.open(sample_regression_file(sample_name), 'w+:UTF-8') do |f|