Class: Imap::Backup::Serializer

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/imap/backup/serializer.rb,
lib/imap/backup/serializer/imap.rb,
lib/imap/backup/serializer/mbox.rb,
lib/imap/backup/serializer/message.rb,
lib/imap/backup/serializer/appender.rb,
lib/imap/backup/serializer/files/path.rb,
lib/imap/backup/serializer/transaction.rb,
lib/imap/backup/serializer/directory_maker.rb,
lib/imap/backup/serializer/integrity_checker.rb,
lib/imap/backup/serializer/message_enumerator.rb,
lib/imap/backup/serializer/permission_checker.rb,
lib/imap/backup/serializer/unused_name_finder.rb,
lib/imap/backup/serializer/delayed_metadata_serializer.rb

Overview

Handles serialization for a folder

Defined Under Namespace

Classes: Appender, DelayedMetadataSerializer, DirectoryMaker, Files, FolderIntegrityError, Imap, IntegrityChecker, Mbox, Message, MessageEnumerator, PermissionChecker, Transaction, UnusedNameFinder

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(files_path:) ⇒ Serializer



37
38
39
40
# File 'lib/imap/backup/serializer.rb', line 37

def initialize(files_path:)
  @files_path = files_path
  @validated = nil
end

Instance Attribute Details

#files_pathSerializer::Files::Path (readonly)



34
35
36
# File 'lib/imap/backup/serializer.rb', line 34

def files_path
  @files_path
end

Instance Method Details

#append(uid, message, flags) ⇒ void

This method returns an undefined value.

Appends a message to the serialized data



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

def append(uid, message, flags)
  files.validate!

  appender = Serializer::Appender.new(files: files)
  appender.append(uid: uid, message: message, flags: flags)
end

#apply_uid_validity(value) ⇒ String?

Sets the folder’s UID validity. If the existing value is nil, it sets the new value and ensures that both the metadata file and the mailbox are saved. If the supplied value is the same as the existing value, it does nothing. If the supplied valued is different to the existing value, it renames the existing folder to a new name, and creates a new folder with the supplied value.



73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/imap/backup/serializer.rb', line 73

def apply_uid_validity(value)
  case
  when uid_validity.nil?
    force_uid_validity(value)
    nil
  when uid_validity == value
    # NOOP
    nil
  else
    apply_new_uid_validity(value)
  end
end

#filter(&block) ⇒ void

This method returns an undefined value.

Calls the supplied block on each message in the folder and discards those for which the block returns a false result



111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/imap/backup/serializer.rb', line 111

def filter(&block)
  temp_files_path = Serializer::UnusedNameFinder.new(serializer: self).run
  temp_files = Serializer::Files.new(files_path: temp_files_path)
  temp_files.uid_validity = files.uid_validity
  appender = Serializer::Appender.new(files: temp_files)
  enumerator = Serializer::MessageEnumerator.new(imap: files.imap)
  enumerator.run(uids: uids) do |message|
    keep = block.call(message)
    appender.append(uid: message.uid, message: message.body, flags: message.flags) if keep
  end
  files.delete
  temp_files.rename files_path
end

#folderObject



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

def folder
  files_path.folder_name
end

#force_uid_validity(value) ⇒ void

This method returns an undefined value.

Overwrites the UID validity of the folder and ensures that both the metadata file and the mailbox are saved.



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

def force_uid_validity(value)
  files.uid_validity = value
end

#pathObject



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

def path
  files_path.base_path
end

#transaction(&block) ⇒ void

This method returns an undefined value.

Calls the supplied block without implementing transactional behaviour. This method is present so that this class implements the same interface as DelayedMetadataSerializer



56
57
58
# File 'lib/imap/backup/serializer.rb', line 56

def transaction(&block)
  block.call
end