Class: Imap::Backup::Serializer::Mbox
- Inherits:
-
Object
- Object
- Imap::Backup::Serializer::Mbox
- Defined in:
- lib/imap/backup/serializer/mbox.rb
Overview
Stores messages
Instance Attribute Summary collapse
-
#folder_path ⇒ String
readonly
The path of the mailbox file, without the ‘.mbox’ extension.
Instance Method Summary collapse
-
#append(message) ⇒ void
Serializes a message.
-
#delete ⇒ void
Deletes the mailbox.
- #exist? ⇒ Boolean
-
#initialize(folder_path) ⇒ Mbox
constructor
A new instance of Mbox.
-
#length ⇒ Integer
The lsize of the disk file.
-
#pathname ⇒ String
The full path name of the mailbox.
-
#read(offset, length) ⇒ String
Reads a message from disk.
-
#rename(new_path) ⇒ void
Renames the mailbox, if it exists, otherwise, simply stores the new name.
-
#rollback ⇒ void
Returns to the pre-transaction state.
-
#touch ⇒ void
Sets the mailbox file’s updated time to the current time.
-
#transaction(&block) ⇒ void
Starts a transaction.
- #valid? ⇒ Boolean
Constructor Details
#initialize(folder_path) ⇒ Mbox
Returns a new instance of Mbox.
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_path ⇒ String (readonly)
Returns 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
53 54 55 56 57 |
# File 'lib/imap/backup/serializer/mbox.rb', line 53 def append() File.open(pathname, "ab") do |file| file.write end end |
#delete ⇒ void
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
79 80 81 |
# File 'lib/imap/backup/serializer/mbox.rb', line 79 def exist? File.exist?(pathname) end |
#length ⇒ Integer
Returns 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 |
#pathname ⇒ String
Returns 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
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
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 |
#rollback ⇒ void
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 |
#touch ⇒ void
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
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
46 47 48 |
# File 'lib/imap/backup/serializer/mbox.rb', line 46 def valid? exist? end |