[macruby-changes] [4262] MacRuby/trunk

source_changes at macosforge.org source_changes at macosforge.org
Tue Jun 22 12:36:08 PDT 2010


Revision: 4262
          http://trac.macosforge.org/projects/ruby/changeset/4262
Author:   lsansonetti at apple.com
Date:     2010-06-22 12:36:08 -0700 (Tue, 22 Jun 2010)
Log Message:
-----------
added NSDate support in #to_plist + reformatted code

Modified Paths:
--------------
    MacRuby/trunk/objc.m
    MacRuby/trunk/spec/macruby/core/plist_spec.rb
    MacRuby/trunk/time.c

Modified: MacRuby/trunk/objc.m
===================================================================
--- MacRuby/trunk/objc.m	2010-06-22 09:45:54 UTC (rev 4261)
+++ MacRuby/trunk/objc.m	2010-06-22 19:36:08 UTC (rev 4262)
@@ -737,33 +737,54 @@
 {
     StringValue(str);
     VALUE bstr = rb_str_bstr(str);
-    NSData *data = [NSData dataWithBytes:rb_bstr_bytes(bstr) length:rb_bstr_length(bstr)];
+    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];
+    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]);
+	rb_raise(rb_eArgError, "error loading property list: '%s'",
+		[[err localizedDescription] UTF8String]);
     }
     return OC2RB(plist);
 }
 
+extern VALUE rb_cNSDate;
+
 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);
+    switch (TYPE(recv)) {
+	case T_STRING:
+	case T_FIXNUM:
+	case T_FLOAT:
+	case T_BIGNUM:
+	case T_HASH:
+	case T_ARRAY:
+	case T_TRUE:
+	case T_FALSE:
+	    break;
+
+	default:
+	    if (!rb_obj_is_kind_of(recv, rb_cNSDate)) {
+		rb_raise(rb_eArgError,
+			"object of class '%s' cannot be serialized to " \
+			"property-list format",
+			rb_obj_classname(recv));
+	    }
     }
-    
-    rb_raise(rb_eArgError, "class '%s' cannot be serialized to property-list format", rb_obj_classname(recv));
+
+    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);
 }
 
 void

Modified: MacRuby/trunk/spec/macruby/core/plist_spec.rb
===================================================================
--- MacRuby/trunk/spec/macruby/core/plist_spec.rb	2010-06-22 09:45:54 UTC (rev 4261)
+++ MacRuby/trunk/spec/macruby/core/plist_spec.rb	2010-06-22 19:36:08 UTC (rev 4262)
@@ -30,6 +30,11 @@
     load_plist(true.to_plist).should == true
     load_plist(false.to_plist).should == false
   end
+
+  it "should work with dates" do
+    time = Time.now
+    load_plist(time.to_plist).description.should == time.description
+  end
   
   it "should raise an TypeError when given something not a String" do
     lambda { load_plist(nil) }.should raise_error(TypeError)

Modified: MacRuby/trunk/time.c
===================================================================
--- MacRuby/trunk/time.c	2010-06-22 09:45:54 UTC (rev 4261)
+++ MacRuby/trunk/time.c	2010-06-22 19:36:08 UTC (rev 4262)
@@ -19,6 +19,7 @@
 #include "objc.h"
 
 VALUE rb_cTime;
+VALUE rb_cNSDate;
 static VALUE time_utc_offset _((VALUE, SEL));
 
 static ID id_divmod, id_mul, id_submicro;
@@ -2359,7 +2360,7 @@
     id_mul = rb_intern("*");
     id_submicro = rb_intern("submicro");
 
-    VALUE rb_cNSDate = (VALUE)objc_getClass("NSDate");
+    rb_cNSDate = (VALUE)objc_getClass("NSDate");
     assert(rb_cNSDate != 0);
 
     rb_cTime = rb_define_class("Time", rb_cNSDate);
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/macruby-changes/attachments/20100622/9b0c1700/attachment.html>


More information about the macruby-changes mailing list