mirror of
https://github.com/fspc/BikeShed-1.git
synced 2025-02-28 08:43:23 -05:00
BikeCsvImporter: add dry run
This commit is contained in:
parent
95dbe162f7
commit
0083dd9bca
@ -1,14 +1,15 @@
|
|||||||
require 'csv'
|
require 'csv'
|
||||||
|
|
||||||
class BikeCsvImporter
|
class BikeCsvImporter
|
||||||
attr_reader :file
|
attr_reader :file, :dry_run
|
||||||
|
|
||||||
def initialize(file)
|
def initialize(file, dry_run)
|
||||||
@file = file
|
@file = file
|
||||||
|
@dry_run = !!dry_run
|
||||||
end
|
end
|
||||||
|
|
||||||
def run
|
def run
|
||||||
result = {imported_count: 0, skipped_shop_ids: []}
|
result = {imported_count: 0, skipped_errors: {}}
|
||||||
|
|
||||||
@bike_purpose_cache = {}
|
@bike_purpose_cache = {}
|
||||||
@bike_brand_cache = {}
|
@bike_brand_cache = {}
|
||||||
@ -16,10 +17,11 @@ class BikeCsvImporter
|
|||||||
|
|
||||||
fetch do |bike_hash|
|
fetch do |bike_hash|
|
||||||
bike = import_bike bike_hash
|
bike = import_bike bike_hash
|
||||||
if bike.try :persisted?
|
check_method = dry_run ? :valid? : :persisted?
|
||||||
|
if bike.try check_method
|
||||||
result[:imported_count] += 1
|
result[:imported_count] += 1
|
||||||
else
|
else
|
||||||
result[:skipped_shop_ids].push bike.try(:shop_id) || bike_hash.values.first
|
result[:skipped_errors][bike.try(:shop_id) || bike_hash.values.first] = bike.try(:errors).try(:messages)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -90,7 +92,9 @@ class BikeCsvImporter
|
|||||||
# I realize the log entry stuff is likely complicated and time consuming. At a minimum, the most important columns to import are the following: Velocipede Number, Program, Gone, Make, Model. To avoid creating the log entry for "Gone", we would instead just set 'gone' to true.
|
# I realize the log entry stuff is likely complicated and time consuming. At a minimum, the most important columns to import are the following: Velocipede Number, Program, Gone, Make, Model. To avoid creating the log entry for "Gone", we would instead just set 'gone' to true.
|
||||||
def import_bike(bike_hash)
|
def import_bike(bike_hash)
|
||||||
bike = Bike.new bike_attrs(bike_hash)
|
bike = Bike.new bike_attrs(bike_hash)
|
||||||
#bike.save
|
#bike.save unless dry_run
|
||||||
|
raise 'TODO save' unless dry_run
|
||||||
|
bike
|
||||||
end
|
end
|
||||||
|
|
||||||
def bike_attrs(bike_hash)
|
def bike_attrs(bike_hash)
|
||||||
|
@ -1,11 +1,11 @@
|
|||||||
namespace :import do
|
namespace :import do
|
||||||
namespace :bikes do
|
namespace :bikes do
|
||||||
# Imports bikes info from CSV file
|
# Imports bikes info from CSV file
|
||||||
task :csv, [:file] => :environment do |t, args|
|
task :csv, [:file, :dry_run] => :environment do |t, args|
|
||||||
file = args[:file]
|
file, dry_run = args.values_at :file, :dry_run
|
||||||
next puts "Usage: rake #{t.name}[$csv_file_path]" unless file
|
next puts "Usage: rake #{t.name}[$csv_file_path[,$dry_run=dry]]" unless file
|
||||||
next puts "File #{file} does not exist or is unreachable" unless File.readable? file
|
next puts "File #{file} does not exist or is unreachable" unless File.readable? file
|
||||||
pp BikeCsvImporter.new(file).run
|
pp BikeCsvImporter.new(file, dry_run == 'dry').run
|
||||||
end
|
end
|
||||||
|
|
||||||
# Analyze a single field from CSV file
|
# Analyze a single field from CSV file
|
||||||
|
Loading…
x
Reference in New Issue
Block a user