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



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

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

#deleteObject



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

def delete
  return if !exist?

  File.unlink(pathname)
end

#exist?Boolean

Returns:

  • (Boolean)


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

def exist?
  File.exist?(pathname)
end

#lengthObject



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

def length
  return nil if !exist?

  File.stat(pathname).size
end

#pathnameObject



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

def pathname
  "#{folder_path}.mbox"
end

#read(offset, length) ⇒ Object



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

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

#rename(new_path) ⇒ Object



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

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



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

def rollback
  tsx.fail_outside_transaction!(:rollback)

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

#touchObject



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

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

#transaction(&block) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
# 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
    rollback
    raise e
  rescue SignalException => e
    Logger.logger.error "#{self.class} handling #{e.class}"
    rollback
    raise e
  end
end

#valid?Boolean

Returns:

  • (Boolean)


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

def valid?
  exist?
end