Class: ActsAsTable::CSV::Reader

Inherits:
Reader
  • Object
show all
Defined in:
lib/acts_as_table/csv/reader.rb

Overview

ActsAsTable reader object for comma-separated values (CSV) format.

Instance Method Summary collapse

Constructor Details

#initialize(row_model, input = $stdin, **options) {|reader| ... } ⇒ ActsAsTable::CSV::Reader

Note:

Delegates input stream and options to constructor for +CSV+ object.

Returns a new ActsAsTable reader object for comma-separated values (CSV) format using the given ActsAsTable row model, input stream and options.

Parameters:

  • row_model (ActsAsTable::RowModel)
  • input (IO) (defaults to: $stdin)
  • options (Hash<Symbol, Object>)

Yield Parameters:

Yield Returns:

  • (void)

See Also:

  • CSV.new


17
18
19
20
21
22
23
24
25
26
# File 'lib/acts_as_table/csv/reader.rb', line 17

def initialize(row_model, input = $stdin, **options, &block)
  # @return [Hash<Symbol, Object>]
  csv_options = (options[:csv] || {}).merge({
    headers: false,
  })

  @csv = ::CSV.new(input, **csv_options)

  super(row_model, input, **options, &block)
end

Instance Method Details

#linenoFixnum

Returns the line number of the last row read from the input stream.

Returns:

  • (Fixnum)


31
32
33
# File 'lib/acts_as_table/csv/reader.rb', line 31

def lineno
  @csv.lineno
end

#read_rowObject

Returns a pair, where the first element is the next row or +nil+ if input stream is at end of file and the second element indicates if input stream is at end of file.

Returns:

  • (Object)


38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/acts_as_table/csv/reader.rb', line 38

def read_row
  # @return [Array<#to_s>, nil]
  row = @csv.shift

  if row.nil?
    [row, true]
  else
    row = row.collect(&:to_s).collect(&:strip).collect { |s| s.empty? ? nil : s }

    if row.all?(&:nil?)
      row = nil
    end

    [row, false]
  end
end