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.0".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.



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

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

Instance Attribute Details

#pathnameObject (readonly)

Returns the value of attribute pathname.



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

def pathname
  @pathname
end

Class Method Details

.default_pathnameObject



16
17
18
# File 'lib/imap/backup/configuration.rb', line 16

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

.exist?(path: nil) ⇒ Boolean

Returns:

  • (Boolean)


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

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

Instance Method Details

#accountsObject



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

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

#modified?Boolean

Returns:

  • (Boolean)


54
55
56
57
58
# File 'lib/imap/backup/configuration.rb', line 54

def modified?
  ensure_loaded!

  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
# File 'lib/imap/backup/configuration.rb', line 32

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