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.



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

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

Instance Method Details

#append(message) ⇒ Object



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

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

#deleteObject



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

def delete
  return if !exist?

  FileUtils.rm(pathname)
end

#exist?Boolean

Returns:

  • (Boolean)


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

def exist?
  File.exist?(pathname)
end

#lengthObject



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

def length
  return nil if !exist?

  File.stat(pathname).size
end

#pathnameObject



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

def pathname
  "#{folder_path}.mbox"
end

#read(offset, length) ⇒ Object



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

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

#rename(new_path) ⇒ Object



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

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



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

def rollback
  tsx.fail_outside_transaction!(:rollback)

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

#touchObject



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

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

#transaction(&block) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/imap/backup/serializer/mbox.rb', line 14

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
    rollback
    raise e
  rescue SignalException => e
    Logger.logger.error "#{self.class} handling #{e.class}"
    rollback
    raise e
  end
end

#valid?Boolean

Returns:

  • (Boolean)


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

def valid?
  exist?
end