Browse Source

Half-finished csv command

dev
Andy Meneely 11 years ago
parent
commit
f60e3721c8
  1. 36
      lib/squib/api/data.rb
  2. 17
      samples/csv.rb
  3. 3
      samples/sample.csv
  4. 12
      spec/api/api_data_spec.rb
  5. 3
      spec/data/csv/basic.csv
  6. 4
      spec/spec_helper.rb

36
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

17
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

3
samples/sample.csv

@ -0,0 +1,3 @@
Type,"Quantity"
Thief,2
Mastermind,1
1 Type Quantity
2 Thief 2
3 Mastermind 1

12
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

3
spec/data/csv/basic.csv

@ -0,0 +1,3 @@
h1,h2
1,2
3,4
1 h1 h2
2 1 2
3 3 4

4
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|

Loading…
Cancel
Save