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:
|
||||
# :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
|
||||
|
|
|
@ -2,14 +2,14 @@ require "cfpropertylist/rbCFPropertyList"
|
|||
|
||||
module Plistifier #:nodoc:
|
||||
module PlistEncoding
|
||||
def to_plist(options = {})
|
||||
options[:plist_format] ||= CFPropertyList::List::FORMAT_BINARY
|
||||
|
||||
hashifier = PlistHashifier.new(self, options)
|
||||
|
||||
plist = CFPropertyList::List.new
|
||||
plist.value = CFPropertyList.guess(hashifier.to_hash, :convert_unknown_to_string => true)
|
||||
plist.to_str(options[:plist_format])
|
||||
# str = IO String
|
||||
# reading plist.load(:data => str)
|
||||
|
||||
attr_accessor :plist_item_options
|
||||
|
||||
def to_plist_item
|
||||
to_hash(plist_item_options)
|
||||
end
|
||||
|
||||
def to_hash(options = {})
|
||||
|
|
|
@ -1,45 +1,40 @@
|
|||
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
|
||||
class Base
|
||||
def render_with_plist(options = nil, extra_options = {}, &block)
|
||||
|
||||
plist = nil
|
||||
plist = options[:plist] unless options.nil?
|
||||
|
||||
plist = options.delete(:plist) unless options.nil?
|
||||
|
||||
if plist
|
||||
|
||||
if options[:plist_filename].blank?
|
||||
|
||||
unless filename = options.delete(:plist_filename)
|
||||
if plist.is_a? Array
|
||||
filename = plist.first.class.name.pluralize + ".plist"
|
||||
filename = plist.first.class.name.pluralize + ".plist"
|
||||
else
|
||||
filename = "#{plist.class.name}-#{plist.id}.plist"
|
||||
end
|
||||
else
|
||||
filename = options[:plist_filename]
|
||||
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(
|
||||
plist.to_plist(options),
|
||||
data,
|
||||
:type => Mime::PLIST,
|
||||
:filename => filename,
|
||||
:disposition => 'inline'
|
||||
|
|
Reference in a new issue