source: extensions/gsdl-video/trunk/installed/cmdline/lib/ruby/1.8/yaml/types.rb@ 18425

Last change on this file since 18425 was 18425, checked in by davidb, 15 years ago

Video extension to Greenstone

File size: 5.0 KB
Line 
1# -*- mode: ruby; ruby-indent-level: 4 -*- vim: sw=4
2#
3# Classes required by the full core typeset
4#
5
6module YAML
7
8 #
9 # Default private type
10 #
11 class PrivateType
12 def self.tag_subclasses?; false; end
13 attr_accessor :type_id, :value
14 verbose, $VERBOSE = $VERBOSE, nil
15 def initialize( type, val )
16 @type_id = type; @value = val
17 @value.taguri = "x-private:#{ @type_id }"
18 end
19 def to_yaml( opts = {} )
20 @value.to_yaml( opts )
21 end
22 ensure
23 $VERBOSE = verbose
24 end
25
26 #
27 # Default domain type
28 #
29 class DomainType
30 def self.tag_subclasses?; false; end
31 attr_accessor :domain, :type_id, :value
32 verbose, $VERBOSE = $VERBOSE, nil
33 def initialize( domain, type, val )
34 @domain = domain; @type_id = type; @value = val
35 @value.taguri = "tag:#{ @domain }:#{ @type_id }"
36 end
37 def to_yaml( opts = {} )
38 @value.to_yaml( opts )
39 end
40 ensure
41 $VERBOSE = verbose
42 end
43
44 #
45 # Unresolved objects
46 #
47 class Object
48 def self.tag_subclasses?; false; end
49 def to_yaml( opts = {} )
50 YAML::quick_emit( object_id, opts ) do |out|
51 out.map( "tag:ruby.yaml.org,2002:object:#{ @class }", to_yaml_style ) do |map|
52 @ivars.each do |k,v|
53 map.add( k, v )
54 end
55 end
56 end
57 end
58 end
59
60 #
61 # YAML Hash class to support comments and defaults
62 #
63 class SpecialHash < ::Hash
64 attr_accessor :default
65 def inspect
66 self.default.to_s
67 end
68 def to_s
69 self.default.to_s
70 end
71 def update( h )
72 if YAML::SpecialHash === h
73 @default = h.default if h.default
74 end
75 super( h )
76 end
77 def to_yaml( opts = {} )
78 opts[:DefaultKey] = self.default
79 super( opts )
80 end
81 end
82
83 #
84 # Builtin collection: !omap
85 #
86 class Omap < ::Array
87 yaml_as "tag:yaml.org,2002:omap"
88 def yaml_initialize( tag, val )
89 if Array === val
90 val.each do |v|
91 if Hash === v
92 concat( v.to_a ) # Convert the map to a sequence
93 else
94 raise YAML::Error, "Invalid !omap entry: " + val.inspect
95 end
96 end
97 else
98 raise YAML::Error, "Invalid !omap: " + val.inspect
99 end
100 self
101 end
102 def self.[]( *vals )
103 o = Omap.new
104 0.step( vals.length - 1, 2 ) do |i|
105 o[vals[i]] = vals[i+1]
106 end
107 o
108 end
109 def []( k )
110 self.assoc( k ).to_a[1]
111 end
112 def []=( k, *rest )
113 val, set = rest.reverse
114 if ( tmp = self.assoc( k ) ) and not set
115 tmp[1] = val
116 else
117 self << [ k, val ]
118 end
119 val
120 end
121 def has_key?( k )
122 self.assoc( k ) ? true : false
123 end
124 def is_complex_yaml?
125 true
126 end
127 def to_yaml( opts = {} )
128 YAML::quick_emit( self.object_id, opts ) do |out|
129 out.seq( taguri, to_yaml_style ) do |seq|
130 self.each do |v|
131 seq.add( Hash[ *v ] )
132 end
133 end
134 end
135 end
136 end
137
138 #
139 # Builtin collection: !pairs
140 #
141 class Pairs < ::Array
142 yaml_as "tag:yaml.org,2002:pairs"
143 def yaml_initialize( tag, val )
144 if Array === val
145 val.each do |v|
146 if Hash === v
147 concat( v.to_a ) # Convert the map to a sequence
148 else
149 raise YAML::Error, "Invalid !pairs entry: " + val.inspect
150 end
151 end
152 else
153 raise YAML::Error, "Invalid !pairs: " + val.inspect
154 end
155 self
156 end
157 def self.[]( *vals )
158 p = Pairs.new
159 0.step( vals.length - 1, 2 ) { |i|
160 p[vals[i]] = vals[i+1]
161 }
162 p
163 end
164 def []( k )
165 self.assoc( k ).to_a
166 end
167 def []=( k, val )
168 self << [ k, val ]
169 val
170 end
171 def has_key?( k )
172 self.assoc( k ) ? true : false
173 end
174 def is_complex_yaml?
175 true
176 end
177 def to_yaml( opts = {} )
178 YAML::quick_emit( self.object_id, opts ) do |out|
179 out.seq( taguri, to_yaml_style ) do |seq|
180 self.each do |v|
181 seq.add( Hash[ *v ] )
182 end
183 end
184 end
185 end
186 end
187
188 #
189 # Builtin collection: !set
190 #
191 class Set < ::Hash
192 yaml_as "tag:yaml.org,2002:set"
193 end
194end
Note: See TracBrowser for help on using the repository browser.