Sha256: 5ace93cea1fedcac19fadb86665567d0badacc12dd913b524cf4caba89714f7b

Contents?: true

Size: 1.95 KB

Versions: 16

Compression:

Stored size: 1.95 KB

Contents

module IOStreams
  module Row
    # Converts each line of an input stream into an array for every line
    class Reader < IOStreams::Reader
      # Read a line as an Array at a time from a stream.
      # Note:
      # - The supplied stream _must_ already be a line stream, or a stream that responds to :each
      def self.stream(line_reader, **args)
        # Pass-through if already a row reader
        return yield(line_reader) if line_reader.is_a?(self.class)

        yield new(line_reader, **args)
      end

      # When reading from a file also add the line reader stream
      def self.file(file_name, original_file_name: file_name, delimiter: $/, **args)
        IOStreams::Line::Reader.file(file_name, original_file_name: original_file_name, delimiter: delimiter) do |io|
          yield new(io, original_file_name: original_file_name, **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(line_reader, cleanse_header: true, original_file_name: nil, **args)
        unless line_reader.respond_to?(:each)
          raise(ArgumentError, "Stream must be a IOStreams::Line::Reader or implement #each")
        end

        @tabular        = IOStreams::Tabular.new(file_name: original_file_name, **args)
        @line_reader    = line_reader
        @cleanse_header = cleanse_header
      end

      def each
        @line_reader.each do |line|
          if @tabular.header?
            columns = @tabular.parse_header(line)
            @tabular.cleanse_header! if @cleanse_header
            yield columns
          else
            yield @tabular.row_parse(line)
          end
        end
      end
    end
  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
iostreams-1.10.3 lib/io_streams/row/reader.rb
iostreams-1.10.2 lib/io_streams/row/reader.rb
iostreams-1.9.0 lib/io_streams/row/reader.rb
iostreams-1.8.0 lib/io_streams/row/reader.rb
iostreams-1.7.0 lib/io_streams/row/reader.rb
iostreams-1.6.2 lib/io_streams/row/reader.rb
iostreams-1.6.1 lib/io_streams/row/reader.rb
iostreams-1.6.0 lib/io_streams/row/reader.rb
iostreams-1.5.1 lib/io_streams/row/reader.rb
iostreams-1.5.0 lib/io_streams/row/reader.rb
iostreams-1.4.0 lib/io_streams/row/reader.rb
iostreams-1.3.3 lib/io_streams/row/reader.rb
iostreams-1.3.2 lib/io_streams/row/reader.rb
iostreams-1.3.1 lib/io_streams/row/reader.rb
iostreams-1.3.0 lib/io_streams/row/reader.rb
iostreams-1.2.1 lib/io_streams/row/reader.rb