File: temp_io_enum.rb

package info (click to toggle)
ruby-tins 1.32.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,248 kB
  • sloc: ruby: 6,659; makefile: 3
file content (26 lines) | stat: -rw-r--r-- 658 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
require 'tins/temp_io'

module Tins
  module TempIO
    class Enum < Enumerator
      include Tins::TempIO

      def initialize(chunk_size: 2 ** 16, filename: nil, &content_proc)
        content_proc or raise ArgumentError, 'need a content proc as block argument'
        super() do |y|
          temp_io(name: 'some-stream', content: content_proc) do |file|
            until file.eof?
              y.yield file.read(chunk_size)
            end
          end
        end.tap do |enum|
          if filename
            enum.define_singleton_method(:filename) do
              filename
            end
          end
        end
      end
    end
  end
end