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

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

Overview

Stores messages

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(folder_path) ⇒ Mbox

Returns a new instance of Mbox.

Parameters:

  • folder_path (String)

    The path of the mailbox file, without the ‘.mbox’ extension



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

def initialize(folder_path)
  @folder_path = folder_path
  @tsx = nil
end

Instance Attribute Details

#folder_pathString (readonly)

Returns The path of the mailbox file, without the ‘.mbox’ extension.

Returns:

  • (String)

    The path of the mailbox file, without the ‘.mbox’ extension



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

def folder_path
  @folder_path
end

Instance Method Details

#append(message) ⇒ void

This method returns an undefined value.

Serializes a message

Parameters:

  • message (String)

    the message text



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

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

#deletevoid

This method returns an undefined value.

Deletes the mailbox



70
71
72
73
74
# File 'lib/imap/backup/serializer/mbox.rb', line 70

def delete
  return if !exist?

  FileUtils.rm(pathname)
end

#exist?Boolean

Returns:

  • (Boolean)


76
77
78
# File 'lib/imap/backup/serializer/mbox.rb', line 76

def exist?
  File.exist?(pathname)
end

#lengthInteger

Returns The lsize of the disk file.

Returns:

  • (Integer)

    The lsize of the disk file



81
82
83
84
85
# File 'lib/imap/backup/serializer/mbox.rb', line 81

def length
  return nil if !exist?

  File.stat(pathname).size
end

#pathnameString

Returns The full path name of the mailbox.

Returns:

  • (String)

    The full path name of the mailbox



88
89
90
# File 'lib/imap/backup/serializer/mbox.rb', line 88

def pathname
  "#{folder_path}.mbox"
end

#read(offset, length) ⇒ String

Reads a message from disk

Parameters:

  • offset (Integer)

    the start of the message inside the mailbox file

  • length (Integer)

    the length of the message (as stored on disk)

Returns:

  • (String)

    the message



61
62
63
64
65
66
# File 'lib/imap/backup/serializer/mbox.rb', line 61

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

#rename(new_path) ⇒ void

This method returns an undefined value.

Renames the mailbox, if it exists, otherwise, simply stores the new name

Parameters:

  • new_path (String)

    the new path (without extension)



96
97
98
99
100
101
102
103
104
# File 'lib/imap/backup/serializer/mbox.rb', line 96

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

#rollbackvoid

This method returns an undefined value.

Returns to the pre-transaction state



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

def rollback
  tsx.fail_outside_transaction!(:rollback)

  rewind(tsx.data[:savepoint][:length])
end

#touchvoid

This method returns an undefined value.

Sets the mailbox file’s updated time to the current time



108
109
110
# File 'lib/imap/backup/serializer/mbox.rb', line 108

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

#transaction(&block) ⇒ void

This method returns an undefined value.

Starts a transaction

Parameters:

  • block (block)

    the block that is wrapped by the transaction

Raises:

  • re-raises errors which occur in the block



21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/imap/backup/serializer/mbox.rb', line 21

def transaction(&block)
  tsx.fail_in_transaction!(:transaction, message: "nested transactions are not supported")

  tsx.begin({savepoint: {length: length}}) do
    block.call
  rescue StandardError => e
    rollback
    raise e
  rescue SignalException => e
    Logger.logger.error "#{self.class} handling #{e.class}"
    rollback
    raise e
  end
end

#valid?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/imap/backup/serializer/mbox.rb', line 44

def valid?
  exist?
end