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 ⇒ Object
readonly
Returns the value of attribute folder_path.
Instance Method Summary collapse
- #append(uid, length, flags: []) ⇒ Object
- #delete ⇒ Object
- #exist? ⇒ Boolean
- #get(uid) ⇒ Object
-
#initialize(folder_path) ⇒ Imap
constructor
A new instance of Imap.
-
#messages ⇒ Object
Make private.
- #pathname ⇒ Object
- #rename(new_path) ⇒ Object
- #rollback ⇒ Object
- #save ⇒ Object
- #transaction(&block) ⇒ Object
- #uid_validity ⇒ Object
- #uid_validity=(value) ⇒ Object
-
#uids ⇒ Object
Deprecated.
- #update_uid(old, new) ⇒ Object
- #valid? ⇒ Boolean
- #version ⇒ Object
Constructor Details
#initialize(folder_path) ⇒ Imap
Returns a new instance of Imap.
18 19 20 21 22 23 24 25 |
# File 'lib/imap/backup/serializer/imap.rb', line 18 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 ⇒ Object (readonly)
Returns the value of attribute folder_path.
15 16 17 |
# File 'lib/imap/backup/serializer/imap.rb', line 15 def folder_path @folder_path end |
Instance Method Details
#append(uid, length, flags: []) ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/imap/backup/serializer/imap.rb', line 69 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 ⇒ Object
89 90 91 92 93 94 95 96 97 |
# File 'lib/imap/backup/serializer/imap.rb', line 89 def delete return if !exist? FileUtils.rm(pathname) @loaded = false @messages = nil @uid_validity = nil @version = nil end |
#exist? ⇒ Boolean
57 58 59 |
# File 'lib/imap/backup/serializer/imap.rb', line 57 def exist? File.exist?(pathname) end |
#get(uid) ⇒ Object
85 86 87 |
# File 'lib/imap/backup/serializer/imap.rb', line 85 def get(uid) .find { |m| m.uid == uid } end |
#messages ⇒ Object
Make private
121 122 123 124 |
# File 'lib/imap/backup/serializer/imap.rb', line 121 def ensure_loaded @messages end |
#pathname ⇒ Object
53 54 55 |
# File 'lib/imap/backup/serializer/imap.rb', line 53 def pathname "#{folder_path}.imap" end |
#rename(new_path) ⇒ Object
99 100 101 102 103 104 105 106 107 |
# File 'lib/imap/backup/serializer/imap.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 ⇒ Object
44 45 46 47 48 49 50 51 |
# File 'lib/imap/backup/serializer/imap.rb', line 44 def rollback tsx.fail_outside_transaction!(:rollback) @messages = tsx.data[:savepoint][:messages] @uid_validity = tsx.data[:savepoint][:uid_validity] tsx.clear end |
#save ⇒ Object
144 145 146 147 148 149 150 |
# File 'lib/imap/backup/serializer/imap.rb', line 144 def save return if tsx.in_transaction? ensure_loaded save_internal(version: version, uid_validity: uid_validity, messages: ) end |
#transaction(&block) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/imap/backup/serializer/imap.rb', line 27 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 ⇒ Object
109 110 111 112 |
# File 'lib/imap/backup/serializer/imap.rb', line 109 def uid_validity ensure_loaded @uid_validity end |
#uid_validity=(value) ⇒ Object
114 115 116 117 118 |
# File 'lib/imap/backup/serializer/imap.rb', line 114 def uid_validity=(value) ensure_loaded @uid_validity = value save end |
#uids ⇒ Object
Deprecated
127 128 129 |
# File 'lib/imap/backup/serializer/imap.rb', line 127 def uids .map(&:uid) end |
#update_uid(old, new) ⇒ Object
131 132 133 134 135 136 137 |
# File 'lib/imap/backup/serializer/imap.rb', line 131 def update_uid(old, new) index = .find_index { |m| m.uid == old } return if index.nil? [index].uid = new save end |
#valid? ⇒ Boolean
61 62 63 64 65 66 67 |
# File 'lib/imap/backup/serializer/imap.rb', line 61 def valid? return false if !exist? return false if version != CURRENT_VERSION return false if !uid_validity true end |
#version ⇒ Object
139 140 141 142 |
# File 'lib/imap/backup/serializer/imap.rb', line 139 def version ensure_loaded @version end |