2005-09-27 18:57:53 -04:00
|
|
|
# -*- mode: ruby; ruby-indent-level: 4 -*- vim: sw=4
|
2003-05-09 17:25:50 -04:00
|
|
|
#
|
|
|
|
# Classes required by the full core typeset
|
|
|
|
#
|
2003-05-13 18:29:52 -04:00
|
|
|
|
2003-05-09 17:25:50 -04:00
|
|
|
module YAML
|
|
|
|
|
2005-09-27 18:57:53 -04:00
|
|
|
#
|
|
|
|
# Default private type
|
|
|
|
#
|
|
|
|
class PrivateType
|
2005-09-12 23:58:33 -04:00
|
|
|
def self.tag_subclasses?; false; end
|
2005-09-27 18:57:53 -04:00
|
|
|
attr_accessor :type_id, :value
|
|
|
|
verbose, $VERBOSE = $VERBOSE, nil
|
|
|
|
def initialize( type, val )
|
|
|
|
@type_id = type; @value = val
|
2005-09-12 23:58:33 -04:00
|
|
|
@value.taguri = "x-private:#{ @type_id }"
|
2005-09-27 18:57:53 -04:00
|
|
|
end
|
|
|
|
def to_yaml( opts = {} )
|
2005-09-12 23:58:33 -04:00
|
|
|
@value.to_yaml( opts )
|
2005-09-27 18:57:53 -04:00
|
|
|
end
|
|
|
|
ensure
|
|
|
|
$VERBOSE = verbose
|
|
|
|
end
|
2003-05-09 17:25:50 -04:00
|
|
|
|
|
|
|
#
|
|
|
|
# Default domain type
|
|
|
|
#
|
|
|
|
class DomainType
|
2005-09-12 23:58:33 -04:00
|
|
|
def self.tag_subclasses?; false; end
|
2005-09-27 18:57:53 -04:00
|
|
|
attr_accessor :domain, :type_id, :value
|
|
|
|
verbose, $VERBOSE = $VERBOSE, nil
|
|
|
|
def initialize( domain, type, val )
|
|
|
|
@domain = domain; @type_id = type; @value = val
|
2005-09-12 23:58:33 -04:00
|
|
|
@value.taguri = "tag:#{ @domain }:#{ @type_id }"
|
2005-09-27 18:57:53 -04:00
|
|
|
end
|
|
|
|
def to_yaml( opts = {} )
|
2005-09-12 23:58:33 -04:00
|
|
|
@value.to_yaml( opts )
|
2005-09-27 18:57:53 -04:00
|
|
|
end
|
|
|
|
ensure
|
|
|
|
$VERBOSE = verbose
|
|
|
|
end
|
2005-09-12 23:58:33 -04:00
|
|
|
|
|
|
|
#
|
|
|
|
# Unresolved objects
|
|
|
|
#
|
|
|
|
class Object
|
|
|
|
def self.tag_subclasses?; false; end
|
2005-09-27 18:57:53 -04:00
|
|
|
def to_yaml( opts = {} )
|
2005-09-12 23:58:33 -04:00
|
|
|
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
|
2005-09-27 18:57:53 -04:00
|
|
|
end
|
|
|
|
end
|
2003-05-09 17:25:50 -04:00
|
|
|
|
2005-09-27 18:57:53 -04:00
|
|
|
#
|
|
|
|
# YAML Hash class to support comments and defaults
|
|
|
|
#
|
|
|
|
class SpecialHash < ::Hash
|
|
|
|
attr_accessor :default
|
2003-05-09 17:25:50 -04:00
|
|
|
def inspect
|
|
|
|
self.default.to_s
|
|
|
|
end
|
2005-09-27 18:57:53 -04:00
|
|
|
def to_s
|
|
|
|
self.default.to_s
|
|
|
|
end
|
|
|
|
def update( h )
|
|
|
|
if YAML::SpecialHash === h
|
|
|
|
@default = h.default if h.default
|
|
|
|
end
|
|
|
|
super( h )
|
|
|
|
end
|
2003-05-09 17:25:50 -04:00
|
|
|
def to_yaml( opts = {} )
|
|
|
|
opts[:DefaultKey] = self.default
|
|
|
|
super( opts )
|
|
|
|
end
|
2005-09-27 18:57:53 -04:00
|
|
|
end
|
2003-05-09 17:25:50 -04:00
|
|
|
|
|
|
|
#
|
|
|
|
# Builtin collection: !omap
|
|
|
|
#
|
2005-09-12 23:58:33 -04:00
|
|
|
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
|
2003-05-09 17:25:50 -04:00
|
|
|
def self.[]( *vals )
|
|
|
|
o = Omap.new
|
2005-09-12 23:58:33 -04:00
|
|
|
0.step( vals.length - 1, 2 ) do |i|
|
2003-05-09 17:25:50 -04:00
|
|
|
o[vals[i]] = vals[i+1]
|
2005-09-12 23:58:33 -04:00
|
|
|
end
|
2003-05-09 17:25:50 -04:00
|
|
|
o
|
|
|
|
end
|
|
|
|
def []( k )
|
|
|
|
self.assoc( k ).to_a[1]
|
|
|
|
end
|
|
|
|
def []=( k, *rest )
|
|
|
|
val, set = rest.reverse
|
|
|
|
if ( tmp = self.assoc( k ) ) and not set
|
|
|
|
tmp[1] = val
|
|
|
|
else
|
|
|
|
self << [ k, val ]
|
|
|
|
end
|
|
|
|
val
|
|
|
|
end
|
|
|
|
def has_key?( k )
|
|
|
|
self.assoc( k ) ? true : false
|
|
|
|
end
|
|
|
|
def is_complex_yaml?
|
|
|
|
true
|
|
|
|
end
|
|
|
|
def to_yaml( opts = {} )
|
2005-09-12 23:58:33 -04:00
|
|
|
YAML::quick_emit( self.object_id, opts ) do |out|
|
|
|
|
out.seq( taguri, to_yaml_style ) do |seq|
|
|
|
|
self.each do |v|
|
2003-05-09 17:25:50 -04:00
|
|
|
seq.add( Hash[ *v ] )
|
2005-09-12 23:58:33 -04:00
|
|
|
end
|
2003-05-09 17:25:50 -04:00
|
|
|
end
|
2005-09-12 23:58:33 -04:00
|
|
|
end
|
2003-05-09 17:25:50 -04:00
|
|
|
end
|
2005-09-12 23:58:33 -04:00
|
|
|
end
|
2003-05-09 17:25:50 -04:00
|
|
|
|
|
|
|
#
|
|
|
|
# Builtin collection: !pairs
|
|
|
|
#
|
2005-09-12 23:58:33 -04:00
|
|
|
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
|
2003-05-09 17:25:50 -04:00
|
|
|
def self.[]( *vals )
|
|
|
|
p = Pairs.new
|
|
|
|
0.step( vals.length - 1, 2 ) { |i|
|
|
|
|
p[vals[i]] = vals[i+1]
|
|
|
|
}
|
|
|
|
p
|
|
|
|
end
|
|
|
|
def []( k )
|
|
|
|
self.assoc( k ).to_a
|
|
|
|
end
|
|
|
|
def []=( k, val )
|
|
|
|
self << [ k, val ]
|
|
|
|
val
|
|
|
|
end
|
|
|
|
def has_key?( k )
|
|
|
|
self.assoc( k ) ? true : false
|
|
|
|
end
|
|
|
|
def is_complex_yaml?
|
|
|
|
true
|
|
|
|
end
|
|
|
|
def to_yaml( opts = {} )
|
2005-09-12 23:58:33 -04:00
|
|
|
YAML::quick_emit( self.object_id, opts ) do |out|
|
|
|
|
out.seq( taguri, to_yaml_style ) do |seq|
|
|
|
|
self.each do |v|
|
2003-05-09 17:25:50 -04:00
|
|
|
seq.add( Hash[ *v ] )
|
2005-09-12 23:58:33 -04:00
|
|
|
end
|
2003-05-09 17:25:50 -04:00
|
|
|
end
|
2005-09-12 23:58:33 -04:00
|
|
|
end
|
2003-05-09 17:25:50 -04:00
|
|
|
end
|
2005-09-12 23:58:33 -04:00
|
|
|
end
|
2003-05-09 17:25:50 -04:00
|
|
|
|
|
|
|
#
|
|
|
|
# Builtin collection: !set
|
|
|
|
#
|
2005-09-12 23:58:33 -04:00
|
|
|
class Set < ::Hash
|
|
|
|
yaml_as "tag:yaml.org,2002:set"
|
2003-05-09 17:25:50 -04:00
|
|
|
end
|
|
|
|
end
|