-
-
Save capitalist/49739 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Reporter | |
class << self | |
def run_example_report(person,start_date,end_date) | |
sd_string = start_date.strftime("%Y%m%d") | |
ed_string = end_date.strftime("%Y%m%d") | |
filename = "#{person.name}_report_for_#{sd_string}_#{ed_string}" | |
self.run_report(filename,"EXAMPLE") do |sheet| | |
sheet.add_row [person.name] | |
sheet.add_row ["Make","Model","Year","VIN"] | |
self.process_by_page(Car,"person_id = #{person.id}") do |cars| | |
cars.each do |car| | |
sheet.add_row [car.make,car.model,car.year,car.vin] | |
end | |
end | |
end | |
end | |
end | |
protected | |
class << self | |
def run_report(file_name,title,header_row = nil) | |
name = "#{file_name}_#{DateTime.now.hour}#{DateTime.now.min}.csv" | |
path = File.join(OUTPUT_DIRECTORY,name) | |
file = File.new(path,"w+"); | |
writer = CSV::Writer.create(file) | |
sheet = CsvSheetWrapper.new(writer) | |
sheet.add_row [title] | |
sheet.index = 2 | |
sheet.add_row header_row if header_row | |
yield(sheet) | |
return name | |
end | |
def process_by_page(model,sql_conditions) | |
set = model.find(:all,:conditions=>sql_conditions,:page => {:size => 50}) | |
keep_going = true | |
while keep_going | |
yield(set) | |
GC.start | |
keep_going = set.next_page! | |
end | |
end | |
end | |
end | |
class CsvSheetWrapper | |
attr_accessor :index | |
def initialize(csv_writer) | |
@sheet = csv_writer | |
@index = 0 | |
end | |
def add_row(array) | |
@sheet << array | |
@index += 1 | |
end | |
def add_lines(count) | |
count.times do | |
@sheet << [] | |
end | |
@index += count | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment