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
|
end
|
||||||
|
|
||||||
class IO
|
class IO
|
||||||
|
module Chunkable
|
||||||
def chunks(chunk_size=1024)
|
def chunks(chunk_size=1024)
|
||||||
Enumerator.new { |y| y << read(chunk_size) until eof? }
|
Enumerator.new { |y| y << read(chunk_size) until eof? }
|
||||||
end
|
end
|
||||||
|
|
@ -15,7 +16,9 @@ class IO
|
||||||
def each_chunk(chunk_size=nil)
|
def each_chunk(chunk_size=nil)
|
||||||
chunks.each { |*args| yield *args }
|
chunks.each { |*args| yield *args }
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
module Digestable
|
||||||
def digest_with(digest, chunk_size=nil)
|
def digest_with(digest, chunk_size=nil)
|
||||||
chunks(chunk_size).each { |chunk| digest << chunk }
|
chunks(chunk_size).each { |chunk| digest << chunk }
|
||||||
digest
|
digest
|
||||||
|
|
@ -24,7 +27,9 @@ class IO
|
||||||
def sha256(chunk_size=nil)
|
def sha256(chunk_size=nil)
|
||||||
digest_with Digest::SHA2.new(256), chunk_size
|
digest_with Digest::SHA2.new(256), chunk_size
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
module Utils
|
||||||
def tee(*procs)
|
def tee(*procs)
|
||||||
ios = procs.map { |proc| FiberChunkedIO.new &proc }
|
ios = procs.map { |proc| FiberChunkedIO.new &proc }
|
||||||
|
|
||||||
|
|
@ -36,6 +41,11 @@ class IO
|
||||||
ios.each { |io| io.close }
|
ios.each { |io| io.close }
|
||||||
ios.map { |io| io.result }
|
ios.map { |io| io.result }
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
include Chunkable
|
||||||
|
include Digestable
|
||||||
|
include Utils
|
||||||
end
|
end
|
||||||
|
|
||||||
class FiberChunkedIO
|
class FiberChunkedIO
|
||||||
|
|
@ -83,11 +93,7 @@ class FiberChunkedIO
|
||||||
@chunks.shift
|
@chunks.shift
|
||||||
end
|
end
|
||||||
|
|
||||||
def chunks(chunk_size=1024)
|
include IO::Chunkable
|
||||||
Enumerator.new do |y|
|
|
||||||
y << read(chunk_size) until eof?
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
File.open("test") do |f|
|
File.open("test") do |f|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue