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 inherited from RandomAccessReader

#read!, #seek!

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)

    keyword parameters passed to IOReader constructor

Since:

  • 0.1



73
74
75
76
77
# File 'lib/iop/file.rb', line 73

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

Instance Method Details

#process!Object

Since:

  • 0.1



79
80
81
82
83
84
85
86
# File 'lib/iop/file.rb', line 79

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