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_STRATEGY =
"delay_metadata".freeze
DOWNLOAD_STRATEGIES =
[
  {key: "direct", description: "write straight to disk"},
  {key: DEFAULT_STRATEGY, description: "delay writing metadata"}
].freeze
VERSION =
"2.2".freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path: nil) ⇒ Configuration

Returns a new instance of Configuration.



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

def initialize(path: nil)
  @pathname = path || self.class.default_pathname
  @download_strategy = nil
  @download_strategy_original = nil
  @download_strategy_modified = false
end

Class Method Details

.default_pathnameObject



21
22
23
# File 'lib/imap/backup/configuration.rb', line 21

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

.exist?(path: nil) ⇒ Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/imap/backup/configuration.rb', line 25

def self.exist?(path: nil)
  File.exist?(path || default_pathname)
end

Instance Method Details

#accountsObject



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

def accounts
  @accounts ||= begin
    ensure_loaded!
    accounts = data[:accounts].map do |attr|
      Account.new(attr)
    end
    inject_global_attributes(accounts)
  end
end

#download_strategyObject



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

def download_strategy
  ensure_loaded!

  @download_strategy
end

#download_strategy=(value) ⇒ Object



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

def download_strategy=(value)
  raise "Unknown strategy '#{value}'" if !DOWNLOAD_STRATEGIES.find { |s| s[:key] == value }

  ensure_loaded!

  @download_strategy = value
  @download_strategy_modified = value != @download_strategy_original
  inject_global_attributes(accounts)
end

#download_strategy_modified?Boolean

Returns:

  • (Boolean)


82
83
84
85
86
# File 'lib/imap/backup/configuration.rb', line 82

def download_strategy_modified?
  ensure_loaded!

  @download_strategy_modified
end

#modified?Boolean

Returns:

  • (Boolean)


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

def modified?
  ensure_loaded!

  return true if download_strategy_modified?

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

#pathObject



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

def path
  File.dirname(pathname)
end

#saveObject



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/imap/backup/configuration.rb', line 40

def save
  ensure_loaded!
  FileUtils.mkdir_p(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),
    download_strategy: download_strategy
  }
  File.open(pathname, "w") { |f| f.write(JSON.pretty_generate(save_data)) }
  FileUtils.chmod(0o600, pathname) if !windows?
  @data = nil
end