Skip to content

Instantly share code, notes, and snippets.

@bastosmichael
Created March 10, 2014 21:42
Show Gist options
  • Save bastosmichael/9475022 to your computer and use it in GitHub Desktop.
Save bastosmichael/9475022 to your computer and use it in GitHub Desktop.
Simple Ruby CSV Model
require 'csv'
class SomethingCSV
def initialize(io, relation)
@target = io
@relation = relation
@headers = [ "Name",
"SomeValue",
"AnotherValue"
]
end
def export
csv = CSV.new(@target)
csv << @headers
@relation.find_in_batches do | somethings|
somethings.each do |something|
csv << row_for(something)
end
csv.flush
end
end
def row_for(something)
[ something.name,
something.some_value,
something.another_value
]
end
end
qa_name = 'something.csv'
out_file = File.open(File.join(path,name), 'wb')
csv = SomethingCSV.new(out_file,self.somethings)
csv.export
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment