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

#backupObject



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

#backup_valueObject



20
21
22
# File 'lib/dutiful/application_default.rb', line 20

def backup_value
  File.read(backup_path)
end

#exist?Boolean

Returns:

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

Returns:

  • (Boolean)


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

def has_backup?
  File.exist? backup_path
end

#in_sync?Boolean

Returns:

  • (Boolean)


63
64
65
# File 'lib/dutiful/application_default.rb', line 63

def in_sync?
  value == backup_value
end

#nameObject



40
41
42
# File 'lib/dutiful/application_default.rb', line 40

def name
  @key
end

#parsed_backup_valueObject



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

#restoreObject



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

#to_sObject



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

Returns:

  • (Boolean)


59
60
61
# File 'lib/dutiful/application_default.rb', line 59

def tracked?
  exist? || has_backup?
end

#valueObject



44
45
46
# File 'lib/dutiful/application_default.rb', line 44

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