Class: Dutiful::ApplicationDefault
- Inherits:
-
Object
- Object
- Dutiful::ApplicationDefault
- Defined in:
- lib/dutiful/application_default.rb
Defined Under Namespace
Classes: Result
Instance Method Summary collapse
- #backup ⇒ Object
- #backup_value ⇒ Object
- #exist? ⇒ Boolean
- #has_backup? ⇒ Boolean
- #in_sync? ⇒ Boolean
-
#initialize(hash) ⇒ ApplicationDefault
constructor
A new instance of ApplicationDefault.
- #name ⇒ Object
- #restore ⇒ Object
- #to_s ⇒ Object
- #tracked? ⇒ Boolean
- #value ⇒ Object
Constructor Details
#initialize(hash) ⇒ ApplicationDefault
Returns a new instance of ApplicationDefault.
4 5 6 7 8 |
# File 'lib/dutiful/application_default.rb', line 4 def initialize(hash) @description = hash[:description] @domain = hash[:domain] @key = hash[:key] end |
Instance Method Details
#backup ⇒ Object
10 11 12 13 14 15 16 17 |
# File 'lib/dutiful/application_default.rb', line 10 def backup FileUtils.mkdir_p File.dirname "#{backup_path}" File.open(backup_path, 'w') { |file| file << value } Result.new nil, true rescue => ex Result.new ex., false end |
#backup_value ⇒ Object
19 20 21 22 23 24 |
# File 'lib/dutiful/application_default.rb', line 19 def backup_value value = nil File.open(backup_path) { |file| value = file.read } value end |
#exist? ⇒ Boolean
41 42 43 44 45 46 |
# File 'lib/dutiful/application_default.rb', line 41 def exist? @exists ||= begin @value = `defaults read #{@domain} #{@key} 2>/dev/null` $?.success? end end |
#has_backup? ⇒ Boolean
48 49 50 |
# File 'lib/dutiful/application_default.rb', line 48 def has_backup? File.exist? backup_path end |
#in_sync? ⇒ Boolean
56 57 58 |
# File 'lib/dutiful/application_default.rb', line 56 def in_sync? value == backup_value end |
#name ⇒ Object
33 34 35 |
# File 'lib/dutiful/application_default.rb', line 33 def name "#{@domain} #{@key}" end |
#restore ⇒ Object
26 27 28 29 30 31 |
# File 'lib/dutiful/application_default.rb', line 26 def restore `defaults write #{@domain} #{@key} #{backup_value}` Result.new nil, true rescue => ex Result.new ex., false end |
#to_s ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/dutiful/application_default.rb', line 60 def to_s if exist? if has_backup? if in_sync? "#{name} ✔".green else "#{name} (modified)".yellow end else "#{name} (pending backup)".yellow end elsif has_backup? "#{name} (pending restore)".yellow else "#{name} does not exist (skipping)".light_black end end |
#tracked? ⇒ Boolean
52 53 54 |
# File 'lib/dutiful/application_default.rb', line 52 def tracked? exist? || has_backup? end |
#value ⇒ Object
37 38 39 |
# File 'lib/dutiful/application_default.rb', line 37 def value @value ||= `defaults read #{@domain} #{@key} 2>/dev/null` end |