Class/Module Index [+]

Quicksearch

Facter::Util::CFPropertyList

Facter::Util::CFPropertyList implementation

class to read, manipulate and write both XML and binary property list files (plist(5)) as defined by Apple. Have a look at Facter::Util::CFPropertyList::List for more documentation.

Example

require 'cfpropertylist'

# create a arbitrary data structure of basic data types
data = {
  'name' => 'John Doe',
  'missing' => true,
  'last_seen' => Time.now,
  'friends' => ['Jane Doe','Julian Doe'],
  'likes' => {
    'me' => false
  }
}

# create Facter::Util::CFPropertyList::List object
plist = Facter::Util::CFPropertyList::List.new

# call Facter::Util::CFPropertyList.guess() to create corresponding CFType values
# pass in optional :convert_unknown_to_string => true to convert things like symbols into strings.
plist.value = Facter::Util::CFPropertyList.guess(data)

# write plist to file
plist.save("example.plist", Facter::Util::CFPropertyList::List::FORMAT_BINARY)

# … later, read it again
plist = Facter::Util::CFPropertyList::List.new(:file => "example.plist")
data = Facter::Util::CFPropertyList.native_types(plist.value)

Author

Christian Kruse (cjk@wwwtech.de)

Copyright

Copyright (c) 2010

License

MIT License

Public Class Methods

guess(object, options = {}) click to toggle source

Create CFType hierarchy by guessing the correct CFType, e.g.

x = {
  'a' => ['b','c','d']
}
cftypes = Facter::Util::CFPropertyList.guess(x)

pass optional options hash. Only possible value actually:

convert_unknown_to_string

Convert unknown objects to string calling to_str()

converter_method

Convert unknown objects to known objects calling method_name

cftypes = Facter::Util::CFPropertyList.guess(x,:convert_unknown_to_string => true,:converter_method => :to_hash, :converter_with_opts => true)
# File lib/facter/util/cfpropertylist/lib/rbCFPropertyList.rb, line 136
def guess(object, options = {})
  case object
  when Fixnum, Integer       then CFInteger.new(object)
  when Float                 then CFReal.new(object)
  when TrueClass, FalseClass then CFBoolean.new(object)

  when String
    object.blob? ? CFData.new(object, CFData::DATA_RAW) : CFString.new(object)

  when Time, DateTime, Date  then CFDate.new(object)

  when Array, Enumerator, Enumerable::Enumerator
    ary = Array.new
    object.each do |o|
      ary.push Facter::Util::CFPropertyList.guess(o, options)
    end
    CFArray.new(ary)

  when Hash
    hsh = Hash.new
    object.each_pair do |k,v|
      k = k.to_s if k.is_a?(Symbol)
      hsh[k] = Facter::Util::CFPropertyList.guess(v, options)
    end
    CFDictionary.new(hsh)
  else
    case
    when Object.const_defined?('BigDecimal') && object.is_a?(BigDecimal)
      CFReal.new(object)
    when object.respond_to?(:read)
      CFData.new(object.read(), CFData::DATA_RAW)
    when options[:converter_method] && object.respond_to?(options[:converter_method])
      if options[:converter_with_opts]
        Facter::Util::CFPropertyList.guess(object.send(options[:converter_method],options),options)
      else
        Facter::Util::CFPropertyList.guess(object.send(options[:converter_method]),options)
      end
    when options[:convert_unknown_to_string]
      CFString.new(object.to_s)
    else
      raise CFTypeError.new("Unknown class #{object.class.to_s}. Try using :convert_unknown_to_string if you want to use unknown object types!")
    end
  end
end
native_types(object,keys_as_symbols=false) click to toggle source

Converts a CFType hiercharchy to native Ruby types

# File lib/facter/util/cfpropertylist/lib/rbCFPropertyList.rb, line 182
def native_types(object,keys_as_symbols=false)
  return if object.nil?

  if(object.is_a?(CFDate) || object.is_a?(CFString) || object.is_a?(CFInteger) || object.is_a?(CFReal) || object.is_a?(CFBoolean)) then
    return object.value
  elsif(object.is_a?(CFData)) then
    return object.decoded_value
  elsif(object.is_a?(CFArray)) then
    ary = []
    object.value.each do
      |v|
      ary.push Facter::Util::CFPropertyList.native_types(v)
    end

    return ary
  elsif(object.is_a?(CFDictionary)) then
    hsh = {}
    object.value.each_pair do
      |k,v|
      k = k.to_sym if keys_as_symbols
      hsh[k] = Facter::Util::CFPropertyList.native_types(v)
    end

    return hsh
  end
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.