lib/io_streams/line/reader.rb in iostreams-0.20.3 vs lib/io_streams/line/reader.rb in iostreams-1.0.0.beta

- old
+ new

@@ -1,22 +1,21 @@ module IOStreams module Line - class Reader + class Reader < IOStreams::Reader attr_reader :delimiter, :buffer_size, :line_number # Prevent denial of service when a delimiter is not found before this number * `buffer_size` characters are read. MAX_BLOCKS_MULTIPLIER = 100 LINEFEED_REGEXP = Regexp.compile(/\r\n|\n|\r/).freeze - # Read a line at a time from a file or stream - def self.open(file_name_or_io, **args) - if file_name_or_io.is_a?(String) - IOStreams::File::Reader.open(file_name_or_io) { |io| yield new(io, **args) } - else - yield new(file_name_or_io, **args) - end + # Read a line at a time from a stream + def self.stream(input_stream, original_file_name: nil, **args, &block) + # Pass-through if already a line reader + return block.call(input_stream) if input_stream.is_a?(self.class) + + yield new(input_stream, **args) end # Create a delimited stream reader from the supplied input stream. # # Lines returned will be in the encoding of the input stream. @@ -44,11 +43,12 @@ # - Skip Comment lines. RegExp? # - Skip "empty" / "blank" lines. RegExp? # - Extract header line(s) / first non-comment, non-blank line # - Embedded newline support, RegExp? or Proc? def initialize(input_stream, delimiter: nil, buffer_size: 65_536, embedded_within: nil) + super(input_stream) + @embedded_within = embedded_within - @input_stream = input_stream @buffer_size = buffer_size # More efficient read buffering only supported when the input stream `#read` method supports it. @use_read_cache_buffer = !@input_stream.method(:read).arity.between?(0, 1)