Class: CSVHasher

Inherits:
Object
  • Object
show all
Defined in:
lib/csv_hasher.rb

Class Method Summary collapse

Class Method Details

.hashify(path_to_csv, options = { original_col_as_keys: false }) ⇒ Object

get arrays of hashes from a CSV file instead of arrays of arrays

Example:

>> CSVHasher.hashify('path/to/csv/file')
=> [{:col_1=> '..', :col_2=> '..'},...]

Arguments:

path_to_csv: (String)
options: (Hash)
         original_col_as_keys: (Boolean)
                                if original_col_as_keys is true then keys are exactly the col headers
                                if original_col_as_keys is false then keys are spidercase symbols from col headers
         keys: (Array) Custom keys for the returned hashes


21
22
23
24
25
26
# File 'lib/csv_hasher.rb', line 21

def self.hashify(path_to_csv, options = { original_col_as_keys: false })
  csv_arrs = CSV.read(path_to_csv)
  keys = csv_keys options, csv_arrs
  start_index = options[:keys].nil? || options[:include_headers] ? 1 : 0
  csv_arrs[start_index..-1].map {|a| Hash[keys.zip(a)] }
end