Module: CFPropertyList

Defined in:
lib/cfpropertylist/rbCFPropertyList.rb,
lib/cfpropertylist/rbCFTypes.rb,
lib/cfpropertylist/rbREXMLParser.rb,
lib/cfpropertylist/rbLibXMLParser.rb,
lib/cfpropertylist/rbCFPropertyList.rb,
lib/cfpropertylist/rbNokogiriParser.rb,
lib/cfpropertylist/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, Blob, CFArray, CFBoolean, CFData, CFDate, CFDictionary, CFInteger, CFReal, CFString, CFType, CFUid, LibXMLParser, List, NokogiriXMLParser, ParserInterface, ReXMLParser, UidFixnum, XMLParserInterface

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.xml_parser_interfaceObject

Returns the value of attribute xml_parser_interface.



47
48
49
# File 'lib/cfpropertylist/rbCFPropertyList.rb', line 47

def xml_parser_interface
  @xml_parser_interface
end

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, :converter_with_opts => true)


129
130
131
132
133
134
135
136
137
138
139
140
141
142
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
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/cfpropertylist/rbCFPropertyList.rb', line 129

def guess(object, options = {})
  case object
  when Fixnum, Integer       then CFInteger.new(object)
  when UidFixnum             then CFUid.new(object)
  when Float                 then CFReal.new(object)
  when TrueClass, FalseClass then CFBoolean.new(object)

  when Blob
    CFData.new(object, CFData::DATA_RAW)

  when String
    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 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] = 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)
      raw_data = object.read
      # treat the data as a bytestring (ASCII-8BIT) if Ruby supports it.  Do this by forcing
      # the encoding, on the assumption that the bytes were read correctly, and just tagged with
      # an inappropriate encoding, rather than transcoding.
      raw_data.force_encoding(Encoding::ASCII_8BIT) if raw_data.respond_to?(:force_encoding)
      CFData.new(raw_data, CFData::DATA_RAW)
    when options[:converter_method] && object.respond_to?(options[:converter_method])
      if options[:converter_with_opts]
        CFPropertyList.guess(object.send(options[:converter_method],options),options)
      else
        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) ⇒ Object

Converts a CFType hiercharchy to native Ruby types



184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/cfpropertylist/rbCFPropertyList.rb', line 184

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)) || object.is_a?(CFUid) then
    return object.value
  elsif(object.is_a?(CFData)) then
    return CFPropertyList::Blob.new(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