class SystemMail::Storage

A class to store string data either in StringIO or in Tempfile.

Public Class Methods

new(path = nil) click to toggle source
# File lib/system_mail/storage.rb, line 9
def initialize(path = nil)
  @tmpdir = path || Dir.tmpdir
  @io = StringIO.new
end

Public Instance Methods

capture() { |path| ... } click to toggle source
# File lib/system_mail/storage.rb, line 28
def capture
  ensure_tempfile
  @io.close
  yield @io.path
  @io.open
end
clear() click to toggle source
# File lib/system_mail/storage.rb, line 39
def clear
  @io.close
  @io.unlink if file?
end
file?() click to toggle source
# File lib/system_mail/storage.rb, line 35
def file?
  @io.kind_of?(Tempfile)
end
puts(data = nil) click to toggle source
# File lib/system_mail/storage.rb, line 14
def puts(data = nil)
  @io.puts(data)
end
read() click to toggle source
# File lib/system_mail/storage.rb, line 18
def read
  if file?
    capture do |file_path|
      File.read file_path
    end
  else
    @io.string.dup
  end
end

Private Instance Methods

create_tempfile() click to toggle source
# File lib/system_mail/storage.rb, line 53
def create_tempfile
  temp_directory = File.join(@tmpdir, 'system_mail')
  FileUtils.mkdir_p(temp_directory)
  Tempfile.new('storage', temp_directory, :mode => IO::APPEND)
end
ensure_tempfile() click to toggle source
# File lib/system_mail/storage.rb, line 46
def ensure_tempfile
  return if file?
  tempfile = create_tempfile
  tempfile.puts @io.string if @io.size > 0
  @io = tempfile
end