Class: Imap::Backup::Serializer

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

Defined Under Namespace

Classes: Appender, Directory, Imap, Mbox, Message, MessageEnumerator, 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.


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

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

Instance Attribute Details

#folderObject (readonly)

Returns the value of attribute folder.


24
25
26
# File 'lib/imap/backup/serializer.rb', line 24

def folder
  @folder
end

#pathObject (readonly)

Returns the value of attribute path.


25
26
27
# File 'lib/imap/backup/serializer.rb', line 25

def path
  @path
end

Class Method Details

.folder_path_for(path:, folder:) ⇒ Object


14
15
16
17
# File 'lib/imap/backup/serializer.rb', line 14

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


72
73
74
75
76
77
# File 'lib/imap/backup/serializer.rb', line 72

def append(uid, message, flags)
  validate!

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

#apply_uid_validity(value) ⇒ Object


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

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

#deleteObject


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

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

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


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

def each_message(required_uids = nil, &block)
  required_uids ||= uids

  validate!

  return enum_for(:each_message, required_uids) if !block

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

#filter(&block) ⇒ Object


98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/imap/backup/serializer.rb', line 98

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.run(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


118
119
120
# File 'lib/imap/backup/serializer.rb', line 118

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

#force_uid_validity(value) ⇒ Object


66
67
68
69
70
# File 'lib/imap/backup/serializer.rb', line 66

def force_uid_validity(value)
  validate!

  internal_force_uid_validity(value)
end

#update(uid, flags: nil) ⇒ Object


79
80
81
82
83
84
85
# File 'lib/imap/backup/serializer.rb', line 79

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)


34
35
36
37
38
39
40
41
42
# File 'lib/imap/backup/serializer.rb', line 34

def validate!
  optionally_migrate2to3

  return true if imap.valid? && mbox.valid?

  delete

  false
end