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".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
-
#download_strategy ⇒ Object
Returns the value of attribute download_strategy.
-
#download_strategy_modified ⇒ Object
readonly
Returns the value of attribute download_strategy_modified.
-
#download_strategy_original ⇒ Object
readonly
Returns the value of attribute download_strategy_original.
-
#pathname ⇒ Object
readonly
Returns the value of attribute pathname.
Class Method Summary collapse
Instance Method Summary collapse
- #accounts ⇒ Object
-
#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.
34 35 36 37 38 39 |
# File 'lib/imap/backup/configuration.rb', line 34 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
#download_strategy ⇒ Object
Returns the value of attribute download_strategy.
22 23 24 |
# File 'lib/imap/backup/configuration.rb', line 22 def download_strategy @download_strategy end |
#download_strategy_modified ⇒ Object (readonly)
Returns the value of attribute download_strategy_modified.
24 25 26 |
# File 'lib/imap/backup/configuration.rb', line 24 def download_strategy_modified @download_strategy_modified end |
#download_strategy_original ⇒ Object (readonly)
Returns the value of attribute download_strategy_original.
23 24 25 |
# File 'lib/imap/backup/configuration.rb', line 23 def download_strategy_original @download_strategy_original end |
#pathname ⇒ Object (readonly)
Returns the value of attribute pathname.
21 22 23 |
# File 'lib/imap/backup/configuration.rb', line 21 def pathname @pathname end |
Class Method Details
.default_pathname ⇒ Object
26 27 28 |
# File 'lib/imap/backup/configuration.rb', line 26 def self.default_pathname File.join(CONFIGURATION_DIRECTORY, "config.json") end |
.exist?(path: nil) ⇒ Boolean
30 31 32 |
# File 'lib/imap/backup/configuration.rb', line 30 def self.exist?(path: nil) File.exist?(path || default_pathname) end |
Instance Method Details
#accounts ⇒ Object
61 62 63 64 65 66 67 68 69 |
# File 'lib/imap/backup/configuration.rb', line 61 def accounts @accounts ||= begin ensure_loaded! accounts = data[:accounts].map do |attr| Account.new(attr) end inject_global_attributes(accounts) end end |
#modified? ⇒ Boolean
81 82 83 84 85 86 87 |
# File 'lib/imap/backup/configuration.rb', line 81 def modified? ensure_loaded! return true if download_strategy_modified accounts.any? { |a| a.modified? || a.marked_for_deletion? } end |
#path ⇒ Object
41 42 43 |
# File 'lib/imap/backup/configuration.rb', line 41 def path File.dirname(pathname) end |
#save ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/imap/backup/configuration.rb', line 45 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 |