Class: Imap::Backup::Configuration

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

Overview

Handles the application’s configuration file

Constant Summary collapse

CONFIGURATION_DIRECTORY =

The default directory of the configuration file

File.expand_path("~/.imap-backup")
DEFAULT_STRATEGY =

The default download strategy key

"delay_metadata".freeze
DOWNLOAD_STRATEGIES =

The available download strategies

[
  {key: "direct", description: "write straight to disk"},
  {key: DEFAULT_STRATEGY, description: "delay writing metadata"}
].freeze
VERSION =

The current file version

"2.2".freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path: nil) ⇒ Configuration

Returns a new instance of Configuration.



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

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_pathnameString

Returns the default configuration file path.

Returns:

  • (String)

    the default configuration file path



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

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

.exist?(path: nil) ⇒ Boolean

Returns:

  • (Boolean)


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

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

Instance Method Details

#accountsArray<Account>

Returns the configured accounts.

Returns:

  • (Array<Account>)

    the configured accounts



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

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

#download_strategyString

Returns the cofigured download strategy.

Returns:

  • (String)

    the cofigured download strategy



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

def download_strategy
  ensure_loaded!

  @download_strategy
end

#download_strategy=(value) ⇒ void

This method returns an undefined value.

Parameters:

  • value (String)

    the new strategy



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

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)


95
96
97
98
99
# File 'lib/imap/backup/configuration.rb', line 95

def download_strategy_modified?
  ensure_loaded!

  @download_strategy_modified
end

#modified?Boolean

Returns:

  • (Boolean)


101
102
103
104
105
106
107
# File 'lib/imap/backup/configuration.rb', line 101

def modified?
  ensure_loaded!

  return true if download_strategy_modified?

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

#pathString

Returns the directory containing the configuration file.

Returns:

  • (String)

    the directory containing the configuration file



43
44
45
# File 'lib/imap/backup/configuration.rb', line 43

def path
  File.dirname(pathname)
end

#savevoid

This method returns an undefined value.

Saves the configuration file in JSON format



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/imap/backup/configuration.rb', line 49

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