Class: IOStreams::Reader

Inherits:
Object
  • Object
show all
Defined in:
lib/io_streams/reader.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input_stream) ⇒ Reader

Returns a new instance of Reader.



24
25
26
# File 'lib/io_streams/reader.rb', line 24

def initialize(input_stream)
  @input_stream = input_stream
end

Instance Attribute Details

#input_streamObject (readonly)

Returns the value of attribute input_stream.



22
23
24
# File 'lib/io_streams/reader.rb', line 22

def input_stream
  @input_stream
end

Class Method Details

.file(file_name, original_file_name: file_name, **args, &block) ⇒ Object

When a Writer supports streams, also allow it to simply support a file



13
14
15
# File 'lib/io_streams/reader.rb', line 13

def self.file(file_name, original_file_name: file_name, **args, &block)
  ::File.open(file_name, 'rb') { |file| stream(file, original_file_name: original_file_name, **args, &block) }
end

.open(file_name_or_io, **args, &block) ⇒ Object

For processing by either a file name or an open IO stream.



18
19
20
# File 'lib/io_streams/reader.rb', line 18

def self.open(file_name_or_io, **args, &block)
  file_name_or_io.is_a?(String) ? file(file_name_or_io, **args, &block) : stream(file_name_or_io, **args, &block)
end

.stream(input_stream, **args, &block) ⇒ Object

When a Reader does not support streams, we copy the stream to a local temp file and then pass that filename in for this reader.



5
6
7
8
9
10
# File 'lib/io_streams/reader.rb', line 5

def self.stream(input_stream, **args, &block)
  Utils.temp_file_name("iostreams_reader") do |file_name|
    ::File.open(file_name, 'wb') { |target| ::IO.copy_stream(input_stream, target) }
    file(file_name, **args, &block)
  end
end