Module: CFPropertyList

Defined in:
lib/rbCFPropertyList.rb,
lib/rbCFTypes.rb,
lib/rbCFPropertyList.rb,
lib/rbXMLCFPropertyList.rb,
lib/rbBinaryCFPropertyList.rb

Overview

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 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 CFPropertyList::List object
plist = CFPropertyList::List.new

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

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

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

Christian Kruse ([email protected])

Copyright

Copyright © 2010

License

MIT License

Defined Under Namespace

Classes: Binary, CFArray, CFBoolean, CFData, CFDate, CFDictionary, CFInteger, CFReal, CFString, CFType, List, ParserInterface, XML

Class Method Summary collapse

Class Method Details

.guess(object, options = {}) ⇒ Object

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

x = {
  'a' => ['b','c','d']
}
cftypes = 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 = CFPropertyList.guess(x,:convert_unknown_to_string => true,:converter_method => :to_hash)


103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/rbCFPropertyList.rb', line 103

def guess(object, options = {})
  if(object.is_a?(Fixnum) || object.is_a?(Integer)) then
    return CFInteger.new(object)
  elsif(object.is_a?(Float) || (Object.const_defined?('BigDecimal') and object.is_a?(BigDecimal))) then
    return CFReal.new(object)
  elsif(object.is_a?(TrueClass) || object.is_a?(FalseClass)) then
    return CFBoolean.new(object)
  elsif(object.is_a?(String)) then
    return object.blob? ? CFData.new(object, CFData::DATA_RAW) : CFString.new(object)
  elsif(object.respond_to?(:read)) then
    return CFData.new(object.read(), CFData::DATA_RAW)
  elsif(object.is_a?(Time) || object.is_a?(DateTime) || object.is_a?(Date)) then
    return CFDate.new(object)
  elsif(object.is_a?(Array)) then
    ary = Array.new
    object.each do
      |o|
      ary.push CFPropertyList.guess(o, options)
    end

    return CFArray.new(ary)
  elsif(object.is_a?(Hash)) then
    hsh = Hash.new
    object.each_pair do
      |k,v|
      k = k.to_s if k.is_a?(Symbol)
      hsh[k] = CFPropertyList.guess(v, options)
    end

    return CFDictionary.new(hsh)
  elsif options[:converter_method] and object.respond_to?(options[:converter_method]) then
    return CFPropertyList.guess(object.send(options[:converter_method]))
  elsif options[:convert_unknown_to_string] then
    return 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

.native_types(object, keys_as_symbols = false) ⇒ Object

Converts a CFType hiercharchy to native Ruby types



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/rbCFPropertyList.rb', line 143

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 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] = CFPropertyList.native_types(v)
    end

    return hsh
  end
end