Class: Imap::Backup::Configuration

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

Constant Summary collapse

CONFIGURATION_DIRECTORY =
File.expand_path("~/.imap-backup")
DEFAULT_DOWNLOAD_BLOCK_SIZE =
1
VERSION =
"2.0"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pathname = self.class.default_pathname) ⇒ Configuration

Returns a new instance of Configuration.



22
23
24
25
26
# File 'lib/imap/backup/configuration.rb', line 22

def initialize(pathname = self.class.default_pathname)
  @pathname = pathname
  @saved_debug = nil
  @debug = nil
end

Instance Attribute Details

#pathnameObject (readonly)

Returns the value of attribute pathname.



12
13
14
# File 'lib/imap/backup/configuration.rb', line 12

def pathname
  @pathname
end

Class Method Details

.default_pathnameObject



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

def self.default_pathname
  File.join(CONFIGURATION_DIRECTORY, "config.json")
end

.exist?(pathname = default_pathname) ⇒ Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/imap/backup/configuration.rb', line 18

def self.exist?(pathname = default_pathname)
  File.exist?(pathname)
end

Instance Method Details

#accountsObject



48
49
50
51
52
53
# File 'lib/imap/backup/configuration.rb', line 48

def accounts
  @accounts ||= begin
    ensure_loaded!
    data[:accounts].map { |data| .new(data) }
  end
end

#debug=(value) ⇒ Object



76
77
78
79
# File 'lib/imap/backup/configuration.rb', line 76

def debug=(value)
  ensure_loaded!
  @debug = [true, false].include?(value) ? value : false
end

#debug?Boolean

Returns:

  • (Boolean)


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

def debug?
  ensure_loaded!
  @debug
end

#download_block_sizeObject



55
56
57
58
59
60
61
62
# File 'lib/imap/backup/configuration.rb', line 55

def download_block_size
  size = ENV["DOWNLOAD_BLOCK_SIZE"].to_i
  if size > 0
    size
  else
    DEFAULT_DOWNLOAD_BLOCK_SIZE
  end
end

#modified?Boolean

Returns:

  • (Boolean)


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

def modified?
  ensure_loaded!
  return true if @saved_debug != @debug

  accounts.any? { |a| a.modified? || a.marked_for_deletion? }
end

#pathObject



28
29
30
# File 'lib/imap/backup/configuration.rb', line 28

def path
  File.dirname(pathname)
end

#saveObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/imap/backup/configuration.rb', line 32

def save
  ensure_loaded!
  FileUtils.mkdir(path) if !File.directory?(path)
  make_private(path) if !windows?
  remove_modified_flags
  remove_deleted_accounts
  save_data = {
    version: VERSION,
    accounts: accounts.map(&:to_h),
    debug: debug?
  }
  File.open(pathname, "w") { |f| f.write(JSON.pretty_generate(save_data)) }
  FileUtils.chmod(0o600, pathname) if !windows?
  @data = nil
end