Class: CsvBuilder::Yielder

Inherits:
Object
  • Object
show all
Defined in:
lib/csv_builder/template_handler.rb

Overview

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 github.com/ruby/ruby/blob/trunk/lib/csv.rb#L2021 - note that @io points to an object of this class.

Instance Method Summary collapse

Constructor Details

#initialize(yielder) ⇒ Yielder

Returns a new instance of Yielder.



40
41
42
# File 'lib/csv_builder/template_handler.rb', line 40

def initialize(yielder)
  @yielder = yielder
end

Instance Method Details

#<<(data) ⇒ Object

this is the method that ultimately yields to the block with output. the block is passed by Rails into the Streamer class’ each method. Streamer provides a Proc to this class, which simply invokes yield from within the context of the each block.



68
69
70
# File 'lib/csv_builder/template_handler.rb', line 68

def <<(data)
  @yielder.call data
end

#eof?Boolean

always indicate that we have reached the end of the file

Returns:

  • (Boolean)


50
51
52
# File 'lib/csv_builder/template_handler.rb', line 50

def eof?
  return true
end

#posObject

always indicate that we are at the start of the io stream



45
46
47
# File 'lib/csv_builder/template_handler.rb', line 45

def pos
  return 0
end

#read(arg1) ⇒ Object

despite indicating that we have no data with pos and eof, we still need to return a newline otherwise CSV will enter an infinite loop with read.



60
61
62
# File 'lib/csv_builder/template_handler.rb', line 60

def read(arg1)
  return "\n"
end

#rewindObject

do nothing, we haven’t moved forward



55
56
# File 'lib/csv_builder/template_handler.rb', line 55

def rewind
end