Skip to content

Instantly share code, notes, and snippets.

@tokland
Forked from regedarek/importer.rb
Last active December 20, 2015 20:59
Show Gist options
  • Save tokland/6194047 to your computer and use it in GitHub Desktop.
Save tokland/6194047 to your computer and use it in GitHub Desktop.
class Importer
CODES = {:header, "00F", :data => "00I"}
def self.import_products(fileobj)
fileobj.lines.each_slice(4).map do |group_lines|
type, *data_lines = group_lines.map(&:strip)
case type
when CODES[:header]
# what to do/return here?
when CODES[:data]
attributes = Hash[[:code, :name, :price].zip(data_lines)]
Product.create!(attributes)
else
fail("Unknown type code: #{type}")
end
end.compact
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment