Half-finished csv command

This commit is contained in:
Andy Meneely
2014-11-26 00:13:45 -05:00
parent 6579c86719
commit f60e3721c8
6 changed files with 74 additions and 1 deletions
+35 -1
View File
@@ -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