Class: IOP::FileReader

Inherits:
IOReader show all
Defined in:
lib/iop/file.rb

Overview

Feed class to read data from local file and send it in blocks downstream.

Contrary to IOReader, this class manages underlying IO instance in order to close it when the process is finished even if exception is risen.

### Use case: compute MD5 hash sum of the first 1024 bytes of a local file.

require 'iop/file'
require 'iop/digest'
( IOP::FileReader.new('input.dat', size: 1024) | (d = IOP::DigestComputer.new(Digest::MD5.new)) ).process!
puts d.digest.hexdigest

Since:

  • 0.1

Instance Attribute Summary

Attributes included from Feed

#downstream

Instance Method Summary collapse

Methods included from Feed

#process, #|

Constructor Details

#initialize(file, mode: 'rb', **options) ⇒ FileReader

Creates class instance.

Parameters:

  • file (String)

    name of file to read from

  • mode (String) (defaults to: 'rb')

    open mode for the file; refer to File for details

  • options (Hash)

    extra keyword parameters passed to IOReader constructor

Since:

  • 0.1



98
99
100
101
102
# File 'lib/iop/file.rb', line 98

def initialize(file, mode: 'rb', **options)
  super(nil, **options)
  @file = file
  @mode = mode
end

Instance Method Details

#process!Object

Since:

  • 0.1



104
105
106
107
108
109
110
111
# File 'lib/iop/file.rb', line 104

def process!
  @io = File.new(@file, @mode)
  begin
    super
  ensure
    @io.close
  end
end