[macruby-changes] [2888] MacRuby/trunk

source_changes at macosforge.org source_changes at macosforge.org
Fri Oct 23 16:28:53 PDT 2009


Revision: 2888
          http://trac.macosforge.org/projects/ruby/changeset/2888
Author:   lsansonetti at apple.com
Date:     2009-10-23 16:28:49 -0700 (Fri, 23 Oct 2009)
Log Message:
-----------
added YAML support for Struct objects

Modified Paths:
--------------
    MacRuby/trunk/ext/libyaml/rubyext.c
    MacRuby/trunk/lib/yaml/rubytypes.rb

Modified: MacRuby/trunk/ext/libyaml/rubyext.c
===================================================================
--- MacRuby/trunk/ext/libyaml/rubyext.c	2009-10-23 23:23:51 UTC (rev 2887)
+++ MacRuby/trunk/ext/libyaml/rubyext.c	2009-10-23 23:28:49 UTC (rev 2888)
@@ -289,9 +289,15 @@
     }
 
     // Dynamic handler, only for pure objects.
+    VALUE outer = Qnil;
     if (strncmp((const char *)tag, "!ruby/object:", 13) == 0) {
+	outer = rb_cObject;
+    }
+    else if (strncmp((const char *)tag, "!ruby/struct:", 13) == 0) {
+	outer = rb_cStruct;
+    }
+    if (outer != Qnil) {
 	char *path = (char *)tag + 13;
-	VALUE outer = rb_cObject;
 	assert(strlen(path) < 100);
 
 	char *p;

Modified: MacRuby/trunk/lib/yaml/rubytypes.rb
===================================================================
--- MacRuby/trunk/lib/yaml/rubytypes.rb	2009-10-23 23:23:51 UTC (rev 2887)
+++ MacRuby/trunk/lib/yaml/rubytypes.rb	2009-10-23 23:28:49 UTC (rev 2888)
@@ -58,6 +58,30 @@
   def taguri; "!ruby/object:#{self.class}"; end
 end
 
+class Struct
+  yaml_as "tag:ruby.yaml.org,2002:struct"
+
+  def to_yaml(output = nil)
+    YAML::quick_emit(output) do |out|
+      out.map(taguri, to_yaml_style) do |map|
+        members.each do |m|
+          out.add(m, self[m])
+        end
+      end
+    end
+  end
+
+  def self.yaml_new(val)
+    obj = self.new
+    val.each { |k, v| obj[k] = v }
+    obj
+  end
+
+  private
+
+  def taguri; "!ruby/struct:#{self.class.to_s.sub(/^Struct::/, '')}"; end
+end
+
 class String
   yaml_as "tag:yaml.org,2002:str"
   
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/macruby-changes/attachments/20091023/2f3892a7/attachment.html>


More information about the macruby-changes mailing list