class Dutiful::ApplicationDefault

Constants

Result

Public Class Methods

new(hash) click to toggle source
# File lib/dutiful/application_default.rb, line 4
def initialize(hash)
  @description = hash[:description]
  @domain      = hash[:domain]
  @key         = hash[:key]
  @type        = hash[:type]
end

Public Instance Methods

backup() click to toggle source
# 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_value() click to toggle source
# File lib/dutiful/application_default.rb, line 20
def backup_value
  File.read(backup_path)
end
exist?() click to toggle source
# File lib/dutiful/application_default.rb, line 48
def exist?
  @exists ||= begin
                 value
                 $?.success?
               end
end
has_backup?() click to toggle source
# File lib/dutiful/application_default.rb, line 55
def has_backup?
  File.exist? backup_path
end
in_sync?() click to toggle source
# File lib/dutiful/application_default.rb, line 63
def in_sync?
  value == backup_value
end
name() click to toggle source
# File lib/dutiful/application_default.rb, line 40
def name
  @key
end
parsed_backup_value() click to toggle source
# 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() click to toggle source
# 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_s() click to toggle source
# 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?() click to toggle source
# File lib/dutiful/application_default.rb, line 59
def tracked?
  exist? || has_backup?
end
value() click to toggle source
# File lib/dutiful/application_default.rb, line 44
def value
  @value ||= `defaults read #{@domain} #{@key} 2>/dev/null`.strip
end

Private Instance Methods

backup_path() click to toggle source
# File lib/dutiful/application_default.rb, line 87
def backup_path
  Dutiful::Config.storage.path "defaults/#{@domain}/#{@key}"
end