lib/csv-hash.rb in csv-hash-0.1.2 vs lib/csv-hash.rb in csv-hash-0.1.3
- old
+ new
@@ -1,16 +1,17 @@
-require 'faster_csv'
+require 'csv'
+
module CSVHash
- module_function
-
+ extend self
+
def from_file file
num = 0
-
+
data = []
columns = []
-
- FasterCSV.foreach(file) do |row|
+
+ CSV.foreach(file) do |row|
num += 1
if num == 1
columns = row.collect {|c| c.strip if c}
next
end
@@ -35,14 +36,14 @@
end
data << hash
end
-
+
return data, columns.compact
end
-
+
def to_string hashes, columns
rows = hashes.collect do |hash|
vals = columns.collect do |col|
sp = col.to_s.split('__')
ret = hash
@@ -50,24 +51,25 @@
puts key unless ret
ret = ret[key]
end
ret
end
-
+
vals
end
-
-
- csv_string = FasterCSV.generate do |csv|
+
+
+ csv_string = CSV.generate do |csv|
csv << columns
rows.each do |val|
csv << val
end
end
end
end
# Pass either a path to a csv file to parse which will return an array of hashes (stringified keys) or pass an array of hashes and an array of column names
+# See readme.rdoc for more detaild information
def CSVHash arg, columns=nil
if arg.is_a?(File)
CSVHash.from_file(arg.path)
elsif arg.is_a?(String)
CSVHash.from_file(arg)
\ No newline at end of file