Module: Tins::TempIO

Included in:
Object, Enum
Defined in:
lib/tins/temp_io.rb,
lib/tins/temp_io_enum.rb

Defined Under Namespace

Classes: Enum

Instance Method Summary collapse

Instance Method Details

#temp_io(content: nil, name: __method__) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/tins/temp_io.rb', line 5

def temp_io(content: nil, name: __method__)
  content.nil? and raise ArgumentError, "missing keyword: content"
  name = File.basename(name.to_s)
  Dir.mktmpdir do |dir|
    name = File.join(dir, name)
    File.open(name, 'w+b') do |io|
      if content.respond_to?(:call)
        if content.respond_to?(:arity) && content.arity == 1
          content.call(io)
        else
          io.write content.call
        end
      else
        io.write content
      end
      io.rewind
      yield io
    end
  end
end