Sha256: 369aa79823b1d55469dc39db6496cda3f2d94ce50289058e609eb825e32a39a0
Contents?: true
Size: 1.89 KB
Versions: 1
Compression:
Stored size: 1.89 KB
Contents
module IOStreams module Row # Converts each line of an input stream into an array for every line class Reader # Read a line as an Array at a time from a file or stream. def self.open(file_name_or_io, delimiter: nil, buffer_size: 65_536, encoding: UTF8_ENCODING, strip_non_printable: false, file_name: nil, **args) if file_name_or_io.is_a?(String) IOStreams.line_reader(file_name_or_io, delimiter: delimiter, buffer_size: buffer_size, encoding: encoding, file_name: file_name, strip_non_printable: strip_non_printable) do |io| yield new(io, file_name: file_name, **args) end else yield new(file_name_or_io, **args) end end # Create a Tabular reader to return the stream rows as arrays. # # Parameters # delimited: [#each] # Anything that returns one line / record at a time when #each is called on it. # # format: [Symbol] # :csv, :hash, :array, :json, :psv, :fixed # # For all other parameters, see Tabular::Header.new def initialize(delimited, cleanse_header: true, **args) @tabular = IOStreams::Tabular.new(**args) @delimited = delimited @cleanse_header = cleanse_header end def each delimited.each do |line| if tabular.requires_header? columns = tabular.parse_header(line) tabular.cleanse_header! if cleanse_header yield columns else yield tabular.row_parse(line) end end end private attr_reader :tabular, :delimited, :cleanse_header end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
iostreams-0.15.0 | lib/io_streams/row/reader.rb |