[macruby-changes] [2174] MacRuby/branches/experimental

source_changes at macosforge.org source_changes at macosforge.org
Mon Aug 3 10:45:48 PDT 2009


Revision: 2174
          http://trac.macosforge.org/projects/ruby/changeset/2174
Author:   pthomson at apple.com
Date:     2009-08-03 10:45:48 -0700 (Mon, 03 Aug 2009)
Log Message:
-----------
Okay, most of the built-in types coerce to YAML properly.

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

Modified: MacRuby/branches/experimental/ext/libyaml/rubyext.c
===================================================================
--- MacRuby/branches/experimental/ext/libyaml/rubyext.c	2009-08-03 17:45:46 UTC (rev 2173)
+++ MacRuby/branches/experimental/ext/libyaml/rubyext.c	2009-08-03 17:45:48 UTC (rev 2174)
@@ -144,6 +144,7 @@
 		yaml_node_t *node = yaml_document_get_node(document, nodeID);
 		VALUE n = rb_yaml_node_new(node, nodeID, self);
 		rb_vm_yield(1, &n);
+		return n;
 	}
 	return self;
 }
@@ -159,6 +160,7 @@
 		yaml_node_t *node = yaml_document_get_node(document, nodeID);
 		VALUE n = rb_yaml_node_new(node, nodeID, self);
 		rb_vm_yield(1, &n);
+		return n;
 	}
 	return self;
 }
@@ -453,10 +455,10 @@
 	//rb_objc_define_method(rb_cNode, "end_mark", rb_yaml_node_end_mark, 0);
 	
 	rb_cSeqNode = rb_define_class_under(rb_mLibYAML, "Seq", rb_cNode);
-	rb_objc_define_method(rb_cNode, "add", rb_sequence_node_add, 1);
+	rb_objc_define_method(rb_cSeqNode, "add", rb_sequence_node_add, 1);
 	
 	rb_cMapNode = rb_define_class_under(rb_mLibYAML, "Map", rb_cNode);
-	rb_objc_define_method(rb_cNode, "add", rb_mapping_node_add, 2);
+	rb_objc_define_method(rb_cMapNode, "add", rb_mapping_node_add, 2);
 	
 	rb_cResolver = rb_define_class_under(rb_mLibYAML, "Resolver", rb_cObject);
 	rb_define_attr(rb_cResolver, "tags", 1, 1);
@@ -469,15 +471,6 @@
 	rb_oDefaultResolver = rb_vm_call(rb_cResolver, selNew, 0, NULL, true);
 	rb_define_const(rb_mLibYAML, "DEFAULT_RESOLVER", rb_oDefaultResolver);
 	
-	#if 0
-	rb_cOut = rb_define_class_under(rb_mLibYAML, "Out", rb_cObject);
-    rb_define_attr(cOut, "document", 1, 1 );
-    rb_objc_define_method(rb_cOut, "initialize", rb_yaml_out_initialize, 1);
-    rb_objc_define_method(rb_cOut, "map", rb_yaml_out_map, -1);
-    rb_objc_define_method(rb_cOut, "seq", rb_yaml_out_seq, -1);
-    rb_objc_define_method(rb_cOut, "scalar", rb_yaml_out_scalar, -1);
-	#endif
-	
 	rb_cEmitter = rb_define_class_under(rb_mLibYAML, "Emitter", rb_cObject);
 	rb_objc_define_method(*(VALUE *)rb_cEmitter, "alloc", rb_yaml_emitter_alloc, 0);
 	rb_define_attr(rb_cEmitter, "output", 1, 1);

Modified: MacRuby/branches/experimental/lib/yaml/rubytypes.rb
===================================================================
--- MacRuby/branches/experimental/lib/yaml/rubytypes.rb	2009-08-03 17:45:46 UTC (rev 2173)
+++ MacRuby/branches/experimental/lib/yaml/rubytypes.rb	2009-08-03 17:45:48 UTC (rev 2174)
@@ -15,6 +15,31 @@
       @taguri || tag
     end
   end
+  
+  yaml_as "tag:ruby.yaml.org,2002:object"
+  
+  def to_yaml_style
+    nil
+  end
+  
+  def to_yaml_properties
+    self.instance_variables.sort
+  end
+  
+  def to_yaml(out)
+    if out.respond_to? :map
+      out.map(taguri, to_yaml_style) do |map|
+        to_yaml_properties.each do |m|
+          # The [1..-1] strips the @ from the variable name.
+          out.add(m[1..-1], instance_variable_get(m))
+        end
+      end
+    elsif out.respond_to? :add
+      to_yaml_properties.each do |m|
+        out.add(m[1..-1], instance_variable_get(m))
+      end
+    end
+  end
 end
 
 class String
@@ -24,10 +49,20 @@
   end
 end
 
+class Exception
+  yaml_as "tag:ruby.yaml.org,2002:exception"
+  def to_yaml(out)
+    out.map(taguri, to_yaml_style) do |map|
+      map.add('message', message)
+    end
+    super(out)
+  end
+end
+
 class Array
   yaml_as "tag:yaml.org,2002:seq"
   def to_yaml(out)
-    out.seq(taguri, nil) do |seq|
+    out.seq(taguri, to_yaml_style) do |seq|
       each { |i| seq.add(i) }
     end
   end
@@ -36,7 +71,7 @@
 class Hash
   yaml_as "tag:yaml.org,2002:map"
   def to_yaml(out)
-    out.map(taguri, nil) do |map| 
+    out.map(taguri, to_yaml_style) do |map| 
       each { |k,v| map.add(k,v) }
     end
   end
@@ -51,7 +86,7 @@
 
 class Float
   yaml_as "tag:yaml.org,2002:float"
-  def to_yaml
+  def to_yaml(out)
     str = self.to_s
     if str == "Infinity"
       str = ".Inf"
@@ -64,6 +99,14 @@
   end
 end
 
+class Regexp
+  yaml_as "tag:ruby.yaml.org,2002:regexp"
+  def to_yaml(out)
+    out.scalar(taguri, self.inspect, :plain)
+    super(out)
+	end
+end
+
 class NilClass 
   yaml_as "tag:yaml.org,2002:null"
 	def to_yaml(out)
@@ -72,14 +115,14 @@
 end
 
 class TrueClass
-  yaml_as "tag:yaml.org,2002:bool#yes"
+  yaml_as "tag:yaml.org,2002:bool"
   def to_yaml(out)
     out.scalar(taguri, "true", :plain)
   end
 end
 
 class FalseClass
-  yaml_as "tag:yaml.org,2002:bool#no"
+  yaml_as "tag:yaml.org,2002:bool"
   def to_yaml(out)
     out.scalar(taguri, "false", :plain)
   end

Modified: MacRuby/branches/experimental/lib/yaml.rb
===================================================================
--- MacRuby/branches/experimental/lib/yaml.rb	2009-08-03 17:45:46 UTC (rev 2173)
+++ MacRuby/branches/experimental/lib/yaml.rb	2009-08-03 17:45:48 UTC (rev 2174)
@@ -43,6 +43,12 @@
   
 end
 
+module Kernel
+  def y(*objs)
+    objs.each { |obj| YAML::dump(obj, $stdout) }
+  end
+end
+
 =begin
 
 require 'stringio'
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/macruby-changes/attachments/20090803/4fcd66e5/attachment-0001.html>


More information about the macruby-changes mailing list