Sha256: e860c851aa3e88c8c3baa015fab9bde3fd7f9e9116a2efab33477fda8df32286

Contents?: true

Size: 1.42 KB

Versions: 4

Compression:

Stored size: 1.42 KB

Contents

# effective_csv_importer 1.0

# Creates one rake task per importer model, as well as a `rake import:all` task.

# Usage:

# Put your importer in /lib/csv_importers/posts.rb
# Put your csv data in /lib/csv_importers/data/posts.csv
# Both filenames should be pluralized

# rake import:posts (one task created per model)
# rake import:all

namespace :import do
  Dir['lib/csv_importers/*.rb'].each do |file|
    importer = file.sub('lib/csv_importers/', '').sub('_importer.rb', '')
    csv_file = "lib/csv_importers/data/#{importer}.csv"
    next unless File.exists?(csv_file)

    # Create a rake task to import this file
    desc "Import #{importer} from #{csv_file}"

    task importer => :environment do
      klass = "CsvImporters::#{importer.classify.pluralize}Importer".safe_constantize
      raise "unable to constantize CsvImporters::#{importer.classify.pluralize}Importer for #{file}" unless klass

      klass.new(csv_file).import!
    end
  end
end

# This task is kind of naive, because some imports could be order dependent. Use at your own risk.
namespace :import do
  desc "Import all from /lib/csv_importers/*.rb"

  task :all => :environment do
    Dir['lib/csv_importers/*.rb'].each do |file|
      importer = file.sub('lib/csv_importers/', '').sub('_importer.rb', '')
      csv_file = "lib/csv_importers/data/#{importer}.csv"
      next unless File.exists?(csv_file)

      Rake::Task["import:#{importer}"].invoke
    end
  end

end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
effective_developer-0.0.5 lib/tasks/effective_csv_importer.rake
effective_developer-0.0.4 lib/tasks/effective_csv_importer.rake
effective_developer-0.0.3 lib/tasks/effective_csv_importer.rake
effective_developer-0.0.2 lib/tasks/effective_csv_importer.rake