Class: Imap::Backup::Serializer

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/imap/backup/serializer.rb,
lib/imap/backup/serializer/appender.rb,
lib/imap/backup/serializer/directory.rb,
lib/imap/backup/serializer/folder_maker.rb,
lib/imap/backup/serializer/integrity_checker.rb

Defined Under Namespace

Classes: Appender, DelayedMetadataSerializer, Directory, FolderIntegrityError, FolderMaker, Imap, IntegrityChecker, Mbox, Message, MessageEnumerator, PermissionChecker, Transaction, UnusedNameFinder, Version2Migrator

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, folder) ⇒ Serializer

Returns a new instance of Serializer.



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

def initialize(path, folder)
  @path = path
  @folder = folder
  @validated = nil
end

Instance Attribute Details

#folderObject (readonly)

Returns the value of attribute folder.



29
30
31
# File 'lib/imap/backup/serializer.rb', line 29

def folder
  @folder
end

#pathObject (readonly)

Returns the value of attribute path.



30
31
32
# File 'lib/imap/backup/serializer.rb', line 30

def path
  @path
end

Class Method Details

.folder_path_for(path:, folder:) ⇒ Object



19
20
21
22
# File 'lib/imap/backup/serializer.rb', line 19

def self.folder_path_for(path:, folder:)
  relative = File.join(path, folder)
  File.expand_path(relative)
end

Instance Method Details

#append(uid, message, flags) ⇒ Object



95
96
97
98
99
100
# File 'lib/imap/backup/serializer.rb', line 95

def append(uid, message, flags)
  validate!

  appender = Serializer::Appender.new(folder: sanitized, imap: imap, mbox: mbox)
  appender.append(uid: uid, message: message, flags: flags)
end

#apply_uid_validity(value) ⇒ Object



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

def apply_uid_validity(value)
  validate!

  case
  when uid_validity.nil?
    internal_force_uid_validity(value)
    nil
  when uid_validity == value
    # NOOP
    nil
  else
    apply_new_uid_validity(value)
  end
end

#check_integrity!Object



64
65
66
# File 'lib/imap/backup/serializer.rb', line 64

def check_integrity!
  IntegrityChecker.new(imap: imap, mbox: mbox).run
end

#deleteObject



68
69
70
71
72
# File 'lib/imap/backup/serializer.rb', line 68

def delete
  imap.delete
  mbox.delete
  reload
end

#each_message(required_uids = nil, &block) ⇒ Object



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

def each_message(required_uids = nil, &block)
  return enum_for(:each_message, required_uids) if !block

  required_uids ||= uids

  validate!

  enumerator = Serializer::MessageEnumerator.new(imap: imap)
  enumerator.run(uids: required_uids, &block)
end

#filter(&block) ⇒ Object



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/imap/backup/serializer.rb', line 121

def filter(&block)
  temp_name = Serializer::UnusedNameFinder.new(serializer: self).run
  temp_folder_path = self.class.folder_path_for(path: path, folder: temp_name)
  new_mbox = Serializer::Mbox.new(temp_folder_path)
  new_imap = Serializer::Imap.new(temp_folder_path)
  new_imap.uid_validity = imap.uid_validity
  appender = Serializer::Appender.new(folder: temp_name, imap: new_imap, mbox: new_mbox)
  enumerator = Serializer::MessageEnumerator.new(imap: 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
  imap.delete
  new_imap.rename imap.folder_path
  mbox.delete
  new_mbox.rename mbox.folder_path
  reload
end

#folder_pathObject



140
141
142
# File 'lib/imap/backup/serializer.rb', line 140

def folder_path
  self.class.folder_path_for(path: path, folder: sanitized)
end

#force_uid_validity(value) ⇒ Object



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

def force_uid_validity(value)
  validate!

  internal_force_uid_validity(value)
end

#reloadObject



148
149
150
151
# File 'lib/imap/backup/serializer.rb', line 148

def reload
  @imap = nil
  @mbox = nil
end

#sanitizedObject



144
145
146
# File 'lib/imap/backup/serializer.rb', line 144

def sanitized
  @sanitized ||= Naming.to_local_path(folder)
end

#transaction(&block) ⇒ void

This method returns an undefined value.

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



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

def transaction(&block)
  block.call
end

#update(uid, flags: nil) ⇒ Object



102
103
104
105
106
107
108
# File 'lib/imap/backup/serializer.rb', line 102

def update(uid, flags: nil)
  message = imap.get(uid)
  return if !message

  message.flags = flags if flags
  imap.save
end

#validate!Object

Returns true if there are existing, valid files false otherwise (in which case any existing files are deleted)



49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/imap/backup/serializer.rb', line 49

def validate!
  return true if @validated

  optionally_migrate2to3

  if imap.valid? && mbox.valid?
    @validated = true
    return true
  end

  delete

  false
end