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



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

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



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

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



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

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

#deletevoid

This method returns an undefined value.

Deletes the mailbox



72
73
74
75
76
77
# File 'lib/imap/backup/serializer/mbox.rb', line 72

def delete
  return if !exist?

  Logger.logger.info("Deleting mailbox '#{pathname}'")
  FileUtils.rm(pathname)
end

#exist?Boolean

Returns:

  • (Boolean)


79
80
81
# File 'lib/imap/backup/serializer/mbox.rb', line 79

def exist?
  File.exist?(pathname)
end

#lengthInteger

Returns The lsize of the disk file.

Returns:

  • (Integer)

    The lsize of the disk file



84
85
86
87
88
# File 'lib/imap/backup/serializer/mbox.rb', line 84

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



91
92
93
# File 'lib/imap/backup/serializer/mbox.rb', line 91

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



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

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)



99
100
101
102
103
104
105
106
107
# File 'lib/imap/backup/serializer/mbox.rb', line 99

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



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

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



111
112
113
# File 'lib/imap/backup/serializer/mbox.rb', line 111

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



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

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)


46
47
48
# File 'lib/imap/backup/serializer/mbox.rb', line 46

def valid?
  exist?
end