Class: Dutiful::ApplicationDefault

Inherits:
Object
  • Object
show all
Defined in:
lib/dutiful/application_default.rb

Defined Under Namespace

Classes: Result

Instance Method Summary collapse

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

#backupObject



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.message, false
end

#backup_valueObject



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

Returns:

  • (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

Returns:

  • (Boolean)


48
49
50
# File 'lib/dutiful/application_default.rb', line 48

def has_backup?
  File.exist? backup_path
end

#in_sync?Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/dutiful/application_default.rb', line 56

def in_sync?
  value == backup_value
end

#nameObject



33
34
35
# File 'lib/dutiful/application_default.rb', line 33

def name
  "#{@domain} #{@key}"
end

#restoreObject



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.message, false
end

#to_sObject



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

Returns:

  • (Boolean)


52
53
54
# File 'lib/dutiful/application_default.rb', line 52

def tracked?
  exist? || has_backup?
end

#valueObject



37
38
39
# File 'lib/dutiful/application_default.rb', line 37

def value
  @value ||= `defaults read #{@domain} #{@key} 2>/dev/null`
end