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
- #parsed_backup_value ⇒ 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 9 |
# File 'lib/dutiful/application_default.rb', line 4 def initialize(hash) @description = hash[:description] @domain = hash[:domain] @key = hash[:key] @type = hash[:type] end |
Instance Method Details
#backup ⇒ Object
11 12 13 14 15 16 17 18 |
# File 'lib/dutiful/application_default.rb', line 11 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
20 21 22 |
# File 'lib/dutiful/application_default.rb', line 20 def backup_value File.read(backup_path) end |
#exist? ⇒ Boolean
48 49 50 51 52 53 |
# File 'lib/dutiful/application_default.rb', line 48 def exist? @exists ||= begin value $?.success? end end |
#has_backup? ⇒ Boolean
55 56 57 |
# File 'lib/dutiful/application_default.rb', line 55 def has_backup? File.exist? backup_path end |
#in_sync? ⇒ Boolean
63 64 65 |
# File 'lib/dutiful/application_default.rb', line 63 def in_sync? value == backup_value end |
#name ⇒ Object
40 41 42 |
# File 'lib/dutiful/application_default.rb', line 40 def name @key end |
#parsed_backup_value ⇒ Object
24 25 26 27 28 29 30 31 |
# File 'lib/dutiful/application_default.rb', line 24 def parsed_backup_value case @type when '-bool' backup_value == '1' ? true : false else backup_value end end |
#restore ⇒ Object
33 34 35 36 37 38 |
# File 'lib/dutiful/application_default.rb', line 33 def restore `defaults write #{@domain} #{@key} #{@type} #{parsed_backup_value}` Result.new nil, true rescue => ex Result.new ex., false end |
#to_s ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/dutiful/application_default.rb', line 67 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
59 60 61 |
# File 'lib/dutiful/application_default.rb', line 59 def tracked? exist? || has_backup? end |
#value ⇒ Object
44 45 46 |
# File 'lib/dutiful/application_default.rb', line 44 def value @value ||= `defaults read #{@domain} #{@key} 2>/dev/null`.strip end |