nicer code and new cfpropertylist
This commit is contained in:
parent
33e7a64a84
commit
3215572541
3 changed files with 55 additions and 37 deletions
|
@ -72,12 +72,13 @@ module CFPropertyList
|
||||||
#
|
#
|
||||||
# pass optional options hash. Only possible value actually:
|
# pass optional options hash. Only possible value actually:
|
||||||
# :convert_unknown_to_string => true
|
# :convert_unknown_to_string => true
|
||||||
|
# :converter_method => :method_name
|
||||||
#
|
#
|
||||||
# cftypes = CFPropertyList.guess(x,:convert_unknown_to_string => true)
|
# cftypes = CFPropertyList.guess(x,:convert_unknown_to_string => true)
|
||||||
def guess(object, options = {})
|
def guess(object, options = {})
|
||||||
if(object.is_a?(Fixnum) || object.is_a?(Integer)) then
|
if(object.is_a?(Fixnum) || object.is_a?(Integer)) then
|
||||||
return CFInteger.new(object)
|
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)
|
return CFReal.new(object)
|
||||||
elsif(object.is_a?(TrueClass) || object.is_a?(FalseClass)) then
|
elsif(object.is_a?(TrueClass) || object.is_a?(FalseClass)) then
|
||||||
return CFBoolean.new(object)
|
return CFBoolean.new(object)
|
||||||
|
@ -104,7 +105,9 @@ module CFPropertyList
|
||||||
end
|
end
|
||||||
|
|
||||||
return CFDictionary.new(hsh)
|
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)
|
return CFString.new(object.to_s)
|
||||||
else
|
else
|
||||||
raise CFTypeError.new("Unknown class #{object.class.to_s}! Try using :convert_unknown_to_string if you want to use unknown object types!")
|
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
|
||||||
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
|
# eof
|
||||||
|
|
|
@ -2,14 +2,14 @@ require "cfpropertylist/rbCFPropertyList"
|
||||||
|
|
||||||
module Plistifier #:nodoc:
|
module Plistifier #:nodoc:
|
||||||
module PlistEncoding
|
module PlistEncoding
|
||||||
def to_plist(options = {})
|
|
||||||
options[:plist_format] ||= CFPropertyList::List::FORMAT_BINARY
|
|
||||||
|
|
||||||
hashifier = PlistHashifier.new(self, options)
|
# str = IO String
|
||||||
|
# reading plist.load(:data => str)
|
||||||
|
|
||||||
plist = CFPropertyList::List.new
|
attr_accessor :plist_item_options
|
||||||
plist.value = CFPropertyList.guess(hashifier.to_hash, :convert_unknown_to_string => true)
|
|
||||||
plist.to_str(options[:plist_format])
|
def to_plist_item
|
||||||
|
to_hash(plist_item_options)
|
||||||
end
|
end
|
||||||
|
|
||||||
def to_hash(options = {})
|
def to_hash(options = {})
|
||||||
|
|
|
@ -1,45 +1,40 @@
|
||||||
require "cfpropertylist/rbCFPropertyList"
|
require "cfpropertylist/rbCFPropertyList"
|
||||||
|
|
||||||
class Array
|
|
||||||
def to_plist(options = {})
|
|
||||||
options[:plist_format] ||= CFPropertyList::List::FORMAT_BINARY
|
|
||||||
|
|
||||||
array = []
|
|
||||||
self.each do |a|
|
|
||||||
if a.is_a? ActiveRecord::Base
|
|
||||||
array << a.to_hash(options)
|
|
||||||
else
|
|
||||||
array << a
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
plist = CFPropertyList::List.new
|
|
||||||
plist.value = CFPropertyList.guess(array, :convert_unknown_to_string => true)
|
|
||||||
plist.to_str(options[:plist_format])
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
module ActionController
|
module ActionController
|
||||||
class Base
|
class Base
|
||||||
def render_with_plist(options = nil, extra_options = {}, &block)
|
def render_with_plist(options = nil, extra_options = {}, &block)
|
||||||
|
|
||||||
plist = nil
|
plist = options.delete(:plist) unless options.nil?
|
||||||
plist = options[:plist] unless options.nil?
|
|
||||||
|
|
||||||
if plist
|
if plist
|
||||||
|
|
||||||
if options[:plist_filename].blank?
|
unless filename = options.delete(:plist_filename)
|
||||||
if plist.is_a? Array
|
if plist.is_a? Array
|
||||||
filename = plist.first.class.name.pluralize + ".plist"
|
filename = plist.first.class.name.pluralize + ".plist"
|
||||||
else
|
else
|
||||||
filename = "#{plist.class.name}-#{plist.id}.plist"
|
filename = "#{plist.class.name}-#{plist.id}.plist"
|
||||||
end
|
end
|
||||||
else
|
|
||||||
filename = options[:plist_filename]
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
unless options.nil?
|
||||||
|
if plist.is_a? Array
|
||||||
|
plist.each do |entry|
|
||||||
|
if entry.respond_to? :plist_item_options=
|
||||||
|
entry.plist_item_options = options
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
plist_options = {
|
||||||
|
:converter_method => :to_plist_item,
|
||||||
|
:convert_unknown_to_string => true
|
||||||
|
}
|
||||||
|
|
||||||
|
data = plist.is_a?(CFPropertyList::List) ? plist : plist.to_plist(plist_options)
|
||||||
|
|
||||||
send_data(
|
send_data(
|
||||||
plist.to_plist(options),
|
data,
|
||||||
:type => Mime::PLIST,
|
:type => Mime::PLIST,
|
||||||
:filename => filename,
|
:filename => filename,
|
||||||
:disposition => 'inline'
|
:disposition => 'inline'
|
||||||
|
|
Reference in a new issue