Class: BlockCipherKit::BlockWritable

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

Overview

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(&blk) ⇒ BlockWritable

Returns a new instance of BlockWritable.



16
17
18
# File 'lib/block_cipher_kit/block_writable.rb', line 16

def initialize(&blk)
  @blk = blk
end

Class Method Details

.new(io = nil, &blk) ⇒ Object



5
6
7
8
9
10
11
12
13
14
# File 'lib/block_cipher_kit/block_writable.rb', line 5

def self.new(io = nil, &blk)
  if (!io && !blk) || (io && blk)
    raise ArgumentError, "BlockWritable requires io or a block, but not both"
  end
  # If the IO is given, it is better to just pass it through
  # as IO.copy_stream will do optimisations for native IOs like
  # File, Socket etc.
  return io if io
  super(&blk)
end

Instance Method Details

#write(string) ⇒ Object



20
21
22
23
# File 'lib/block_cipher_kit/block_writable.rb', line 20

def write(string)
  @blk.call(string.b)
  string.bytesize
end