Class: CsvBuilder::Streamer
- Inherits:
-
Object
- Object
- CsvBuilder::Streamer
- Defined in:
- lib/csv_streamer/template_handler.rb
Instance Method Summary collapse
- #each ⇒ Object
-
#initialize(template_proc) ⇒ Streamer
constructor
A new instance of Streamer.
Constructor Details
#initialize(template_proc) ⇒ Streamer
Returns a new instance of Streamer.
32 33 34 |
# File 'lib/csv_streamer/template_handler.rb', line 32 def initialize(template_proc) @template_proc = template_proc end |
Instance Method Details
#each ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/csv_streamer/template_handler.rb', line 36 def each # The ruby csv class will try to infer a separator to use, if the csv options # do not set it. ruby's csv calls pos, eof?, read, and rewind to check the first line # of the io to infer a separator. Rails' output object does not support these methods # so we provide a mock implementation to satisfy csv. # # See code at https://github.com/ruby/ruby/blob/trunk/lib/csv.rb#L2021 - note that @io points # to the output variable defined by this block. # output.class.send(:define_method, "pos") {return 0 } # output.class.send(:define_method, "eof?") { return true } # output.class.send(:define_method, "rewind") {} # read needs to return a newline, otherwise csv loops indefinitely looking for the end of the first line. # output.class.send(:define_method, "read") {|arg1| return "\\n" } # output = "" # The ruby csv write method requires output to support << for writing. Here we just # delegate the method call to output's write method. # output.class.send(:define_method, "<<") {|arg1| yield arg1} yielder = CsvBuilder::Yielder.new(Proc.new{|data| yield data}) csv_stream = CsvBuilder::CSV_LIB.new(yielder, @csv_options || {}) csv = CsvBuilder::TransliteratingFilter.new(csv_stream, @input_encoding || 'UTF-8', @output_encoding || 'LATIN1') @template_proc.call(csv) end |