Skip to content

Instantly share code, notes, and snippets.

@yukas
Last active September 1, 2015 11:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yukas/f14db6b776cf4859353b to your computer and use it in GitHub Desktop.
Save yukas/f14db6b776cf4859353b to your computer and use it in GitHub Desktop.
require "csv"
class Client
def import
importer = CsvProductImporter.new(params[:file_path])
inventory = Inventory.new(importer)
inventory.import_products
end
end
class Inventory
attr_reader :importer
def initialize(importer)
@importer = importer
end
def import_products
valid_products.each do |product|
# do the work
end
end
private
def valid_products
@valid_products ||= begin
importer.import_products
importer.valid_products
end
end
end
class CsvProductImporter
attr_reader :file_path
attr_reader :valid_products
def initialize(file_path)
@file_path = file_path
@valid_products = []
end
def import_products
CSV.read(file_path, headers: true).each do |row|
@valid_products << row if valid_row?(row)
end
end
private
def valid_row?(row)
row.field("name").present?
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment