lib/imap/backup/configuration/store.rb in imap-backup-4.0.5 vs lib/imap/backup/configuration/store.rb in imap-backup-4.0.6

- old
+ new

@@ -1,13 +1,16 @@ require "json" require "os" +require "imap/backup/account" + module Imap::Backup module Configuration; end class Configuration::Store CONFIGURATION_DIRECTORY = File.expand_path("~/.imap-backup") + VERSION = "2.0" attr_reader :pathname def self.default_pathname File.join(CONFIGURATION_DIRECTORY, "config.json") @@ -17,63 +20,82 @@ File.exist?(pathname) end def initialize(pathname = self.class.default_pathname) @pathname = pathname + @debug = nil end def path File.dirname(pathname) end def save + ensure_loaded! 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)) } + save_data = { + version: VERSION, + accounts: accounts.map(&:to_h), + debug: debug? + } + File.open(pathname, "w") { |f| f.write(JSON.pretty_generate(save_data)) } FileUtils.chmod(0o600, pathname) if !windows? end def accounts - data[:accounts] + @accounts ||= begin + ensure_loaded! + data[:accounts].map { |data| Account.new(data) } + end end def modified? - accounts.any? { |a| a[:modified] || a[:delete] } + ensure_loaded! + accounts.any? { |a| a.modified? || a.marked_for_deletion? } end def debug? - data[:debug] + ensure_loaded! + @debug end def debug=(value) - data[:debug] = [true, false].include?(value) ? value : false + ensure_loaded! + @debug = [true, false].include?(value) ? value : false end private + def ensure_loaded! + return true if @data + + data + @debug = data.key?(:debug) ? data[:debug] == true : false + true + end + def data @data ||= begin if File.exist?(pathname) Utils.check_permissions(pathname, 0o600) if !windows? contents = File.read(pathname) - data = JSON.parse(contents, symbolize_names: true) + JSON.parse(contents, symbolize_names: true) else - data = {accounts: []} + {accounts: []} end - data[:debug] = data.key?(:debug) ? data[:debug] == true : false - data end end def remove_modified_flags - accounts.each { |a| a.delete(:modified) } + accounts.each { |a| a.clear_changes! } end def remove_deleted_accounts - accounts.reject! { |a| a[:delete] } + accounts.reject! { |a| a.marked_for_deletion? } end def make_private(path) FileUtils.chmod(0o700, path) if Utils.mode(path) != 0o700 end