Skip to content

Instantly share code, notes, and snippets.

@awd
Created October 12, 2010 19:50
Show Gist options
  • Select an option

  • Save awd/622791 to your computer and use it in GitHub Desktop.

Select an option

Save awd/622791 to your computer and use it in GitHub Desktop.
class ProgressiveContent
# additionally you could pass in arguments
# to help initialize a specific scope
def initialize
@generated = false
@test_response = false
end
# this method is used in the CSV Responder
# you could dynamically produce a filename here
def to_filename
"example.csv"
end
# our testing showed that this method is called on 3 separate occasions
# we could not determine why this happens, so we introduced guard clauses
def each(&block)
# first time each() is called
if (@test_response == false)
@test_response = true
return
end
# if we have already generated content, return, otherwise generate
return if @generated
# generate a CSV header
yield generate_header()
# generate the CSV content
# in our production code, we have this method being called in a X.times {} cycle
# where the block passes the iteration number into the method, calculates and returns data
# X.times { |t| generate_content(t) }
yield generate_content()
# generate a CSV footer
yield generate_footer()
# this lets the renderer know we are done
yield "\r\n"
@generated = true
end
# without this method, yielding a string
# Cache-Control is not sent as specified in our custom responder
def all?
yield "generated-string"
end
private
def generate_header
csv_header = ["Col1", "Col2", "Col3", "Col4"]
return FasterCSV.generate_line(csv_header)
end
def generate_content
# do something here, returning the output
# this method could be called inside of an iterative loop
# producing calculated results each time it is called
end
def generate_footer
csv_footer = ["end", "end", "end", "end"]
return FasterCSV.generate_line(csv_footer)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment