Class: SystemMail::Storage

Inherits:
Object
  • Object
show all
Defined in:
lib/system_mail/storage.rb

Overview

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

Instance Method Summary collapse

Constructor Details

#initialize(path = nil) ⇒ Storage

Returns a new instance of Storage.



9
10
11
12
# File 'lib/system_mail/storage.rb', line 9

def initialize(path = nil)
  @tmpdir = path || Dir.tmpdir
  @io = StringIO.new
end

Instance Method Details

#capture {|@io.path| ... } ⇒ Object

Yields:

  • (@io.path)


28
29
30
31
32
33
# File 'lib/system_mail/storage.rb', line 28

def capture
  ensure_tempfile
  @io.close
  yield @io.path
  @io.open
end

#clearObject



39
40
41
42
# File 'lib/system_mail/storage.rb', line 39

def clear
  @io.close
  @io.unlink if file?
end

#file?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/system_mail/storage.rb', line 35

def file?
  @io.kind_of?(Tempfile)
end

#puts(data = nil) ⇒ Object



14
15
16
# File 'lib/system_mail/storage.rb', line 14

def puts(data = nil)
  @io.puts(data)
end

#readObject



18
19
20
21
22
23
24
25
26
# 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