Class: Imap::Backup::Configuration::Store

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

Constant Summary collapse

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Store.



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

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

Instance Attribute Details

#pathname ⇒ Object (readonly)

Returns the value of attribute pathname.



10
11
12
# File 'lib/imap/backup/configuration/store.rb', line 10

def pathname
  @pathname
end

Class Method Details

.default_pathname ⇒ Object



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

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

.exist?(pathname = default_pathname) ⇒ Boolean

Returns:

  • (Boolean)


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

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

Instance Method Details

#accounts ⇒ Object



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

def accounts
  data[:accounts]
end

#debug=(value) ⇒ Object



49
50
51
# File 'lib/imap/backup/configuration/store.rb', line 49

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

#debug? ⇒ Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/imap/backup/configuration/store.rb', line 45

def debug?
  data[:debug]
end

#modified? ⇒ Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/imap/backup/configuration/store.rb', line 41

def modified?
  accounts.any? { |a| a[:modified] || a[:delete] }
end

#path ⇒ Object



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

def path
  File.dirname(pathname)
end

#save ⇒ Object



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

def save
  FileUtils.mkdir(path) if !File.directory?(path)
  make_private(path) if !windows?
  remove_modified_flags
  remove_deleted_accounts
  File.open(pathname, "w") { |f| f.write(JSON.pretty_generate(data)) }
  FileUtils.chmod(0o600, pathname) if !windows?
end