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,
lib/imap/backup/serializer/mbox_enumerator.rb

Defined Under Namespace

Classes: Appender, Directory, Imap, Mbox, MboxEnumerator, MessageEnumerator, UnusedNameFinder

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, folder) ⇒ Serializer

Returns a new instance of Serializer.


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

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

Instance Attribute Details

#folderObject (readonly)

Returns the value of attribute folder.


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

def folder
  @folder
end

#pathObject (readonly)

Returns the value of attribute path.


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

def path
  @path
end

Class Method Details

.folder_path_for(path:, folder:) ⇒ Object


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

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

Instance Method Details

#append(uid, message) ⇒ Object


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

def append(uid, message)
  validate!

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

#apply_uid_validity(value) ⇒ Object


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

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

#each_message(required_uids, &block) ⇒ Object


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

def each_message(required_uids, &block)
  validate!

  return enum_for(:each_message, required_uids) if !block

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

#force_uid_validity(value) ⇒ Object


59
60
61
62
63
# File 'lib/imap/backup/serializer.rb', line 59

def force_uid_validity(value)
  validate!

  internal_force_uid_validity(value)
end

#load(uid_maybe_string) ⇒ Object


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

def load(uid_maybe_string)
  validate!

  uid = uid_maybe_string.to_i
  message_index = imap.index(uid)
  return nil if message_index.nil?

  internal_load_nth(message_index)
end

#load_nth(index) ⇒ Object


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

def load_nth(index)
  validate!

  internal_load_nth(index)
end

#validate!Object

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


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

def validate!
  return true if imap.valid? && mbox.valid?

  imap.delete
  @imap = nil
  mbox.delete
  @mbox = nil

  false
end