class Serverkit::Resources::Defaults

A resource class for Mac OS X user defaults system.

Constants

DEFAULT_DOMAIN

Public Instance Methods

apply() click to toggle source
# File lib/serverkit/resources/defaults.rb, line 21
def apply
  run_command("defaults write #{escaped_domain} #{escaped_key} #{type_option} #{value_in_plist}")
end
check() click to toggle source

@note Override @return [true, false]

# File lib/serverkit/resources/defaults.rb, line 27
def check
  value == read_value
end

Private Instance Methods

default_id() click to toggle source

@note Override

# File lib/serverkit/resources/defaults.rb, line 34
def default_id
  "#{domain} #{key}"
end
escaped_domain() click to toggle source

@return [String] @example “com.apple.TextEdit”

# File lib/serverkit/resources/defaults.rb, line 40
def escaped_domain
  ::Shellwords.shellescape(domain)
end
escaped_key() click to toggle source

@return [String] @example “com.apple.keyboard.fnState”

# File lib/serverkit/resources/defaults.rb, line 46
def escaped_key
  ::Shellwords.shellescape(key)
end
escaped_value() click to toggle source

@return [String] @example “foo\ bar”

# File lib/serverkit/resources/defaults.rb, line 52
def escaped_value
  ::Shellwords.shellescape(value)
end
has_same_value?() click to toggle source

@return [true, false] True if specified value is same with stored value

# File lib/serverkit/resources/defaults.rb, line 57
def has_same_value?
  value == stored_value
end
read_binary_value() click to toggle source

@return [String] @example “42n”

# File lib/serverkit/resources/defaults.rb, line 63
def read_binary_value
  run_command("defaults read #{escaped_domain} #{escaped_key}").stdout
end
read_raw_value() click to toggle source

@return [Array, Hash, String, nil] @example “1”

# File lib/serverkit/resources/defaults.rb, line 81
def read_raw_value
  tempfile = ::Tempfile.new("")
  tempfile << read_binary_value
  tempfile.close
  run_command("plutil -convert xml1 #{tempfile.path}")
  ::Plist.parse_xml(tempfile.path)
end
read_value() click to toggle source

@return [Object] Plain old ruby object with dirty-coerce

# File lib/serverkit/resources/defaults.rb, line 68
def read_value
  case raw_value = read_raw_value
  when /\A-?\d+\z/
    raw_value.to_i
  when /\A-?\d+\.\d+\z/
    raw_value.to_f
  else
    raw_value
  end
end
type_option() click to toggle source

@return [String]

# File lib/serverkit/resources/defaults.rb, line 95
def type_option
  case value
  when Fixnum
    "-int"
  when Float
    "-float"
  when String
    "-string"
  end
end
value_in_plist() click to toggle source

@return [String]

# File lib/serverkit/resources/defaults.rb, line 90
def value_in_plist
  ::Shellwords.shellescape(Plist.generate(value))
end