summaryrefslogtreecommitdiff
path: root/lib/yaml.rb
diff options
context:
space:
mode:
authorwhy <why@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2005-09-13 03:58:33 +0000
committerwhy <why@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2005-09-13 03:58:33 +0000
commitf1827a2fafaa50ba89f35c8c05beffd28b1dd6e6 (patch)
tree095d144b8790cb1d1a99fbe40e0e79e31f65fcac /lib/yaml.rb
parent2007d73102b29e7bf50d08f6690ae5e6ffa5ea9a (diff)
* lib/yaml.rb: reworking YAML::Stream to use the new
emitter. * lib/yaml/stream.rb: ditto. * lib/yaml/rubytypes.rb: added Object#yaml_new. * lib/yaml/tag.rb: the tag_subclasses? method now shows up in the class. allow taguri to be set using an accessor. continue support of Object#to_yaml_type. * ext/syck/rubyext.c: new emitter code. yaml_new and yaml_initialize get called, should they be present. consolidated all the diaspora of internal node types into the family below YAML::Syck::Node -- Map, Seq, Scalar -- all of whom are SyckNode structs pointing to Ruby data. moved Object#yaml_new into the node_import and made it the default behavior. the target_class is always called wih yaml_new, prepended a parameter, which is the klass. loaded nodes through GenericResolver show their style. new Resolver#tagurize converts type ids to taguris. * ext/syck/implicit.re: were 'y' and 'n' seriously omitted?? * ext/syck/emitter.c: renovated emitter, walks the tree in advance. consolidated redundant block_styles struct into the scalar_style struct. (this means loaded nodes can now be sent back to emitter and preserve at least its very basic formatting.) * ext/syck/gram.c: headless documents of any kind allowed. * ext/syck/node.c: new syck_replace_str methods and syck_empty_* methods for rewriting node contents, while keeping the ID and other setup info. added syck_seq_assign. * ext/syck/syck.h: reflect block_styles and new node functions. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9141 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'lib/yaml.rb')
-rw-r--r--lib/yaml.rb119
1 files changed, 70 insertions, 49 deletions
diff --git a/lib/yaml.rb b/lib/yaml.rb
index 6837cc239d..5d409cfae3 100644
--- a/lib/yaml.rb
+++ b/lib/yaml.rb
@@ -6,8 +6,10 @@
# Author:: why the lucky stiff
#
+require 'stringio'
+require 'yaml/error'
require 'yaml/syck'
-require 'yaml/loader'
+require 'yaml/tag'
require 'yaml/stream'
# == YAML
@@ -73,7 +75,7 @@ require 'yaml/stream'
# ruby_obj == test_obj
# # => true
#
-# To register your custom types with the global loader, use +add_domain_type+.
+# To register your custom types with the global resolver, use +add_domain_type+.
#
# YAML::add_domain_type( "your-site.com,2004", "widget" ) do |type, val|
# Widget.new( val )
@@ -81,9 +83,21 @@ require 'yaml/stream'
#
module YAML
- @@parser = YAML::Syck::Parser
- @@loader = YAML::Syck::DefaultLoader
- @@emitter = YAML::Syck::Emitter
+ Resolver = YAML::Syck::Resolver
+ DefaultResolver = YAML::Syck::DefaultResolver
+ DefaultResolver.use_types_at( @@tagged_classes )
+ GenericResolver = YAML::Syck::GenericResolver
+ Parser = YAML::Syck::Parser
+ Emitter = YAML::Syck::Emitter
+
+ # Returns a new default parser
+ def YAML.parser; Parser.new.set_resolver( YAML.resolver ); end
+ # Returns a new generic parser
+ def YAML.generic_parser; Parser.new.set_resolver( GenericResolver ); end
+ # Returns the default resolver
+ def YAML.resolver; DefaultResolver; end
+ # Returns a new default emitter
+ def YAML.emitter; Emitter.new.set_resolver( YAML.resolver ); end
#
# Converts _obj_ to YAML and writes the YAML result to _io_.
@@ -99,9 +113,8 @@ module YAML
# #=> "--- :locked"
#
def YAML.dump( obj, io = nil )
- io ||= ""
- io << obj.to_yaml
- io
+ obj.to_yaml( io || io2 = StringIO.new )
+ io || ( io2.rewind; io2.read )
end
#
@@ -116,7 +129,7 @@ module YAML
# #=> :locked
#
def YAML.load( io )
- yp = @@parser.new.load( io )
+ yp = parser.load( io )
end
#
@@ -153,13 +166,13 @@ module YAML
#
# Can also load from a string.
#
- # YAML.load( "--- :locked" )
+ # YAML.parse( "--- :locked" )
# #=> #<YAML::Syck::Node:0x82edddc
# @type_id="tag:ruby.yaml.org,2002:sym",
# @value=":locked", @kind=:scalar>
#
def YAML.parse( io )
- yp = @@parser.new( :Model => :Generic ).load( io )
+ yp = generic_parser.load( io )
end
#
@@ -200,7 +213,7 @@ module YAML
# end
#
def YAML.each_document( io, &block )
- yp = @@parser.new.load_documents( io, &block )
+ yp = parser.load_documents( io, &block )
end
#
@@ -230,7 +243,7 @@ module YAML
# end
#
def YAML.each_node( io, &doc_proc )
- yp = @@parser.new( :Model => :Generic ).load_documents( io, &doc_proc )
+ yp = generic_parser.load_documents( io, &doc_proc )
end
#
@@ -254,12 +267,11 @@ module YAML
# loaded documents.
#
def YAML.load_stream( io )
- yp = @@parser.new
d = nil
- yp.load_documents( io ) { |doc|
- d = YAML::Stream.new( yp.options ) if not d
+ parser.load_documents( io ) do |doc|
+ d = YAML::Stream.new if not d
d.add( doc )
- }
+ end
return d
end
@@ -283,43 +295,50 @@ module YAML
#
# Add a global handler for a YAML domain type.
#
- def YAML.add_domain_type( domain, type_re, &transfer_proc )
- @@loader.add_domain_type( domain, type_re, &transfer_proc )
+ def YAML.add_domain_type( domain, type_tag, &transfer_proc )
+ resolver.add_type( "tag:#{ domain }:#{ type_tag }", transfer_proc )
end
#
# Add a transfer method for a builtin type
#
- def YAML.add_builtin_type( type_re, &transfer_proc )
- @@loader.add_builtin_type( type_re, &transfer_proc )
+ def YAML.add_builtin_type( type_tag, &transfer_proc )
+ resolver.add_type( "tag:yaml.org,2002:#{ type_tag }", transfer_proc )
end
#
# Add a transfer method for a builtin type
#
def YAML.add_ruby_type( type, &transfer_proc )
- @@loader.add_ruby_type( type, &transfer_proc )
+ resolver.add_type( "tag:ruby.yaml.org,2002:#{ type_tag }", transfer_proc )
end
#
# Add a private document type
#
def YAML.add_private_type( type_re, &transfer_proc )
- @@loader.add_private_type( type_re, &transfer_proc )
+ resolver.add_type( "x-private:" + type_re, transfer_proc )
end
#
# Detect typing of a string
#
def YAML.detect_implicit( val )
- @@loader.detect_implicit( val )
+ resolver.detect_implicit( val )
+ end
+
+ #
+ # Convert a type_id to a taguri
+ #
+ def YAML.tagurize( val )
+ resolver.tagurize( val )
end
#
# Apply a transfer method to a Ruby object
#
def YAML.transfer( type_id, obj )
- @@loader.transfer( type_id, obj )
+ resolver.transfer( YAML.tagurize( type_id ), obj )
end
#
@@ -358,24 +377,13 @@ module YAML
# Allocate an Emitter if needed
#
def YAML.quick_emit( oid, opts = {}, &e )
- old_opt = nil
- if opts[:Emitter].is_a? @@emitter
- out = opts.delete( :Emitter )
- old_opt = out.options.dup
- out.options.update( opts )
- else
- out = @@emitter.new( opts )
- end
- aidx = out.start_object( oid )
- if aidx
- out.simple( "*#{ aidx }" )
- else
- e.call( out )
- end
- if old_opt.is_a? Hash
- out.options = old_opt
- end
- out.end_object
+ out =
+ if opts.is_a? YAML::Emitter
+ opts
+ else
+ emitter.reset( opts )
+ end
+ out.emit( oid, &e )
end
end
@@ -383,7 +391,7 @@ end
require 'yaml/rubytypes'
require 'yaml/types'
-module Kernel
+module Kernel # :nodoc:
#
# 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
@@ -400,12 +408,25 @@ module Kernel
# ryan:: Either way, I certainly will have a pony parade.
#
- def y( o, *x )
- x.unshift o
- puts( if x.length == 1
- YAML::dump( *x )
+ # Prints any supplied _objects_ out in YAML. Intended as
+ # a variation on +Kernel::p+.
+ #
+ # S = Struct.new(:name, :state)
+ # s = S['dave', 'TX']
+ # y s
+ #
+ # _produces:_
+ #
+ # --- !ruby/struct:S
+ # name: dave
+ # state: TX
+ #
+ def y( object, *objects )
+ objects.unshift object
+ puts( if objects.length == 1
+ YAML::dump( *objects )
else
- YAML::dump_stream( *x )
+ YAML::dump_stream( *objects )
end )
end
private :y