Class: BlockCipherKit::BlockWritable

Inherits:
Object
  • Object
show all
Defined in:
lib/block_cipher_kit/block_writable.rb

Overview

An adapter which allows a block that accepts chunks of written data to be used as an IO and passed to IO.copy_stream

Instance Method Summary collapse

Constructor Details

#initialize(io = nil, &blk) ⇒ BlockWritable

Returns a new instance of BlockWritable.



4
5
6
7
8
9
10
# File 'lib/block_cipher_kit/block_writable.rb', line 4

def initialize(io = nil, &blk)
  if (!io && !blk) || (io && blk)
    raise ArgumentError, "BlockWritable requires io or a block, but not both"
  end
  @io = io
  @blk = blk
end

Instance Method Details

#write(string) ⇒ Object



12
13
14
15
16
17
18
19
# File 'lib/block_cipher_kit/block_writable.rb', line 12

def write(string)
  if string.bytesize.nonzero? && @io
    @io.write(string.b)
  elsif string.bytesize.nonzero? && @blk
    @blk.call(string.b)
  end
  string.bytesize
end