nicer code and new cfpropertylist

This commit is contained in:
Jeena Paradies 2010-06-18 23:23:00 +02:00
parent 33e7a64a84
commit 3215572541
3 changed files with 55 additions and 37 deletions

View file

@ -72,12 +72,13 @@ module CFPropertyList
#
# pass optional options hash. Only possible value actually:
# :convert_unknown_to_string => true
# :converter_method => :method_name
#
# cftypes = CFPropertyList.guess(x,:convert_unknown_to_string => true)
def guess(object, options = {})
if(object.is_a?(Fixnum) || object.is_a?(Integer)) then
return CFInteger.new(object)
elsif(object.is_a?(Float) || object.is_a?(BigDecimal)) then
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)
@ -104,7 +105,9 @@ module CFPropertyList
end
return CFDictionary.new(hsh)
elsif options[:convert_unknown_to_string]
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!")
@ -290,4 +293,24 @@ module CFPropertyList
end
end
class Array
def to_plist(options={})
options[:plist_format] ||= CFPropertyList::List::FORMAT_BINARY
plist = CFPropertyList::List.new
plist.value = CFPropertyList.guess(self, options)
plist.to_str(options[:plist_format])
end
end
class Hash
def to_plist(options={})
options[:plist_format] ||= CFPropertyList::List::FORMAT_BINARY
plist = CFPropertyList::List.new
plist.value = CFPropertyList.guess(self, options)
plist.to_str(options[:plist_format])
end
end
# eof