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

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


Revision: 2169
          http://trac.macosforge.org/projects/ruby/changeset/2169
Author:   pthomson at apple.com
Date:     2009-08-03 10:45:41 -0700 (Mon, 03 Aug 2009)
Log Message:
-----------
Further changes. Going to add soem methods to the Node subclasses to try and keep as compatible as possible with Syck.

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:40 UTC (rev 2168)
+++ MacRuby/branches/experimental/ext/libyaml/rubyext.c	2009-08-03 17:45:41 UTC (rev 2169)
@@ -25,6 +25,7 @@
 VALUE rb_cResolver;
 VALUE rb_cNode;
 VALUE rb_cScalar;
+VALUE rb_cOut;
 
 VALUE rb_oDefaultResolver;
 
@@ -114,7 +115,8 @@
 
 static VALUE rb_yaml_node_new(yaml_node_t *node);
 
-static VALUE rb_yaml_document_alloc(VALUE klass, SEL sel)
+static VALUE 
+rb_yaml_document_alloc(VALUE klass, SEL sel)
 {
 	yaml_document_t *document = ALLOC(yaml_document_t);
 	// XXX: still need to write a finalizer
@@ -122,7 +124,32 @@
 	return Data_Wrap_Struct(rb_cDocument, NULL, NULL, document);
 }
 
+static VALUE 
+rb_yaml_document_add_node(VALUE self, SEL sel, VALUE obj)
+{
+	yaml_document_t *document;
+	Data_Get_Struct(self, yaml_document_t, document);
+	rb_yaml_dump_object_to_document(document, obj);
+	return self;
+}
+
 static VALUE
+rb_yaml_document_add_sequence(VALUE self, SEL sel, VALUE taguri, VALUE style)
+{
+	yaml_document_t *document;
+	Data_Get_Struct(self, yaml_document_t, document);
+	// TODO: stop ignoring the style parameter
+	int nodeID = yaml_document_add_sequence(document, RSTRING_PTR(taguri), YAML_ANY_SEQUENCE_STYLE);
+	if (rb_block_given_p())
+	{
+		yaml_node_t *node = yaml_document_get_node(document, nodeID);
+		VALUE n = rb_yaml_node_new(node);
+		rb_yield(n);
+	}
+	return self;
+}
+
+static VALUE
 rb_yaml_document_root_node(VALUE self, SEL sel)
 {
 	yaml_document_t *document;
@@ -212,8 +239,9 @@
 	}
 	else 
 	{
-		// check the taguri
-		// hmm... put some more thought into this.
+		VALUE document = rb_vm_call(rb_cDocument, selNew, 0, NULL, true);
+		rb_vm_call_with_cache(obj, "to_yaml", 1, &document, true);
+		return document;
 	}
 	return Qnil;
 }
@@ -322,6 +350,7 @@
 	rb_cDocument = rb_define_class_under(rb_mLibYAML, "Document", rb_cObject);
 	//rb_objc_define_method(rb_cDocument, "<<", rb_yaml_document_add_node, 1);
 	rb_objc_define_method(rb_cDocument, "root", rb_yaml_document_root_node, 0);
+	rb_objc_define_method(rb_cDocument, "seq", rb_yaml_document_add_sequence, 2);
 	//rb_objc_define_method(rb_cDocument, "[]", rb_yaml_document_aref, 1);
 	//rb_objc_define_method(rb_cDocument, "version", rb_yaml_document_version, 0);
 	rb_objc_define_method(rb_cDocument, "implicit_start?", rb_yaml_document_implicit_start_p, 0);
@@ -352,6 +381,15 @@
 	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:40 UTC (rev 2168)
+++ MacRuby/branches/experimental/lib/yaml/rubytypes.rb	2009-08-03 17:45:41 UTC (rev 2169)
@@ -1,6 +1,38 @@
 # -*- mode: ruby; ruby-indent-level: 4; tab-width: 4 -*- vim: sw=4 ts=4
-require 'date'
+# require 'date'
 
+class Object
+  def yaml_as(tag)
+    class_eval <<-EOS
+      def taguri
+        @taguri || '#{tag}'
+      end
+      
+      def taguri=(t)
+        @taguri = t
+      end
+    EOS
+  end
+end
+
+class String
+  yaml_as "tag:yaml.org,2002:str"
+  def to_yaml(out)
+    out.scalar(taguri, self, self =~ /^:/ ? :quote2 : to_yaml_style)
+  end
+end
+
+class Array
+  yaml_as "tag:yaml.org,2002:seq"
+  def to_yaml(out)
+    out.seq(taguri, to_yaml_style) do |seq|
+      each { |i| seq.add(i) }
+    end
+  end
+end
+
+=begin
+
 class Class
 	def to_yaml( opts = {} )
 		raise TypeError, "can't dump anonymous class %s" % self.class
@@ -444,3 +476,4 @@
 	end
 end
 
+=end
\ No newline at end of file

Modified: MacRuby/branches/experimental/lib/yaml.rb
===================================================================
--- MacRuby/branches/experimental/lib/yaml.rb	2009-08-03 17:45:40 UTC (rev 2168)
+++ MacRuby/branches/experimental/lib/yaml.rb	2009-08-03 17:45:41 UTC (rev 2169)
@@ -7,41 +7,38 @@
 # 
 
 require 'libyaml'
+require 'yaml/rubytypes'
 
 module YAML
   
   def YAML.parser
-    Parser.new
+
   end
   
   def YAML.emitter
-    Emitter.new
+
   end
   
   def YAML.dump(obj, io=nil)
-    obj.to_yaml(io)
+
   end
   
   def YAML.load(io)
-    parser.load(io)
+
   end
   
   def YAML.load_file(path)
-    File.open(path) { |i| load(i) }
+
   end
   
   def YAML.parse(io)
-    parser.parse(io)
+
   end
   
   def YAML.parse_file(path)
-    File.open(path) { |i| parse(i) }
+
   end
   
-  def YAML.quick_emit(obj, opts={}, &block)
-    emitter.emit(obj)
-  end
-  
 end
 
 =begin
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/macruby-changes/attachments/20090803/b6f4b26f/attachment.html>


More information about the macruby-changes mailing list