RbYAML 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/rbyaml.rb +14 -256
- data/lib/rbyaml.rb.~1.2.~ +383 -0
- data/lib/rbyaml/composer.rb +9 -11
- data/lib/rbyaml/{composer.rb.~1.2.~ → composer.rb.~1.3.~} +28 -25
- data/lib/rbyaml/constants.rb +95 -0
- data/lib/rbyaml/constructor.rb +180 -89
- data/lib/rbyaml/{constructor.rb.~1.2.~ → constructor.rb.~1.9.~} +137 -95
- data/lib/rbyaml/dumper.rb +12 -9
- data/lib/rbyaml/dumper.rb.~1.3.~ +36 -0
- data/lib/rbyaml/emitter.rb +14 -28
- data/lib/rbyaml/{emitter.rb.~1.2.~ → emitter.rb.~1.6.~} +22 -33
- data/lib/rbyaml/error.rb +4 -57
- data/lib/rbyaml/error.rb.~1.2.~ +75 -0
- data/lib/rbyaml/events.rb +8 -14
- data/lib/rbyaml/{events.rb.~1.2.~ → events.rb.~1.4.~} +29 -6
- data/lib/rbyaml/nodes.rb +5 -5
- data/lib/rbyaml/{nodes.rb.~1.2.~ → nodes.rb.~1.3.~} +13 -9
- data/lib/rbyaml/parser.rb +70 -108
- data/lib/rbyaml/parser.rb.~1.4.~ +632 -0
- data/lib/rbyaml/representer.rb +19 -157
- data/lib/rbyaml/representer.rb.old +317 -0
- data/lib/rbyaml/{representer.rb.~1.2.~ → representer.rb.~1.5.~} +60 -26
- data/lib/rbyaml/resolver.rb +6 -6
- data/lib/rbyaml/{resolver.rb.~1.1.~ → resolver.rb.~1.6.~} +20 -20
- data/lib/rbyaml/rubytypes.rb +391 -0
- data/lib/rbyaml/scanner.rb +123 -225
- data/lib/rbyaml/{scanner.rb.~1.2.~ → scanner.rb.~1.5.~} +466 -378
- data/lib/rbyaml/serializer.rb +9 -9
- data/lib/rbyaml/{serializer.rb.~1.2.~ → serializer.rb.~1.4.~} +19 -17
- data/lib/rbyaml/stream.rb +48 -0
- data/lib/rbyaml/tag.rb +72 -0
- data/lib/rbyaml/tokens.rb +22 -16
- data/lib/rbyaml/{tokens.rb.~1.2.~ → tokens.rb.~1.3.~} +44 -4
- data/lib/rbyaml/types.rb +146 -0
- data/lib/rbyaml/util.rb.~1.3.~ +38 -0
- data/lib/rbyaml/yaml.rb +22 -32
- data/lib/rbyaml/{yaml.rb.~1.2.~ → yaml.rb.~1.5.~} +17 -17
- data/test/load_one.rb +6 -0
- data/test/load_one_yaml.rb +6 -0
- data/test/output_events.rb +9 -0
- data/test/test_add_ctor.rb +51 -0
- data/test/test_add_ctor.rb.~1.1.~ +30 -0
- data/test/test_bm.rb +2 -2
- data/test/test_bm.rb.~1.1.~ +28 -0
- data/test/test_gems.rb +10 -0
- data/test/test_one.rb.~1.1.~ +5 -0
- data/test/test_one_syck.rb +5 -0
- data/test/test_rbyaml.rb +63 -32
- data/test/test_rbyaml.rb.~1.6.~ +59 -0
- data/test/{test_rbyaml.rb.~1.2.~ → test_rbyaml_old.rb} +13 -4
- data/test/test_time_events.rb +24 -0
- data/test/test_time_nodes.rb +24 -0
- data/test/test_time_tokens.rb +24 -0
- data/test/yaml/gems_new.yml +147456 -0
- data/test/yaml/test1.rb +8 -0
- data/test/yaml/test10.rb +14 -0
- data/test/yaml/test11.rb +13 -0
- data/test/yaml/test12.rb +9 -0
- data/test/yaml/test13.rb +9 -0
- data/test/yaml/test14.rb +13 -0
- data/test/yaml/test15.rb +12 -0
- data/test/yaml/test16.rb +11 -0
- data/test/yaml/test16.rb.~1.1.~ +11 -0
- data/test/yaml/test17.rb +10 -0
- data/test/yaml/test18.rb +13 -0
- data/test/yaml/test19.rb +9 -0
- data/test/yaml/test19.yml +1 -1
- data/test/yaml/test2.rb +8 -0
- data/test/yaml/test20.rb +11 -0
- data/test/yaml/test20.rb.~1.1.~ +9 -0
- data/test/yaml/test20.yml +1 -1
- data/test/yaml/test3.rb +13 -0
- data/test/yaml/test4.rb +13 -0
- data/test/yaml/test5.rb +8 -0
- data/test/yaml/test6.rb +10 -0
- data/test/yaml/test7.rb +15 -0
- data/test/yaml/test8.rb +15 -0
- data/test/yaml/test9.rb +13 -0
- metadata +61 -16
- data/lib/rbyaml/dumper.rb.~1.2.~ +0 -43
- data/lib/rbyaml/parser.rb.~1.2.~ +0 -494
data/lib/rbyaml.rb
CHANGED
@@ -1,9 +1,11 @@
|
|
1
1
|
|
2
2
|
require 'rbyaml/yaml'
|
3
|
+
require 'rbyaml/stream'
|
4
|
+
require 'rbyaml/dumper'
|
3
5
|
|
4
6
|
module RbYAML
|
5
|
-
def self.dump(obj, io = nil)
|
6
|
-
_dump(obj,io)
|
7
|
+
def self.dump(obj, io = nil, opts={})
|
8
|
+
_dump(obj,io,Dumper,opts)
|
7
9
|
end
|
8
10
|
|
9
11
|
def self.load( io )
|
@@ -63,6 +65,11 @@ module RbYAML
|
|
63
65
|
d.emit
|
64
66
|
end
|
65
67
|
|
68
|
+
|
69
|
+
def self.add_builtin_ctor(type_tag, &transfer_proc)
|
70
|
+
BaseConstructor::add_constructor("tag:yaml.org,2002:#{ type_tag }",transfer_proc)
|
71
|
+
end
|
72
|
+
|
66
73
|
# this operation does not make sense in RbYAML (right now)
|
67
74
|
def self.add_domain_type( domain, type_re, &transfer_proc )
|
68
75
|
# @@loader.add_domain_type( domain, type_re, &transfer_proc )
|
@@ -119,260 +126,11 @@ module RbYAML
|
|
119
126
|
def self.quick_emit( oid, opts = {}, &e )
|
120
127
|
end
|
121
128
|
|
122
|
-
|
123
|
-
|
124
|
-
@@tagged_classes = {}
|
125
|
-
|
126
|
-
#
|
127
|
-
# Associates a taguri _tag_ with a Ruby class _cls_. The taguri is used to give types
|
128
|
-
# to classes when loading YAML. Taguris are of the form:
|
129
|
-
#
|
130
|
-
# tag:authorityName,date:specific
|
131
|
-
#
|
132
|
-
# The +authorityName+ is a domain name or email address. The +date+ is the date the type
|
133
|
-
# was issued in YYYY or YYYY-MM or YYYY-MM-DD format. The +specific+ is a name for
|
134
|
-
# the type being added.
|
135
|
-
#
|
136
|
-
# For example, built-in YAML types have 'yaml.org' as the +authorityName+ and '2002' as the
|
137
|
-
# +date+. The +specific+ is simply the name of the type:
|
138
|
-
#
|
139
|
-
# tag:yaml.org,2002:int
|
140
|
-
# tag:yaml.org,2002:float
|
141
|
-
# tag:yaml.org,2002:timestamp
|
142
|
-
#
|
143
|
-
# The domain must be owned by you on the +date+ declared. If you don't own any domains on the
|
144
|
-
# date you declare the type, you can simply use an e-mail address.
|
145
|
-
#
|
146
|
-
# tag:why@ruby-lang.org,2004:notes/personal
|
147
|
-
#
|
148
|
-
def self.tag_class( tag, cls )
|
149
|
-
if @@tagged_classes.has_key? tag
|
150
|
-
warn "class #{ @@tagged_classes[tag] } held ownership of the #{ tag } tag"
|
151
|
-
end
|
152
|
-
@@tagged_classes[tag] = cls
|
153
|
-
end
|
154
|
-
|
155
|
-
# Returns the complete dictionary of taguris, paired with classes. The key for
|
156
|
-
# the dictionary is the full taguri. The value for each key is the class constant
|
157
|
-
# associated to that taguri.
|
158
|
-
#
|
159
|
-
# YAML.tagged_classes["tag:yaml.org,2002:int"] => Integer
|
160
|
-
#
|
161
|
-
def self.tagged_classes
|
162
|
-
@@tagged_classes
|
163
|
-
end
|
164
|
-
|
165
|
-
#
|
166
|
-
# RbYAML::Stream -- for emitting many documents
|
167
|
-
#
|
168
|
-
class Stream
|
169
|
-
include Enumerable
|
170
|
-
|
171
|
-
attr_accessor :documents, :options
|
172
|
-
def initialize(opts = {})
|
173
|
-
@options = opts
|
174
|
-
@documents = []
|
175
|
-
end
|
176
|
-
|
177
|
-
def [](i)
|
178
|
-
@documents[ i ]
|
179
|
-
end
|
180
|
-
|
181
|
-
def add(doc)
|
182
|
-
@documents << doc
|
183
|
-
end
|
184
|
-
|
185
|
-
def edit(doc_num,doc)
|
186
|
-
@documents[ doc_num ] = doc
|
187
|
-
end
|
188
|
-
|
189
|
-
def each(&block)
|
190
|
-
@documents.each(&block)
|
191
|
-
end
|
192
|
-
|
193
|
-
def emit
|
194
|
-
# TODO: implement
|
195
|
-
|
196
|
-
opts = @options.dup
|
197
|
-
opts[:UseHeader] = true if @documents.length > 1
|
198
|
-
ct = 0
|
199
|
-
out = Emitter.new( opts )
|
200
|
-
@documents.each { |v|
|
201
|
-
if ct > 0
|
202
|
-
out << "\n--- "
|
203
|
-
end
|
204
|
-
v.to_yaml( :Emitter => out )
|
205
|
-
ct += 1
|
206
|
-
}
|
207
|
-
out.end_object
|
208
|
-
end
|
129
|
+
def self.quick_emit_node( oid, rep, &e )
|
130
|
+
e.call(rep)
|
209
131
|
end
|
210
132
|
end
|
211
133
|
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
class_eval <<-"end;", __FILE__, __LINE__+1
|
216
|
-
attr_writer :taguri
|
217
|
-
def taguri
|
218
|
-
return @taguri if defined?(@taguri) and @taguri
|
219
|
-
tag = #{ tag.dump }
|
220
|
-
if self.class.yaml_tag_subclasses? and self.class != RbYAML::tagged_classes[tag]
|
221
|
-
tag = "\#{ tag }:\#{ self.class.yaml_tag_class_name }"
|
222
|
-
end
|
223
|
-
tag
|
224
|
-
end
|
225
|
-
def self.yaml_tag_subclasses?; #{ sc ? 'true' : 'false' }; end
|
226
|
-
end;
|
227
|
-
RbYAML::tag_class tag, self
|
228
|
-
end
|
229
|
-
# Transforms the subclass name into a name suitable for display
|
230
|
-
# in a subclassed tag.
|
231
|
-
def yaml_tag_class_name
|
232
|
-
self.name
|
233
|
-
end
|
234
|
-
# Transforms the subclass name found in the tag into a Ruby
|
235
|
-
# constant name.
|
236
|
-
def yaml_tag_read_class( name )
|
237
|
-
name
|
238
|
-
end
|
239
|
-
end
|
240
|
-
|
241
|
-
require 'date'
|
242
|
-
|
243
|
-
class Class
|
244
|
-
def to_yaml( opts = {} )
|
245
|
-
raise RbYAML::TypeError, "can't dump anonymous class %s" % self.class
|
246
|
-
end
|
247
|
-
end
|
248
|
-
|
249
|
-
class Object
|
250
|
-
yaml_as "tag:ruby.yaml.org,2002:object"
|
251
|
-
def is_complex_yaml?; true; end
|
252
|
-
def to_yaml_style; end
|
253
|
-
def to_yaml_properties; instance_variables.sort; end
|
254
|
-
def to_yaml( opts = {} )
|
255
|
-
RbYAML::_dump_ruby_object(self)
|
256
|
-
end
|
257
|
-
end
|
258
|
-
|
259
|
-
class Hash
|
260
|
-
yaml_as "tag:ruby.yaml.org,2002:hash"
|
261
|
-
yaml_as "tag:yaml.org,2002:map"
|
262
|
-
def is_complex_yaml?; true; end
|
263
|
-
def yaml_initialize( tag, val )
|
264
|
-
if Array === val
|
265
|
-
update Hash.[]( *val ) # Convert the map to a sequence
|
266
|
-
elsif Hash === val
|
267
|
-
update val
|
268
|
-
else
|
269
|
-
raise RbYAML::TypeError, "Invalid map explicitly tagged #{ tag }: " + val.inspect
|
270
|
-
end
|
271
|
-
end
|
272
|
-
end
|
273
|
-
|
274
|
-
class Array
|
275
|
-
yaml_as "tag:ruby.yaml.org,2002:array"
|
276
|
-
yaml_as "tag:yaml.org,2002:seq"
|
277
|
-
def is_complex_yaml?; true; end
|
278
|
-
def yaml_initialize( tag, val ); concat( val.to_a ); end
|
279
|
-
end
|
280
|
-
|
281
|
-
class Exception
|
282
|
-
yaml_as "tag:ruby.yaml.org,2002:exception"
|
283
|
-
def Exception.yaml_new( klass, tag, val )
|
284
|
-
o = RbYAML.object_maker( klass, { 'mesg' => val.delete( 'message' ) } )
|
285
|
-
val.each_pair do |k,v|
|
286
|
-
o.instance_variable_set("@#{k}", v)
|
287
|
-
end
|
288
|
-
o
|
289
|
-
end
|
290
|
-
end
|
291
|
-
|
292
|
-
class String
|
293
|
-
yaml_as "tag:ruby.yaml.org,2002:string"
|
294
|
-
yaml_as "tag:yaml.org,2002:binary"
|
295
|
-
yaml_as "tag:yaml.org,2002:str"
|
296
|
-
def is_complex_yaml?
|
297
|
-
to_yaml_style or not to_yaml_properties.empty? or self =~ /\n.+/
|
298
|
-
end
|
299
|
-
def is_binary_data?
|
300
|
-
( self.count( "^ -~", "^\r\n" ) / self.size > 0.3 || self.count( "\x00" ) > 0 ) unless empty?
|
301
|
-
end
|
302
|
-
def String.yaml_new( klass, tag, val )
|
303
|
-
val = val.unpack("m")[0] if tag == "tag:yaml.org,2002:binary"
|
304
|
-
val = { 'str' => val } if String === val
|
305
|
-
if Hash === val
|
306
|
-
s = klass.allocate
|
307
|
-
# Thank you, NaHi
|
308
|
-
String.instance_method(:initialize).
|
309
|
-
bind(s).
|
310
|
-
call( val.delete( 'str' ) )
|
311
|
-
val.each { |k,v| s.instance_variable_set( k, v ) }
|
312
|
-
s
|
313
|
-
else
|
314
|
-
raise RbYAML::TypeError, "Invalid String: " + val.inspect
|
315
|
-
end
|
316
|
-
end
|
317
|
-
end
|
318
|
-
|
319
|
-
class Symbol
|
320
|
-
yaml_as "tag:ruby.yaml.org,2002:symbol"
|
321
|
-
yaml_as "tag:ruby.yaml.org,2002:sym"
|
322
|
-
def is_complex_yaml?; false; end
|
323
|
-
def Symbol.yaml_new( klass, tag, val )
|
324
|
-
if String === val
|
325
|
-
val.intern
|
326
|
-
else
|
327
|
-
raise RbYAML::TypeError, "Invalid Symbol: " + val.inspect
|
328
|
-
end
|
329
|
-
end
|
330
|
-
end
|
331
|
-
|
332
|
-
class Time
|
333
|
-
yaml_as "tag:ruby.yaml.org,2002:time"
|
334
|
-
yaml_as "tag:yaml.org,2002:timestamp"
|
335
|
-
def is_complex_yaml?; false; end
|
336
|
-
def Time.yaml_new( klass, tag, val )
|
337
|
-
if Hash === val
|
338
|
-
t = val.delete( 'at' )
|
339
|
-
val.each { |k,v| t.instance_variable_set( k, v ) }
|
340
|
-
t
|
341
|
-
else
|
342
|
-
raise RbYAML::TypeError, "Invalid Time: " + val.inspect
|
343
|
-
end
|
344
|
-
end
|
345
|
-
end
|
346
|
-
|
347
|
-
class Date
|
348
|
-
yaml_as "tag:yaml.org,2002:timestamp#ymd"
|
349
|
-
def is_complex_yaml?; false; end
|
350
|
-
end
|
351
|
-
|
352
|
-
class Numeric
|
353
|
-
def is_complex_yaml?; false; end
|
354
|
-
end
|
355
|
-
|
356
|
-
class Fixnum
|
357
|
-
yaml_as "tag:yaml.org,2002:int"
|
358
|
-
end
|
359
|
-
|
360
|
-
class Float
|
361
|
-
yaml_as "tag:yaml.org,2002:float"
|
362
|
-
end
|
363
|
-
|
364
|
-
class TrueClass
|
365
|
-
yaml_as "tag:yaml.org,2002:bool#yes"
|
366
|
-
def is_complex_yaml?; false; end
|
367
|
-
end
|
368
|
-
|
369
|
-
class FalseClass
|
370
|
-
yaml_as "tag:yaml.org,2002:bool#no"
|
371
|
-
def is_complex_yaml?; false; end
|
372
|
-
end
|
373
|
-
|
374
|
-
class NilClass
|
375
|
-
yaml_as "tag:yaml.org,2002:null"
|
376
|
-
def is_complex_yaml?; false; end
|
377
|
-
end
|
378
|
-
end
|
134
|
+
require 'rbyaml/tag'
|
135
|
+
require 'rbyaml/types'
|
136
|
+
require 'rbyaml/rubytypes'
|
@@ -0,0 +1,383 @@
|
|
1
|
+
|
2
|
+
require 'rbyaml/yaml'
|
3
|
+
|
4
|
+
module RbYAML
|
5
|
+
def self.dump(obj, io = nil)
|
6
|
+
_dump(obj,io)
|
7
|
+
end
|
8
|
+
|
9
|
+
def self.load( io )
|
10
|
+
_load(io)
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.load_file( filepath )
|
14
|
+
File.open( filepath ) do |f|
|
15
|
+
load( f )
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
# this operation does not make sense in RbYAML (right now)
|
20
|
+
def self.parse( io )
|
21
|
+
# yp = @@parser.new( :Model => :Generic ).load( io )
|
22
|
+
end
|
23
|
+
|
24
|
+
# this operation does not make sense in RbYAML (right now)
|
25
|
+
def self.parse_file( filepath )
|
26
|
+
# File.open( filepath ) do |f|
|
27
|
+
# parse( f )
|
28
|
+
# end
|
29
|
+
end
|
30
|
+
|
31
|
+
def self.each_document( io, &block )
|
32
|
+
_load_all(io,&block)
|
33
|
+
end
|
34
|
+
|
35
|
+
def self.load_documents( io, &doc_proc )
|
36
|
+
each_document( io, &doc_proc )
|
37
|
+
end
|
38
|
+
|
39
|
+
# this operation does not make sense in RbYAML (right now)
|
40
|
+
def self.each_node( io, &doc_proc )
|
41
|
+
# yp = @@parser.new( :Model => :Generic ).load_documents( io, &doc_proc )
|
42
|
+
end
|
43
|
+
|
44
|
+
# this operation does not make sense in RbYAML (right now)
|
45
|
+
def self.parse_documents( io, &doc_proc )
|
46
|
+
# YAML.each_node( io, &doc_proc )
|
47
|
+
end
|
48
|
+
|
49
|
+
def self.load_stream( io )
|
50
|
+
d = nil
|
51
|
+
load_documents(io) { |doc|
|
52
|
+
d = Stream.new( nil ) if not d
|
53
|
+
d.add( doc )
|
54
|
+
}
|
55
|
+
d
|
56
|
+
end
|
57
|
+
|
58
|
+
def self.dump_stream( *objs )
|
59
|
+
d = RbYAML::Stream.new
|
60
|
+
objs.each do |doc|
|
61
|
+
d.add( doc )
|
62
|
+
end
|
63
|
+
d.emit
|
64
|
+
end
|
65
|
+
|
66
|
+
|
67
|
+
def self.add_builtin_ctor(type_tag, &transfer_proc)
|
68
|
+
BaseConstructor::add_constructor("tag:yaml.org,2002:#{ type_tag }",transfer_proc)
|
69
|
+
end
|
70
|
+
|
71
|
+
# this operation does not make sense in RbYAML (right now)
|
72
|
+
def self.add_domain_type( domain, type_re, &transfer_proc )
|
73
|
+
# @@loader.add_domain_type( domain, type_re, &transfer_proc )
|
74
|
+
end
|
75
|
+
|
76
|
+
# this operation does not make sense in RbYAML (right now)
|
77
|
+
def self.add_builtin_type( type_re, &transfer_proc )
|
78
|
+
# @@loader.add_builtin_type( type_re, &transfer_proc )
|
79
|
+
end
|
80
|
+
|
81
|
+
# this operation does not make sense in RbYAML (right now)
|
82
|
+
def self.add_ruby_type( type_tag, &transfer_proc )
|
83
|
+
# @@loader.add_ruby_type( type, &transfer_proc )
|
84
|
+
end
|
85
|
+
|
86
|
+
# this operation does not make sense in RbYAML (right now)
|
87
|
+
def self.add_private_type( type_re, &transfer_proc )
|
88
|
+
# @@loader.add_private_type( type_re, &transfer_proc )
|
89
|
+
end
|
90
|
+
|
91
|
+
def self.detect_implicit( val )
|
92
|
+
SimpleDetector.detect(val)
|
93
|
+
end
|
94
|
+
|
95
|
+
# this operation does not make sense in RbYAML (right now)
|
96
|
+
def self.transfer( type_id, obj )
|
97
|
+
# @@loader.transfer( type_id, obj )
|
98
|
+
end
|
99
|
+
|
100
|
+
# this operation does not make sense in RbYAML (right now)
|
101
|
+
def self.try_implicit( obj )
|
102
|
+
# YAML.transfer( YAML.detect_implicit( obj ), obj )
|
103
|
+
end
|
104
|
+
|
105
|
+
def self.read_type_class( type, obj_class )
|
106
|
+
scheme, domain, type, tclass = type.split( ':', 4 )
|
107
|
+
tclass.split( "::" ).each { |c| obj_class = obj_class.const_get( c ) } if tclass
|
108
|
+
return [ type, obj_class ]
|
109
|
+
end
|
110
|
+
|
111
|
+
def self.object_maker( obj_class, val )
|
112
|
+
if Hash === val
|
113
|
+
o = obj_class.allocate
|
114
|
+
val.each_pair { |k,v|
|
115
|
+
o.instance_variable_set("@#{k}", v)
|
116
|
+
}
|
117
|
+
o
|
118
|
+
else
|
119
|
+
raise YAMLError, "Invalid object explicitly tagged !ruby/Object: " + val.inspect
|
120
|
+
end
|
121
|
+
end
|
122
|
+
|
123
|
+
# this operation does not make sense in RbYAML (right now)
|
124
|
+
def self.quick_emit( oid, opts = {}, &e )
|
125
|
+
end
|
126
|
+
|
127
|
+
# A dictionary of taguris which map to
|
128
|
+
# Ruby classes.
|
129
|
+
@@tagged_classes = {}
|
130
|
+
|
131
|
+
#
|
132
|
+
# Associates a taguri _tag_ with a Ruby class _cls_. The taguri is used to give types
|
133
|
+
# to classes when loading YAML. Taguris are of the form:
|
134
|
+
#
|
135
|
+
# tag:authorityName,date:specific
|
136
|
+
#
|
137
|
+
# The +authorityName+ is a domain name or email address. The +date+ is the date the type
|
138
|
+
# was issued in YYYY or YYYY-MM or YYYY-MM-DD format. The +specific+ is a name for
|
139
|
+
# the type being added.
|
140
|
+
#
|
141
|
+
# For example, built-in YAML types have 'yaml.org' as the +authorityName+ and '2002' as the
|
142
|
+
# +date+. The +specific+ is simply the name of the type:
|
143
|
+
#
|
144
|
+
# tag:yaml.org,2002:int
|
145
|
+
# tag:yaml.org,2002:float
|
146
|
+
# tag:yaml.org,2002:timestamp
|
147
|
+
#
|
148
|
+
# The domain must be owned by you on the +date+ declared. If you don't own any domains on the
|
149
|
+
# date you declare the type, you can simply use an e-mail address.
|
150
|
+
#
|
151
|
+
# tag:why@ruby-lang.org,2004:notes/personal
|
152
|
+
#
|
153
|
+
def self.tag_class( tag, cls )
|
154
|
+
if @@tagged_classes.has_key? tag
|
155
|
+
warn "class #{ @@tagged_classes[tag] } held ownership of the #{ tag } tag"
|
156
|
+
end
|
157
|
+
@@tagged_classes[tag] = cls
|
158
|
+
end
|
159
|
+
|
160
|
+
# Returns the complete dictionary of taguris, paired with classes. The key for
|
161
|
+
# the dictionary is the full taguri. The value for each key is the class constant
|
162
|
+
# associated to that taguri.
|
163
|
+
#
|
164
|
+
# YAML.tagged_classes["tag:yaml.org,2002:int"] => Integer
|
165
|
+
#
|
166
|
+
def self.tagged_classes
|
167
|
+
@@tagged_classes
|
168
|
+
end
|
169
|
+
|
170
|
+
#
|
171
|
+
# RbYAML::Stream -- for emitting many documents
|
172
|
+
#
|
173
|
+
class Stream
|
174
|
+
include Enumerable
|
175
|
+
|
176
|
+
attr_accessor :documents, :options
|
177
|
+
def initialize(opts = {})
|
178
|
+
@options = opts
|
179
|
+
@documents = []
|
180
|
+
end
|
181
|
+
|
182
|
+
def [](i)
|
183
|
+
@documents[ i ]
|
184
|
+
end
|
185
|
+
|
186
|
+
def add(doc)
|
187
|
+
@documents << doc
|
188
|
+
end
|
189
|
+
|
190
|
+
def edit(doc_num,doc)
|
191
|
+
@documents[ doc_num ] = doc
|
192
|
+
end
|
193
|
+
|
194
|
+
def each(&block)
|
195
|
+
@documents.each(&block)
|
196
|
+
end
|
197
|
+
|
198
|
+
def emit
|
199
|
+
# TODO: implement
|
200
|
+
|
201
|
+
opts = @options.dup
|
202
|
+
opts[:UseHeader] = true if @documents.length > 1
|
203
|
+
ct = 0
|
204
|
+
out = Emitter.new( opts )
|
205
|
+
@documents.each { |v|
|
206
|
+
if ct > 0
|
207
|
+
out << "\n--- "
|
208
|
+
end
|
209
|
+
v.to_yaml( :Emitter => out )
|
210
|
+
ct += 1
|
211
|
+
}
|
212
|
+
out.end_object
|
213
|
+
end
|
214
|
+
end
|
215
|
+
end
|
216
|
+
|
217
|
+
if !Object.method_defined?(:to_yaml)
|
218
|
+
class Module # :nodoc: all
|
219
|
+
def yaml_as( tag, sc = true )
|
220
|
+
class_eval <<-"end;", __FILE__, __LINE__+1
|
221
|
+
attr_writer :taguri
|
222
|
+
def taguri
|
223
|
+
return @taguri if defined?(@taguri) and @taguri
|
224
|
+
tag = #{ tag.dump }
|
225
|
+
if self.class.yaml_tag_subclasses? and self.class != RbYAML::tagged_classes[tag]
|
226
|
+
tag = "\#{ tag }:\#{ self.class.yaml_tag_class_name }"
|
227
|
+
end
|
228
|
+
tag
|
229
|
+
end
|
230
|
+
def self.yaml_tag_subclasses?; #{ sc ? 'true' : 'false' }; end
|
231
|
+
end;
|
232
|
+
RbYAML::tag_class tag, self
|
233
|
+
end
|
234
|
+
# Transforms the subclass name into a name suitable for display
|
235
|
+
# in a subclassed tag.
|
236
|
+
def yaml_tag_class_name
|
237
|
+
self.name
|
238
|
+
end
|
239
|
+
# Transforms the subclass name found in the tag into a Ruby
|
240
|
+
# constant name.
|
241
|
+
def yaml_tag_read_class( name )
|
242
|
+
name
|
243
|
+
end
|
244
|
+
end
|
245
|
+
|
246
|
+
require 'date'
|
247
|
+
|
248
|
+
class Class
|
249
|
+
def to_yaml( opts = {} )
|
250
|
+
raise RbYAML::TypeError, "can't dump anonymous class %s" % self.class
|
251
|
+
end
|
252
|
+
end
|
253
|
+
|
254
|
+
class Object
|
255
|
+
yaml_as "tag:ruby.yaml.org,2002:object"
|
256
|
+
def is_complex_yaml?; true; end
|
257
|
+
def to_yaml_style; end
|
258
|
+
def to_yaml_properties; instance_variables.sort; end
|
259
|
+
def to_yaml( opts = {} )
|
260
|
+
RbYAML::_dump_ruby_object(self)
|
261
|
+
end
|
262
|
+
end
|
263
|
+
|
264
|
+
class Hash
|
265
|
+
yaml_as "tag:ruby.yaml.org,2002:hash"
|
266
|
+
yaml_as "tag:yaml.org,2002:map"
|
267
|
+
def is_complex_yaml?; true; end
|
268
|
+
def yaml_initialize( tag, val )
|
269
|
+
if Array === val
|
270
|
+
update Hash.[]( *val ) # Convert the map to a sequence
|
271
|
+
elsif Hash === val
|
272
|
+
update val
|
273
|
+
else
|
274
|
+
raise RbYAML::TypeError, "Invalid map explicitly tagged #{ tag }: " + val.inspect
|
275
|
+
end
|
276
|
+
end
|
277
|
+
end
|
278
|
+
|
279
|
+
class Array
|
280
|
+
yaml_as "tag:ruby.yaml.org,2002:array"
|
281
|
+
yaml_as "tag:yaml.org,2002:seq"
|
282
|
+
def is_complex_yaml?; true; end
|
283
|
+
def yaml_initialize( tag, val ); concat( val.to_a ); end
|
284
|
+
end
|
285
|
+
|
286
|
+
class Exception
|
287
|
+
yaml_as "tag:ruby.yaml.org,2002:exception"
|
288
|
+
def Exception.yaml_new( klass, tag, val )
|
289
|
+
o = RbYAML.object_maker( klass, { 'mesg' => val.delete( 'message' ) } )
|
290
|
+
val.each_pair do |k,v|
|
291
|
+
o.instance_variable_set("@#{k}", v)
|
292
|
+
end
|
293
|
+
o
|
294
|
+
end
|
295
|
+
end
|
296
|
+
|
297
|
+
class String
|
298
|
+
yaml_as "tag:ruby.yaml.org,2002:string"
|
299
|
+
yaml_as "tag:yaml.org,2002:binary"
|
300
|
+
yaml_as "tag:yaml.org,2002:str"
|
301
|
+
def is_complex_yaml?
|
302
|
+
to_yaml_style or not to_yaml_properties.empty? or self =~ /\n.+/
|
303
|
+
end
|
304
|
+
def is_binary_data?
|
305
|
+
( self.count( "^ -~", "^\r\n" ) / self.size > 0.3 || self.count( "\x00" ) > 0 ) unless empty?
|
306
|
+
end
|
307
|
+
def String.yaml_new( klass, tag, val )
|
308
|
+
val = val.unpack("m")[0] if tag == "tag:yaml.org,2002:binary"
|
309
|
+
val = { 'str' => val } if String === val
|
310
|
+
if Hash === val
|
311
|
+
s = klass.allocate
|
312
|
+
# Thank you, NaHi
|
313
|
+
String.instance_method(:initialize).
|
314
|
+
bind(s).
|
315
|
+
call( val.delete( 'str' ) )
|
316
|
+
val.each { |k,v| s.instance_variable_set( k, v ) }
|
317
|
+
s
|
318
|
+
else
|
319
|
+
raise RbYAML::TypeError, "Invalid String: " + val.inspect
|
320
|
+
end
|
321
|
+
end
|
322
|
+
end
|
323
|
+
|
324
|
+
class Symbol
|
325
|
+
yaml_as "tag:ruby.yaml.org,2002:symbol"
|
326
|
+
yaml_as "tag:ruby.yaml.org,2002:sym"
|
327
|
+
def is_complex_yaml?; false; end
|
328
|
+
def Symbol.yaml_new( klass, tag, val )
|
329
|
+
if String === val
|
330
|
+
val.intern
|
331
|
+
else
|
332
|
+
raise RbYAML::TypeError, "Invalid Symbol: " + val.inspect
|
333
|
+
end
|
334
|
+
end
|
335
|
+
end
|
336
|
+
|
337
|
+
class Time
|
338
|
+
yaml_as "tag:ruby.yaml.org,2002:time"
|
339
|
+
yaml_as "tag:yaml.org,2002:timestamp"
|
340
|
+
def is_complex_yaml?; false; end
|
341
|
+
def Time.yaml_new( klass, tag, val )
|
342
|
+
if Hash === val
|
343
|
+
t = val.delete( 'at' )
|
344
|
+
val.each { |k,v| t.instance_variable_set( k, v ) }
|
345
|
+
t
|
346
|
+
else
|
347
|
+
raise RbYAML::TypeError, "Invalid Time: " + val.inspect
|
348
|
+
end
|
349
|
+
end
|
350
|
+
end
|
351
|
+
|
352
|
+
class Date
|
353
|
+
yaml_as "tag:yaml.org,2002:timestamp#ymd"
|
354
|
+
def is_complex_yaml?; false; end
|
355
|
+
end
|
356
|
+
|
357
|
+
class Numeric
|
358
|
+
def is_complex_yaml?; false; end
|
359
|
+
end
|
360
|
+
|
361
|
+
class Fixnum
|
362
|
+
yaml_as "tag:yaml.org,2002:int"
|
363
|
+
end
|
364
|
+
|
365
|
+
class Float
|
366
|
+
yaml_as "tag:yaml.org,2002:float"
|
367
|
+
end
|
368
|
+
|
369
|
+
class TrueClass
|
370
|
+
yaml_as "tag:yaml.org,2002:bool#yes"
|
371
|
+
def is_complex_yaml?; false; end
|
372
|
+
end
|
373
|
+
|
374
|
+
class FalseClass
|
375
|
+
yaml_as "tag:yaml.org,2002:bool#no"
|
376
|
+
def is_complex_yaml?; false; end
|
377
|
+
end
|
378
|
+
|
379
|
+
class NilClass
|
380
|
+
yaml_as "tag:yaml.org,2002:null"
|
381
|
+
def is_complex_yaml?; false; end
|
382
|
+
end
|
383
|
+
end
|