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.



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

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

Instance Attribute Details

#folderObject (readonly)

Returns the value of attribute folder.



27
28
29
# File 'lib/imap/backup/serializer.rb', line 27

def folder
  @folder
end

#pathObject (readonly)

Returns the value of attribute path.



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

def path
  @path
end

Class Method Details

.folder_path_for(path:, folder:) ⇒ Object



17
18
19
20
# File 'lib/imap/backup/serializer.rb', line 17

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



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

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



68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/imap/backup/serializer.rb', line 68

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



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

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

#deleteObject



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

def delete
  imap.delete
  @imap = nil
  mbox.delete
  @mbox = nil
end

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



104
105
106
107
108
109
110
111
112
113
# File 'lib/imap/backup/serializer.rb', line 104

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



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/imap/backup/serializer.rb', line 115

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
  @imap = nil
  @mbox = nil
end

#folder_pathObject



135
136
137
# File 'lib/imap/backup/serializer.rb', line 135

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

#force_uid_validity(value) ⇒ Object



83
84
85
86
87
# File 'lib/imap/backup/serializer.rb', line 83

def force_uid_validity(value)
  validate!

  internal_force_uid_validity(value)
end

#sanitizedObject



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

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

#transaction(&block) ⇒ Object



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

def transaction(&block)
  block.call
end

#update(uid, flags: nil) ⇒ Object



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

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)



42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/imap/backup/serializer.rb', line 42

def validate!
  return true if @validated

  optionally_migrate2to3

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

  delete

  false
end