Class: ActsAsTable::CSV::Reader
- Inherits:
-
Reader
- Object
- Reader
- ActsAsTable::CSV::Reader
- Defined in:
- lib/acts_as_table/csv/reader.rb
Overview
ActsAsTable reader object for comma-separated values (CSV) format.
Instance Method Summary collapse
-
#initialize(row_model, input = $stdin, **options) {|reader| ... } ⇒ ActsAsTable::CSV::Reader
constructor
Returns a new ActsAsTable reader object for comma-separated values (CSV) format using the given ActsAsTable row model, input stream and options.
-
#lineno ⇒ Fixnum
Returns the line number of the last row read from the input stream.
-
#read_row ⇒ Object
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.
Constructor Details
#initialize(row_model, input = $stdin, **options) {|reader| ... } ⇒ ActsAsTable::CSV::Reader
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.
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, **, &block) # @return [Hash<Symbol, Object>] = ([:csv] || {}).merge({ headers: false, }) @csv = ::CSV.new(input, **) super(row_model, input, **, &block) end |
Instance Method Details
#lineno ⇒ Fixnum
Returns the line number of the last row read from the input stream.
31 32 33 |
# File 'lib/acts_as_table/csv/reader.rb', line 31 def lineno @csv.lineno end |
#read_row ⇒ Object
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.
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 |