Half-finished csv command
parent
6579c86719
commit
f60e3721c8
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
Type,"Quantity"
|
||||
Thief,2
|
||||
Mastermind,1
|
||||
|
|
|
@ -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
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
h1,h2
|
||||
1,2
|
||||
3,4
|
||||
|
|
|
@ -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…
Reference in New Issue