[macruby-changes] [4258] MacRuby/trunk

source_changes at macosforge.org source_changes at macosforge.org
Mon Jun 21 17:26:46 PDT 2010


Revision: 4258
          http://trac.macosforge.org/projects/ruby/changeset/4258
Author:   pthomson at apple.com
Date:     2010-06-21 17:26:46 -0700 (Mon, 21 Jun 2010)
Log Message:
-----------
Add Object#to_plist and Kernel#load_plist for property list manipulation.

Modified Paths:
--------------
    MacRuby/trunk/objc.m

Added Paths:
-----------
    MacRuby/trunk/spec/macruby/core/plist_spec.rb

Modified: MacRuby/trunk/objc.m
===================================================================
--- MacRuby/trunk/objc.m	2010-06-21 22:12:21 UTC (rev 4257)
+++ MacRuby/trunk/objc.m	2010-06-22 00:26:46 UTC (rev 4258)
@@ -732,11 +732,47 @@
     [obj didChangeValueForKey:key];
 }
 
+static VALUE
+rb_objc_load_plist(VALUE recv, SEL sel, VALUE str)
+{
+    StringValue(str);
+    VALUE bstr = rb_str_bstr(str);
+    NSData *data = [NSData dataWithBytes:rb_bstr_bytes(bstr) length:rb_bstr_length(bstr)];
+    NSError *err = nil;
+    id plist = [NSPropertyListSerialization propertyListWithData:data options:0 format:NULL error:&err];
+    if (plist == nil) {
+        rb_raise(rb_eArgError, "error loading property list: '%s'", [[err localizedDescription] UTF8String]);
+    }
+    return OC2RB(plist);
+}
+
+static VALUE
+rb_objc_to_plist(VALUE recv, SEL sel)
+{
+    const int type = TYPE(recv);
+    if ((type == T_STRING) || (type == T_FIXNUM) || (type == T_FLOAT) || (type == T_BIGNUM) ||
+        (type == T_HASH) || (type == T_ARRAY) || (type == T_TRUE) || (type == T_FALSE)) {
+        id objc_obj = RB2OC(recv);
+        NSError *err = nil;
+        NSData *data = [NSPropertyListSerialization dataWithPropertyList:objc_obj format:NSPropertyListXMLFormat_v1_0 options:0 error:&err];
+        if (data == nil) {
+            rb_raise(rb_eArgError, "error serializing property list: '%s'", [[err localizedDescription] UTF8String]);
+        }
+        const uint8_t* bytes = [data bytes];
+        const size_t len = [data length];
+        return rb_bstr_new_with_data(bytes, len);
+    }
+    
+    rb_raise(rb_eArgError, "class '%s' cannot be serialized to property-list format", rb_obj_classname(recv));
+}
+
 void
 Init_ObjC(void)
 {
-    rb_objc_define_method(rb_mKernel, "load_bridge_support_file",
-	    rb_objc_load_bs, 1);
+    rb_objc_define_module_function(rb_mKernel, "load_bridge_support_file", rb_objc_load_bs, 1);
+    rb_objc_define_module_function(rb_mKernel, "load_plist", rb_objc_load_plist, 1);
+    
+    rb_objc_define_method(rb_cObject, "to_plist", rb_objc_to_plist, 0);
 
     Class k = objc_getClass("NSKeyValueUnnestedProperty");
     assert(k != NULL);

Added: MacRuby/trunk/spec/macruby/core/plist_spec.rb
===================================================================
--- MacRuby/trunk/spec/macruby/core/plist_spec.rb	                        (rev 0)
+++ MacRuby/trunk/spec/macruby/core/plist_spec.rb	2010-06-22 00:26:46 UTC (rev 4258)
@@ -0,0 +1,56 @@
+require File.expand_path("../../spec_helper", __FILE__)
+
+describe "Kernel#load_plist" do
+  it "should work with Fixnums" do
+    load_plist(100.to_plist).should == 100
+    load_plist(-42.to_plist).should == -42
+  end
+  
+  it "should work with Strings" do
+    load_plist("hello there".to_plist).should == "hello there"
+    load_plist("MacRuby \n FTW".to_plist).should == "MacRuby \n FTW"
+  end
+  
+  it "should work with Bignums" do
+    bn = 100_000_000_000_000
+    load_plist(bn.to_plist).should == bn
+  end
+  
+  it "should work with arrays" do
+    arr = [1, "two", 3.0, true, false]
+    load_plist(arr.to_plist).should == arr
+  end
+  
+  it "should work with simple hashes" do
+    hash = { "a" => "b", "c" => "d", "e" => "f"}
+    load_plist(hash.to_plist).should == hash
+  end
+  
+  it "should work with booleans" do
+    load_plist(true.to_plist).should == true
+    load_plist(false.to_plist).should == false
+  end
+  
+  it "should raise an TypeError when given something not a String" do
+    lambda { load_plist(nil) }.should raise_error(TypeError)
+    lambda { load_plist(42) }.should raise_error(TypeError)
+  end
+  
+  it "should raise an ArgumentError when given invalid String data" do
+    lambda { load_plist("hello dolly") }.should raise_error(ArgumentError)
+  end
+end
+
+describe "Kernel#to_plist" do
+  it "should serialize everything to a string" do
+    100.to_plist.should be_kind_of(String)
+    "hello".to_plist.should be_kind_of(String)
+    true.to_plist.should be_kind_of(String)
+    false.to_plist.should be_kind_of(String)
+  end
+  
+  it "should raise an ArgumentError if given a non-serializable data type" do
+    lambda { Object.new.to_plist }.should raise_error(ArgumentError)
+  end
+  
+end
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/macruby-changes/attachments/20100621/dd108bc8/attachment.html>


More information about the macruby-changes mailing list