Class: Imap::Backup::Serializer::Imap

Inherits:
Object
  • Object
show all
Defined in:
lib/imap/backup/serializer/imap.rb

Constant Summary collapse

CURRENT_VERSION =
3

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(folder_path) ⇒ Imap

Returns a new instance of Imap.


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

def initialize(folder_path)
  @folder_path = folder_path
  @loaded = false
  @uid_validity = nil
  @messages = nil
  @version = nil
end

Instance Attribute Details

#folder_pathObject (readonly)

Returns the value of attribute folder_path.


7
8
9
# File 'lib/imap/backup/serializer/imap.rb', line 7

def folder_path
  @folder_path
end

#loadedObject (readonly)

Returns the value of attribute loaded.


8
9
10
# File 'lib/imap/backup/serializer/imap.rb', line 8

def loaded
  @loaded
end

Instance Method Details

#append(uid, length, flags = []) ⇒ Object


26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/imap/backup/serializer/imap.rb', line 26

def append(uid, length, flags = [])
  offset =
    if messages.empty?
      0
    else
      last_message = messages[-1]
      last_message.offset + last_message.length
    end

  messages << Serializer::Message.new(
    uid: uid, offset: offset, length: length, mbox: mbox, flags: flags
  )

  save
end

#deleteObject


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

def delete
  return if !exist?

  File.unlink(pathname)
  @loaded = false
  @messages = nil
  @uid_validity = nil
  @version = nil
end

#get(uid) ⇒ Object


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

def get(uid)
  messages.find { |m| m.uid == uid }
end

#messagesObject

Make private


78
79
80
81
# File 'lib/imap/backup/serializer/imap.rb', line 78

def messages
  ensure_loaded
  @messages
end

#rename(new_path) ⇒ Object


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

def rename(new_path)
  if exist?
    old_pathname = pathname
    @folder_path = new_path
    File.rename(old_pathname, pathname)
  else
    @folder_path = new_path
  end
end

#saveObject


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

def save
  ensure_loaded

  raise "Cannot save metadata without a uid_validity" if !uid_validity

  data = {
    version: @version,
    uid_validity: @uid_validity,
    messages: @messages.map(&:to_h)
  }
  content = data.to_json
  File.open(pathname, "w") { |f| f.write content }
end

#uid_validityObject


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

def uid_validity
  ensure_loaded
  @uid_validity
end

#uid_validity=(value) ⇒ Object


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

def uid_validity=(value)
  ensure_loaded
  @uid_validity = value
  save
end

#uidsObject

Deprecated


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

def uids
  messages.map(&:uid)
end

#update_uid(old, new) ⇒ Object


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

def update_uid(old, new)
  index = messages.find_index { |m| m.uid == old }
  return if index.nil?

  messages[index].uid = new
  save
end

#valid?Boolean

Returns:

  • (Boolean)

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

def valid?
  return false if !exist?
  return false if version != CURRENT_VERSION
  return false if !uid_validity

  true
end

#versionObject


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

def version
  ensure_loaded
  @version
end