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.
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_path ⇒ String (readonly)
Returns 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
51 52 53 54 55 |
# File 'lib/imap/backup/serializer/mbox.rb', line 51 def append() File.open(pathname, "ab") do |file| file.write end end |
#delete ⇒ void
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
76 77 78 |
# File 'lib/imap/backup/serializer/mbox.rb', line 76 def exist? File.exist?(pathname) end |
#length ⇒ Integer
Returns 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 |
#pathname ⇒ String
Returns 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
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
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 |
#rollback ⇒ void
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 |
#touch ⇒ void
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
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
44 45 46 |
# File 'lib/imap/backup/serializer/mbox.rb', line 44 def valid? exist? end |