Class: Imap::Backup::Configuration
- Inherits:
-
Object
- Object
- Imap::Backup::Configuration
- Defined in:
- lib/imap/backup/configuration.rb
Constant Summary collapse
- CONFIGURATION_DIRECTORY =
File.("~/.imap-backup")
- VERSION_2_1 =
"2.1".freeze
- VERSION =
"2.2".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
-
#pathname ⇒ Object
readonly
Returns the value of attribute pathname.
Class Method Summary collapse
Instance Method Summary collapse
- #accounts ⇒ Object
- #download_strategy ⇒ Object
- #download_strategy=(value) ⇒ Object
- #download_strategy_modified? ⇒ Boolean
-
#initialize(path: nil) ⇒ Configuration
constructor
A new instance of Configuration.
- #modified? ⇒ Boolean
- #path ⇒ Object
- #save ⇒ Object
Constructor Details
#initialize(path: nil) ⇒ Configuration
Returns a new instance of Configuration.
32 33 34 35 36 37 |
# File 'lib/imap/backup/configuration.rb', line 32 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
#pathname ⇒ Object (readonly)
Returns the value of attribute pathname.
22 23 24 |
# File 'lib/imap/backup/configuration.rb', line 22 def pathname @pathname end |
Class Method Details
.default_pathname ⇒ Object
24 25 26 |
# File 'lib/imap/backup/configuration.rb', line 24 def self.default_pathname File.join(CONFIGURATION_DIRECTORY, "config.json") end |
.exist?(path: nil) ⇒ Boolean
28 29 30 |
# File 'lib/imap/backup/configuration.rb', line 28 def self.exist?(path: nil) File.exist?(path || default_pathname) end |
Instance Method Details
#accounts ⇒ Object
59 60 61 62 63 64 65 66 67 |
# File 'lib/imap/backup/configuration.rb', line 59 def accounts @accounts ||= begin ensure_loaded! accounts = data[:accounts].map do |attr| Account.new(attr) end inject_global_attributes(accounts) end end |
#download_strategy ⇒ Object
69 70 71 72 73 |
# File 'lib/imap/backup/configuration.rb', line 69 def download_strategy ensure_loaded! @download_strategy end |
#download_strategy=(value) ⇒ Object
75 76 77 78 79 80 81 82 83 |
# File 'lib/imap/backup/configuration.rb', line 75 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
85 86 87 88 89 |
# File 'lib/imap/backup/configuration.rb', line 85 def download_strategy_modified? ensure_loaded! @download_strategy_modified end |
#modified? ⇒ Boolean
91 92 93 94 95 96 97 |
# File 'lib/imap/backup/configuration.rb', line 91 def modified? ensure_loaded! return true if download_strategy_modified? accounts.any? { |a| a.modified? || a.marked_for_deletion? } end |
#path ⇒ Object
39 40 41 |
# File 'lib/imap/backup/configuration.rb', line 39 def path File.dirname(pathname) end |
#save ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/imap/backup/configuration.rb', line 43 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 |