aboutsummaryrefslogtreecommitdiffstats
path: root/lib/yaml.rb
diff options
context:
space:
mode:
authorwhy <why@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2003-05-09 21:25:50 +0000
committerwhy <why@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2003-05-09 21:25:50 +0000
commit55f4dc4c9a5345c28d0da750d1ee00fbb0870885 (patch)
tree904359659b75882365348decb2ca0789ca1ac803 /lib/yaml.rb
parent605adb86e2c2889c13e07cadf328f8032eebae7c (diff)
downloadruby-55f4dc4c9a5345c28d0da750d1ee00fbb0870885.tar.gz
Initial checkin of YAML substances.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@3772 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'lib/yaml.rb')
-rw-r--r--lib/yaml.rb119
1 files changed, 119 insertions, 0 deletions
diff --git a/lib/yaml.rb b/lib/yaml.rb
new file mode 100644
index 0000000000..3b669d3723
--- /dev/null
+++ b/lib/yaml.rb
@@ -0,0 +1,119 @@
+# vim:sw=4:ts=4
+# $Id$
+#
+# YAML.rb
+#
+# Loads the parser/loader and emitter/writer.
+#
+
+module YAML
+
+ begin
+ require 'syck'
+ Parser = YAML::Syck::Parser
+ rescue LoadError
+ require 'yaml/parser'
+ Parser = YAML::Parser
+ end
+ require 'yaml/emitter'
+ require 'yaml/rubytypes'
+
+ #
+ # Allocate blank object
+ #
+ def YAML.object_maker( obj_class, val )
+ if Hash === val
+ name = obj_class.name
+ o = ::Marshal.load( sprintf( "\004\006o:%c%s\000", name.length + 5, name ))
+ val.each_pair { |k,v|
+ o.instance_eval "@#{k} = v"
+ }
+ o
+ else
+ raise YAML::Error, "Invalid object explicitly tagged !ruby/Object: " + val.inspect
+ end
+ end
+
+ #
+ # Input methods
+ #
+
+ #
+ # Load a single document from the current stream
+ #
+ def YAML.load( io )
+ yp = YAML::Parser.new.parse( io )
+ end
+
+ #
+ # Parse a single document from the current stream
+ #
+ def YAML.parse( io )
+ yp = YAML::Parser.new( :Model => :Generic ).parse( io )
+ end
+
+ #
+ # Load all documents from the current stream
+ #
+ def YAML.each_document( io, &doc_proc )
+ yp = YAML::Parser.new.parse_documents( io, &doc_proc )
+ end
+
+ #
+ # Identical to each_document
+ #
+ def YAML.load_documents( io, &doc_proc )
+ YAML.each_document( io, &doc_proc )
+ end
+
+ #
+ # Parse all documents from the current stream
+ #
+ def YAML.each_node( io, &doc_proc )
+ yp = YAML::Parser.new( :Model => :Generic ).parse_documents( io, &doc_proc )
+ end
+
+ #
+ # Parse all documents from the current stream
+ #
+ def YAML.parse_documents( io, &doc_proc )
+ YAML.each_node( io, &doc_proc )
+ end
+
+ #
+ # Load all documents from the current stream
+ #
+ def YAML.load_stream( io )
+ yp = YAML::Parser.new
+ d = nil
+ yp.parse_documents( io ) { |doc|
+ d = YAML::Stream.new( yp.options ) if not d
+ d.add( doc )
+ }
+ return d
+ end
+
+end
+
+#
+# ryan: You know how Kernel.p is a really convenient way to dump ruby
+# structures? The only downside is that it's not as legible as
+# YAML.
+#
+# _why: (listening)
+#
+# ryan: I know you don't want to urinate all over your users' namespaces.
+# But, on the other hand, convenience of dumping for debugging is,
+# IMO, a big YAML use case.
+#
+# _why: Go nuts! Have a pony parade!
+#
+# ryan: Either way, I certainly will have a pony parade.
+#
+module Kernel
+ def y( x )
+ puts x.to_yaml
+ end
+end
+
+