[macruby-changes] [637] MacRuby/trunk

source_changes at macosforge.org source_changes at macosforge.org
Sun Sep 28 19:29:35 PDT 2008


Revision: 637
          http://trac.macosforge.org/projects/ruby/changeset/637
Author:   lsansonetti at apple.com
Date:     2008-09-28 19:29:34 -0700 (Sun, 28 Sep 2008)
Log Message:
-----------
adding Object#to_plist, HotCocoa#read_plist

Modified Paths:
--------------
    MacRuby/trunk/lib/hotcocoa.rb

Added Paths:
-----------
    MacRuby/trunk/lib/hotcocoa/plist.rb
    MacRuby/trunk/test/hotcocoa/
    MacRuby/trunk/test/hotcocoa/test_plist.rb

Added: MacRuby/trunk/lib/hotcocoa/plist.rb
===================================================================
--- MacRuby/trunk/lib/hotcocoa/plist.rb	                        (rev 0)
+++ MacRuby/trunk/lib/hotcocoa/plist.rb	2008-09-29 02:29:34 UTC (rev 637)
@@ -0,0 +1,45 @@
+module HotCocoa
+  def read_plist(data, mutability=:all)
+    mutability = case mutability
+      when :none
+        NSPropertyListImmutable
+      when :containers_only
+        NSPropertyListMutableContainers
+      when :all
+        NSPropertyListMutableContainersAndLeaves
+      else
+        raise ArgumentError, "invalid mutability `#{mutability}'"
+    end
+    if data.is_a?(String)
+      data = data.dataUsingEncoding(NSASCIIStringEncoding)
+      if data.nil?
+        raise ArgumentError, "cannot convert string `#{data}' to data"
+      end
+    end
+    # TODO retrieve error description and raise it if there is an error.
+    NSPropertyListSerialization.propertyListFromData(data,
+      mutabilityOption:mutability,
+      format:nil,
+      errorDescription:nil)
+  end
+end
+
+class Object
+  def to_plist(format=:xml)
+    format = case format
+      when :xml
+        NSPropertyListXMLFormat_v1_0 
+      when :binary
+        NSPropertyListBinaryFormat_v1_0
+      when :open_step
+        NSPropertyListOpenStepFormat 
+      else
+        raise ArgumentError, "invalid format `#{format}'"
+    end
+    # TODO retrieve error description and raise it if there is an error.
+    data = NSPropertyListSerialization.dataFromPropertyList(self,
+      format:format,
+      errorDescription:nil)
+    NSMutableString.alloc.initWithData(data, encoding:NSASCIIStringEncoding) 
+  end
+end

Modified: MacRuby/trunk/lib/hotcocoa.rb
===================================================================
--- MacRuby/trunk/lib/hotcocoa.rb	2008-09-29 01:51:41 UTC (rev 636)
+++ MacRuby/trunk/lib/hotcocoa.rb	2008-09-29 02:29:34 UTC (rev 637)
@@ -16,5 +16,6 @@
 require 'hotcocoa/data_sources/table_data_source'
 require 'hotcocoa/data_sources/combo_box_data_source'
 require 'hotcocoa/kernel_ext'
+require 'hotcocoa/plist'
 
 HotCocoa::Mappings.reload

Added: MacRuby/trunk/test/hotcocoa/test_plist.rb
===================================================================
--- MacRuby/trunk/test/hotcocoa/test_plist.rb	                        (rev 0)
+++ MacRuby/trunk/test/hotcocoa/test_plist.rb	2008-09-29 02:29:34 UTC (rev 637)
@@ -0,0 +1,28 @@
+require 'test/unit'
+require 'hotcocoa'
+
+class TestPlist < Test::Unit::TestCase
+  include HotCocoa
+
+  def test_to_plist
+    assert_plist(123)
+    assert_plist(true)
+    assert_plist(false)
+    assert_plist('foo')
+    assert_plist(:foo, 'foo')
+    assert_plist([1,2,3])
+    assert_plist({'un' => 1, 'deux' => 2})
+  end
+
+  def test_to_plist_with_invalid_objects
+    assert_plist(nil, nil)
+    assert_plist(Object.new, nil)
+    assert_plist(/foo/, nil)
+  end
+
+  private
+
+  def assert_plist(val, expected=val)
+    assert_equal(expected, read_plist(val.to_plist))
+  end
+end
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.macosforge.org/pipermail/macruby-changes/attachments/20080928/7df3172b/attachment-0001.html 


More information about the macruby-changes mailing list