Class: Imap::Backup::Serializer::Imap
- Inherits:
-
Object
- Object
- Imap::Backup::Serializer::Imap
- Defined in:
- lib/imap/backup/serializer/imap.rb
Overview
Stores message metadata
Constant Summary collapse
- CURRENT_VERSION =
The version number to store in the metadata file
3
Instance Attribute Summary collapse
-
#folder_path ⇒ String
readonly
The path of the imap metadata file, without the ‘.imap’ extension.
Instance Method Summary collapse
-
#append(uid, length, flags: []) ⇒ void
Append message metadata.
-
#delete ⇒ void
Deletes the metadata file and discards stored attributes.
- #exist? ⇒ Boolean
-
#get(uid) ⇒ Serializer::Message
Get message metadata.
-
#initialize(folder_path) ⇒ Imap
constructor
A new instance of Imap.
- #messages ⇒ Array<Hash>
-
#pathname ⇒ String
The full path name of the metadata file.
-
#rename(new_path) ⇒ void
Renames the metadata file, if it exists, otherwise, simply stores the new name.
-
#rollback ⇒ void
Discards stored changes to the data.
-
#save ⇒ void
Saves the file, except in a transaction when it does nothing.
-
#transaction(&block) ⇒ void
Opens a transaction.
-
#uid_validity ⇒ Integer
The UID validity for the folder.
-
#uid_validity=(value) ⇒ void
Sets the folder’s UID validity and saves the metadata file.
-
#uids ⇒ Array<Integer>
The uids of all messages.
-
#update_uid(old, new) ⇒ void
Update a message’s metadata, replacing its UID.
- #valid? ⇒ Boolean
-
#version ⇒ String
The format version for the metadata file.
Constructor Details
#initialize(folder_path) ⇒ Imap
Returns a new instance of Imap.
19 20 21 22 23 24 25 26 |
# File 'lib/imap/backup/serializer/imap.rb', line 19 def initialize(folder_path) @folder_path = folder_path @loaded = false @uid_validity = nil @messages = nil @version = nil @tsx = nil end |
Instance Attribute Details
#folder_path ⇒ String (readonly)
Returns The path of the imap metadata file, without the ‘.imap’ extension.
16 17 18 |
# File 'lib/imap/backup/serializer/imap.rb', line 16 def folder_path @folder_path end |
Instance Method Details
#append(uid, length, flags: []) ⇒ void
This method returns an undefined value.
Append message metadata
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/imap/backup/serializer/imap.rb', line 82 def append(uid, length, flags: []) offset = if .empty? 0 else = [-1] .offset + .length end << Serializer::Message.new( uid: uid, offset: offset, length: length, mbox: mbox, flags: flags ) save end |
#delete ⇒ void
This method returns an undefined value.
Deletes the metadata file and discards stored attributes
108 109 110 111 112 113 114 115 116 |
# File 'lib/imap/backup/serializer/imap.rb', line 108 def delete return if !exist? FileUtils.rm(pathname) @loaded = false @messages = nil @uid_validity = nil @version = nil end |
#exist? ⇒ Boolean
65 66 67 |
# File 'lib/imap/backup/serializer/imap.rb', line 65 def exist? File.exist?(pathname) end |
#get(uid) ⇒ Serializer::Message
Get message metadata
101 102 103 |
# File 'lib/imap/backup/serializer/imap.rb', line 101 def get(uid) .find { |m| m.uid == uid } end |
#messages ⇒ Array<Hash>
148 149 150 151 |
# File 'lib/imap/backup/serializer/imap.rb', line 148 def ensure_loaded @messages end |
#pathname ⇒ String
Returns The full path name of the metadata file.
61 62 63 |
# File 'lib/imap/backup/serializer/imap.rb', line 61 def pathname "#{folder_path}.imap" end |
#rename(new_path) ⇒ void
This method returns an undefined value.
Renames the metadata file, if it exists, otherwise, simply stores the new name
122 123 124 125 126 127 128 129 130 |
# File 'lib/imap/backup/serializer/imap.rb', line 122 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.
Discards stored changes to the data
51 52 53 54 55 56 57 58 |
# File 'lib/imap/backup/serializer/imap.rb', line 51 def rollback tsx.fail_outside_transaction!(:rollback) @messages = tsx.data[:savepoint][:messages] @uid_validity = tsx.data[:savepoint][:uid_validity] tsx.clear end |
#save ⇒ void
This method returns an undefined value.
Saves the file, except in a transaction when it does nothing
180 181 182 183 184 185 186 |
# File 'lib/imap/backup/serializer/imap.rb', line 180 def save return if tsx.in_transaction? ensure_loaded save_internal(version: version, uid_validity: uid_validity, messages: ) end |
#transaction(&block) ⇒ void
This method returns an undefined value.
Opens a transaction
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/imap/backup/serializer/imap.rb', line 32 def transaction(&block) tsx.fail_in_transaction!(:transaction, message: "nested transactions are not supported") ensure_loaded # rubocop:disable Lint/RescueException tsx.begin({savepoint: {messages: .dup, uid_validity: uid_validity}}) do block.call save_internal(version: version, uid_validity: uid_validity, messages: ) if tsx.data rescue Exception => e Logger.logger.error "#{self.class} handling #{e.class}" rollback raise e end # rubocop:enable Lint/RescueException end |
#uid_validity ⇒ Integer
Returns the UID validity for the folder.
133 134 135 136 |
# File 'lib/imap/backup/serializer/imap.rb', line 133 def uid_validity ensure_loaded @uid_validity end |
#uid_validity=(value) ⇒ void
This method returns an undefined value.
Sets the folder’s UID validity and saves the metadata file
141 142 143 144 145 |
# File 'lib/imap/backup/serializer/imap.rb', line 141 def uid_validity=(value) ensure_loaded @uid_validity = value save end |
#uids ⇒ Array<Integer>
Returns The uids of all messages.
154 155 156 |
# File 'lib/imap/backup/serializer/imap.rb', line 154 def uids .map(&:uid) end |
#update_uid(old, new) ⇒ void
This method returns an undefined value.
Update a message’s metadata, replacing its UID
162 163 164 165 166 167 168 |
# File 'lib/imap/backup/serializer/imap.rb', line 162 def update_uid(old, new) index = .find_index { |m| m.uid == old } return if index.nil? [index].uid = new save end |
#valid? ⇒ Boolean
69 70 71 72 73 74 75 |
# File 'lib/imap/backup/serializer/imap.rb', line 69 def valid? return false if !exist? return false if version != CURRENT_VERSION return false if !uid_validity true end |
#version ⇒ String
Returns The format version for the metadata file.
171 172 173 174 |
# File 'lib/imap/backup/serializer/imap.rb', line 171 def version ensure_loaded @version end |