diff options
author | why <why@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2005-09-13 03:58:33 +0000 |
---|---|---|
committer | why <why@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2005-09-13 03:58:33 +0000 |
commit | 176a7ab72a9bb763a97ad2dadc4ce3bf357b5976 (patch) | |
tree | 43d2fe12e0eb86c4a688a22cd043f898c7683872 /lib/yaml/types.rb | |
parent | 19dbb4c3c17e1ec458b02587fce49950dfca0648 (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/branches/ruby_1_8@9141 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'lib/yaml/types.rb')
-rw-r--r-- | lib/yaml/types.rb | 151 |
1 files changed, 71 insertions, 80 deletions
diff --git a/lib/yaml/types.rb b/lib/yaml/types.rb index f7772cb3a0..c890f22bfa 100644 --- a/lib/yaml/types.rb +++ b/lib/yaml/types.rb @@ -8,15 +8,14 @@ module YAML # Default private type # class PrivateType + def self.tag_subclasses?; false; end attr_accessor :type_id, :value def initialize( type, val ) @type_id = type; @value = val + @value.taguri = "x-private:#{ @type_id }" end def to_yaml( opts = {} ) - YAML::quick_emit( self.object_id, opts ) { |out| - out << " !!#{@type_id}" - value.to_yaml( :Emitter => out ) - } + @value.to_yaml( opts ) end end @@ -24,29 +23,37 @@ module YAML # Default domain type # class DomainType + def self.tag_subclasses?; false; end attr_accessor :domain, :type_id, :value def initialize( domain, type, val ) @domain = domain; @type_id = type; @value = val + @value.taguri = "tag:#{ @domain }:#{ @type_id }" end - def to_yaml_type - dom = @domain.dup - if dom =~ /\.yaml\.org,2002$/ - dom = $` - end - "#{dom}/#{@type_id}" - end def to_yaml( opts = {} ) - YAML::quick_emit( self.object_id, opts ) { |out| - out << " !#{to_yaml_type} " - value.to_yaml( :Emitter => out ) - } + @value.to_yaml( opts ) + end + end + + # + # Unresolved objects + # + class Object + def self.tag_subclasses?; false; end + def to_yaml( opts = {} ) + YAML::quick_emit( object_id, opts ) do |out| + out.map( "tag:ruby.yaml.org,2002:object:#{ @class }", to_yaml_style ) do |map| + @ivars.each do |k,v| + map.add( k, v ) + end + end + end end end # # YAML Hash class to support comments and defaults # - class SpecialHash < Object::Hash + class SpecialHash < ::Hash attr_accessor :default def inspect self.default.to_s @@ -69,12 +76,27 @@ module YAML # # Builtin collection: !omap # - class Omap < Array + class Omap < ::Array + yaml_as "tag:yaml.org,2002:omap" + def yaml_initialize( tag, val ) + if Array === val + val.each do |v| + if Hash === v + concat( v.to_a ) # Convert the map to a sequence + else + raise YAML::Error, "Invalid !omap entry: " + val.inspect + end + end + else + raise YAML::Error, "Invalid !omap: " + val.inspect + end + self + end def self.[]( *vals ) o = Omap.new - 0.step( vals.length - 1, 2 ) { |i| + 0.step( vals.length - 1, 2 ) do |i| o[vals[i]] = vals[i+1] - } + end o end def []( k ) @@ -96,36 +118,35 @@ module YAML true end def to_yaml( opts = {} ) - YAML::quick_emit( self.object_id, opts ) { |out| - out.seq( "!omap" ) { |seq| - self.each { |v| + YAML::quick_emit( self.object_id, opts ) do |out| + out.seq( taguri, to_yaml_style ) do |seq| + self.each do |v| seq.add( Hash[ *v ] ) - } - } - } - end - end - - YAML.add_builtin_type( "omap" ) { |type, val| - if Array === val - p = Omap.new - val.each { |v| - if Hash === v - p.concat( v.to_a ) # Convert the map to a sequence - else - raise YAML::Error, "Invalid !omap entry: " + val.inspect + end end - } - else - raise YAML::Error, "Invalid !omap: " + val.inspect + end end - p - } + end # # Builtin collection: !pairs # - class Pairs < Array + class Pairs < ::Array + yaml_as "tag:yaml.org,2002:pairs" + def yaml_initialize( tag, val ) + if Array === val + val.each do |v| + if Hash === v + concat( v.to_a ) # Convert the map to a sequence + else + raise YAML::Error, "Invalid !pairs entry: " + val.inspect + end + end + else + raise YAML::Error, "Invalid !pairs: " + val.inspect + end + self + end def self.[]( *vals ) p = Pairs.new 0.step( vals.length - 1, 2 ) { |i| @@ -147,50 +168,20 @@ module YAML true end def to_yaml( opts = {} ) - YAML::quick_emit( self.object_id, opts ) { |out| - out.seq( "!pairs" ) { |seq| - self.each { |v| + YAML::quick_emit( self.object_id, opts ) do |out| + out.seq( taguri, to_yaml_style ) do |seq| + self.each do |v| seq.add( Hash[ *v ] ) - } - } - } - end - end - - YAML.add_builtin_type( "pairs" ) { |type, val| - if Array === val - p = Pairs.new - val.each { |v| - if Hash === v - p.concat( v.to_a ) # Convert the map to a sequence - else - raise YAML::Error, "Invalid !pairs entry: " + val.inspect + end end - } - else - raise YAML::Error, "Invalid !pairs: " + val.inspect + end end - p - } + end # # Builtin collection: !set # - class Set < Hash - def to_yaml_type - "!set" - end + class Set < ::Hash + yaml_as "tag:yaml.org,2002:set" end - - YAML.add_builtin_type( 'set' ) { |type, val| - if Array === val - val = Set[ *val ] - elsif Hash === val - Set[ val ] - else - raise YAML::Error, "Invalid map explicitly tagged !map: " + val.inspect - end - val - } - end |