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

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(folder_path) ⇒ Mbox

Returns a new instance of Mbox.



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

def initialize(folder_path)
  @folder_path = folder_path
  @tsx = nil
end

Instance Attribute Details

#folder_pathObject (readonly)

Returns the value of attribute folder_path.



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

def folder_path
  @folder_path
end

#savepointObject (readonly)

Returns the value of attribute savepoint.



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

def savepoint
  @savepoint
end

Instance Method Details

#append(message) ⇒ Object



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

def append(message)
  File.open(pathname, "ab") do |file|
    file.write message
  end
end

#deleteObject



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

def delete
  return if !exist?

  File.unlink(pathname)
end

#exist?Boolean

Returns:

  • (Boolean)


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

def exist?
  File.exist?(pathname)
end

#lengthObject



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

def length
  return nil if !exist?

  File.stat(pathname).size
end

#pathnameObject



74
75
76
# File 'lib/imap/backup/serializer/mbox.rb', line 74

def pathname
  "#{folder_path}.mbox"
end

#read(offset, length) ⇒ Object



51
52
53
54
55
56
# File 'lib/imap/backup/serializer/mbox.rb', line 51

def read(offset, length)
  File.open(pathname, "rb") do |f|
    f.seek offset
    f.read length
  end
end

#rename(new_path) ⇒ Object



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

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

#rollbackObject



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

def rollback
  tsx.fail_outside_transaction!(:rollback)

  rewind(tsx.data[:savepoint][:length])
end

#touchObject



88
89
90
# File 'lib/imap/backup/serializer/mbox.rb', line 88

def touch
  File.open(pathname, "a") {}
end

#transaction(&block) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/imap/backup/serializer/mbox.rb', line 15

def transaction(&block)
  tsx.fail_in_transaction!(:transaction, message: "nested transactions are not supported")

  tsx.begin({savepoint: {length: length}}) do
    block.call
  rescue StandardError => e
    message = <<~ERROR
      #{self.class} error #{e}
      #{e.backtrace.join("\n")}
    ERROR
    Logger.logger.error message
    rollback
    raise e
  rescue SignalException => e
    Logger.logger.error "#{self.class} handling #{e.class}"
    rollback
    raise e
  end
end

#valid?Boolean

Returns:

  • (Boolean)


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

def valid?
  exist?
end