Class: Eco::CSV::Stream
- Includes:
- Language::AuxiliarLogger
- Defined in:
- lib/eco/csv/stream.rb
Constant Summary collapse
- CSV_PARAMS =
%i[ col_sep row_sep quote_char headers skip_blanks skip_lines nil_value empty_value converters unconverted_fields return_headers header_converters liberal_parsing field_size_limit ].freeze
Instance Attribute Summary collapse
-
#filename ⇒ Object
readonly
Returns the value of attribute filename.
Attributes included from Language::AuxiliarLogger
Instance Method Summary collapse
- #eof? ⇒ Boolean
- #for_each(start_at_idx: 0) ⇒ Object
-
#initialize(filename, **kargs) ⇒ Stream
constructor
A new instance of Stream.
- #move_to_idx(start_at_idx) ⇒ Object
- #shift ⇒ Object
- #started? ⇒ Boolean
Methods included from Language::AuxiliarLogger
Constructor Details
#initialize(filename, **kargs) ⇒ Stream
Returns a new instance of Stream.
18 19 20 21 22 23 24 25 26 27 |
# File 'lib/eco/csv/stream.rb', line 18 def initialize(filename, **kargs) raise ArgumentError, "File '#{filename}' does not exist" unless ::File.exist?(filename) @filename = filename @params = { headers: true, skip_blanks: true }.merge(kargs) init end |
Instance Attribute Details
#filename ⇒ Object (readonly)
Returns the value of attribute filename.
16 17 18 |
# File 'lib/eco/csv/stream.rb', line 16 def filename @filename end |
Instance Method Details
#eof? ⇒ Boolean
29 30 31 |
# File 'lib/eco/csv/stream.rb', line 29 def eof? started? && !row end |
#for_each(start_at_idx: 0) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/eco/csv/stream.rb', line 61 def for_each(start_at_idx: 0) raise ArgumentError, 'Expecting block, but not given.' unless block_given? @started = true move_to_idx(start_at_idx) yield(row, next_idx) while (self.row = csv.shift) rescue StandardError => err self.exception = err raise ensure (fd.close; @fd = nil) if fd.is_a?(::File) # rubocop:disable Style/Semicolon if exception # Give some feedback if it crashes msg = [] msg << "Last row IDX: #{idx}" msg << "Last row content: #{row.to_h.pretty_inspect}" puts msg log(:debug) { msg.join("\n") } end end |
#move_to_idx(start_at_idx) ⇒ Object
83 84 85 86 87 |
# File 'lib/eco/csv/stream.rb', line 83 def move_to_idx(start_at_idx) @started = true start_at_idx ||= 0 next_idx while (idx < start_at_idx) && (self.row = csv.shift) end |
#shift ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/eco/csv/stream.rb', line 37 def shift raise ArgumentError, 'Expecting block, but not given.' unless block_given? @started = true yield(row, next_idx) if (self.row = csv.shift) rescue StandardError => err self.exception = err raise ensure unless row || !fd.is_a?(::File) fd.close @fd = nil end if exception # Give some feedback if it crashes msg = [] msg << "Last row IDX: #{idx}" msg << "Last row content: #{row.to_h.pretty_inspect}" puts msg log(:debug) { msg.join("\n") } end end |
#started? ⇒ Boolean
33 34 35 |
# File 'lib/eco/csv/stream.rb', line 33 def started? @started ||= false end |