Revision
2888
Author
lsansonetti@apple.com
Date
2009-10-23 16:28:49 -0700 (Fri, 23 Oct 2009)

Log Message

added YAML support for Struct objects

Modified Paths

Diff

Modified: MacRuby/trunk/ext/libyaml/rubyext.c (2887 => 2888)


--- 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 (2887 => 2888)


--- 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"