Skip to content

Instantly share code, notes, and snippets.

@omarzina
Forked from voleinikov/csv_to_hash_array.rake
Created April 28, 2022 12:08
Show Gist options
  • Save omarzina/ccf36b4bbe2008c97eded12d6e0d9645 to your computer and use it in GitHub Desktop.
Save omarzina/ccf36b4bbe2008c97eded12d6e0d9645 to your computer and use it in GitHub Desktop.

Revisions

  1. @voleinikov voleinikov revised this gist May 7, 2015. 1 changed file with 4 additions and 2 deletions.
    6 changes: 4 additions & 2 deletions csv_to_hash_array.rake
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,8 @@
    # Adapted from this StackOverflow answer:
    # Adapted from StackOverflow answers:
    # http://stackoverflow.com/a/8477067/1319740
    # With help from
    # and
    # http://stackoverflow.com/a/18113090/1319740
    # With help from:
    # http://technicalpickles.com/posts/parsing-csv-with-ruby/

    # A small rake task that validates that all required headers are present in a csv
  2. @voleinikov voleinikov revised this gist May 7, 2015. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion csv_to_hash_array.rake
    Original file line number Diff line number Diff line change
    @@ -40,7 +40,8 @@ namespace :csv do
    attrs << Hash[row.headers.zip(row.fields)]
    end

    # You can now pass the attrs array wherever you need -- for example a background job that creates/validates/saves model objects
    # You can now pass the attrs array wherever you need -- for example a background job
    # that creates/validates/saves model objects
    attrs
    end
    end
  3. @voleinikov voleinikov revised this gist May 7, 2015. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion csv_to_hash_array.rake
    Original file line number Diff line number Diff line change
    @@ -28,11 +28,12 @@ namespace :csv do

    # Now we read and transform file

    # First set up any custom converters -- This one turns blanks to nils
    # First set up any custom converters -- This one turns blank row values to nils
    CSV::Converters[:blank_to_nil] = lambda do |field|
    field && field.empty? ? nil : field
    end

    # Then we create and populate our array
    attrs = []

    CSV.foreach(args.csv_file_path, :headers => true, :header_converters => :symbol, :converters => [:all, :blank_to_nil]) do |row|
  4. @voleinikov voleinikov revised this gist May 7, 2015. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion csv_to_hash_array.rake
    Original file line number Diff line number Diff line change
    @@ -12,7 +12,9 @@
    namespace :csv do
    desc 'take a csv file and transform it into an array of hashes with headers as keys'
    task :to_hash, [:csv_file_path] => :environment do |task, args|
    ($stderr.puts "ERROR: Please include file path -- Usage: rake csv:to_hash[path/to/csv/file]" && return) unless args.csv_file_path.present?
    unless args.csv_file_path.present?
    ($stderr.puts "ERROR: Please include file path -- Usage: rake csv:to_hash[path/to/csv/file]" && return)
    end

    # Check the csv to make sure all required headers are present before reading whole file
    required_headers = %i(id) # Add any required csv headers here
  5. @voleinikov voleinikov created this gist May 7, 2015.
    43 changes: 43 additions & 0 deletions csv_to_hash_array.rake
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,43 @@
    # Adapted from this StackOverflow answer:
    # http://stackoverflow.com/a/8477067/1319740
    # With help from
    # http://technicalpickles.com/posts/parsing-csv-with-ruby/

    # A small rake task that validates that all required headers are present in a csv
    # then converts the csv to an array of hashes with column headers as keys mapped
    # to relevant row values.
    #
    # The data can then be passed wherever it is needed.

    namespace :csv do
    desc 'take a csv file and transform it into an array of hashes with headers as keys'
    task :to_hash, [:csv_file_path] => :environment do |task, args|
    ($stderr.puts "ERROR: Please include file path -- Usage: rake csv:to_hash[path/to/csv/file]" && return) unless args.csv_file_path.present?

    # Check the csv to make sure all required headers are present before reading whole file
    required_headers = %i(id) # Add any required csv headers here
    headers = CSV.open(args.csv_file_path, 'r', :headers => true, :header_converters => :symbol) do |csv|
    csv.first.headers
    end

    if (required_headers - headers).any?
    %stderr.puts "ERROR: Please include #{required_headers} headers in your csv file" && return
    end

    # Now we read and transform file

    # First set up any custom converters -- This one turns blanks to nils
    CSV::Converters[:blank_to_nil] = lambda do |field|
    field && field.empty? ? nil : field
    end

    attrs = []

    CSV.foreach(args.csv_file_path, :headers => true, :header_converters => :symbol, :converters => [:all, :blank_to_nil]) do |row|
    attrs << Hash[row.headers.zip(row.fields)]
    end

    # You can now pass the attrs array wherever you need -- for example a background job that creates/validates/saves model objects
    attrs
    end
    end