Class: Imap::Backup::Serializer::Mbox

Inherits:
Object
  • Object
show all
Defined in:
lib/imap/backup/serializer/mbox.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(folder_path) ⇒ Mbox

Returns a new instance of Mbox.


5
6
7
# File 'lib/imap/backup/serializer/mbox.rb', line 5

def initialize(folder_path)
  @folder_path = folder_path
end

Instance Attribute Details

#folder_pathObject (readonly)

Returns the value of attribute folder_path.


3
4
5
# File 'lib/imap/backup/serializer/mbox.rb', line 3

def folder_path
  @folder_path
end

Instance Method Details

#append(message) ⇒ Object


13
14
15
16
17
# File 'lib/imap/backup/serializer/mbox.rb', line 13

def append(message)
  File.open(pathname, "ab") do |file|
    file.write message
  end
end

#deleteObject


26
27
28
29
30
# File 'lib/imap/backup/serializer/mbox.rb', line 26

def delete
  return if !exist?

  File.unlink(pathname)
end

#lengthObject


32
33
34
35
36
# File 'lib/imap/backup/serializer/mbox.rb', line 32

def length
  return nil if !exist?

  File.stat(pathname).size
end

#pathnameObject


38
39
40
# File 'lib/imap/backup/serializer/mbox.rb', line 38

def pathname
  "#{folder_path}.mbox"
end

#read(offset, length) ⇒ Object


19
20
21
22
23
24
# File 'lib/imap/backup/serializer/mbox.rb', line 19

def read(offset, length)
  File.open(pathname, "rb") do |f|
    f.seek offset
    f.read length
  end
end

#rename(new_path) ⇒ Object


42
43
44
45
46
47
48
49
50
# File 'lib/imap/backup/serializer/mbox.rb', line 42

def rename(new_path)
  if exist?
    old_pathname = pathname
    @folder_path = new_path
    File.rename(old_pathname, pathname)
  else
    @folder_path = new_path
  end
end

#rewind(length) ⇒ Object


52
53
54
55
56
# File 'lib/imap/backup/serializer/mbox.rb', line 52

def rewind(length)
  File.open(pathname, File::RDWR | File::CREAT, 0o644) do |f|
    f.truncate(length)
  end
end

#touchObject


58
59
60
# File 'lib/imap/backup/serializer/mbox.rb', line 58

def touch
  File.open(pathname, "a") {}
end

#valid?Boolean

Returns:

  • (Boolean)

9
10
11
# File 'lib/imap/backup/serializer/mbox.rb', line 9

def valid?
  exist?
end