mirror of
https://github.com/lloeki/ruby-tee.git
synced 2025-12-06 10:34:42 +01:00
modulization
This commit is contained in:
parent
6bd9fef9af
commit
755bd3bc11
1 changed files with 37 additions and 31 deletions
16
tee.rb
16
tee.rb
|
|
@ -8,6 +8,7 @@ module Enumerable
|
|||
end
|
||||
|
||||
class IO
|
||||
module Chunkable
|
||||
def chunks(chunk_size=1024)
|
||||
Enumerator.new { |y| y << read(chunk_size) until eof? }
|
||||
end
|
||||
|
|
@ -15,7 +16,9 @@ class IO
|
|||
def each_chunk(chunk_size=nil)
|
||||
chunks.each { |*args| yield *args }
|
||||
end
|
||||
end
|
||||
|
||||
module Digestable
|
||||
def digest_with(digest, chunk_size=nil)
|
||||
chunks(chunk_size).each { |chunk| digest << chunk }
|
||||
digest
|
||||
|
|
@ -24,7 +27,9 @@ class IO
|
|||
def sha256(chunk_size=nil)
|
||||
digest_with Digest::SHA2.new(256), chunk_size
|
||||
end
|
||||
end
|
||||
|
||||
module Utils
|
||||
def tee(*procs)
|
||||
ios = procs.map { |proc| FiberChunkedIO.new &proc }
|
||||
|
||||
|
|
@ -38,6 +43,11 @@ class IO
|
|||
end
|
||||
end
|
||||
|
||||
include Chunkable
|
||||
include Digestable
|
||||
include Utils
|
||||
end
|
||||
|
||||
class FiberChunkedIO
|
||||
def initialize(chunk_size=1024, &block)
|
||||
@chunk_size = chunk_size
|
||||
|
|
@ -83,11 +93,7 @@ class FiberChunkedIO
|
|||
@chunks.shift
|
||||
end
|
||||
|
||||
def chunks(chunk_size=1024)
|
||||
Enumerator.new do |y|
|
||||
y << read(chunk_size) until eof?
|
||||
end
|
||||
end
|
||||
include IO::Chunkable
|
||||
end
|
||||
|
||||
File.open("test") do |f|
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue