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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path: nil) ⇒ Configuration

Returns a new instance of Configuration.



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

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

Instance Attribute Details

#pathnameObject (readonly)

Returns the value of attribute pathname.



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

def pathname
  @pathname
end

Class Method Details

.default_pathnameObject



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

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

.exist?(path: nil) ⇒ Boolean

Returns:

  • (Boolean)


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

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

Instance Method Details

#accountsObject



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

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

#download_strategyObject



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

def download_strategy
  ensure_loaded!

  @download_strategy
end

#download_strategy=(value) ⇒ Object



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

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_modifiedObject



84
85
86
87
88
# File 'lib/imap/backup/configuration.rb', line 84

def download_strategy_modified
  ensure_loaded!

  @download_strategy_modified
end

#modified?Boolean

Returns:

  • (Boolean)


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

def modified?
  ensure_loaded!

  return true if download_strategy_modified

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

#pathObject



38
39
40
# File 'lib/imap/backup/configuration.rb', line 38

def path
  File.dirname(pathname)
end

#saveObject



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

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