1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
|
# vim:sw=4:ts=4
# $Id$
#
# YAML.rb
#
# Loads the parser/loader and emitter/writer.
#
module YAML
begin
require 'syck'
Parser = YAML::Syck::Parser
rescue LoadError
require 'yaml/parser'
Parser = YAML::Parser
end
require 'yaml/emitter'
require 'yaml/rubytypes'
#
# Allocate blank object
#
def YAML.object_maker( obj_class, val )
if Hash === val
name = obj_class.name
o = ::Marshal.load( sprintf( "\004\006o:%c%s\000", name.length + 5, name ))
val.each_pair { |k,v|
o.instance_eval "@#{k} = v"
}
o
else
raise YAML::Error, "Invalid object explicitly tagged !ruby/Object: " + val.inspect
end
end
#
# Input methods
#
#
# Load a single document from the current stream
#
def YAML.load( io )
yp = YAML::Parser.new.parse( io )
end
#
# Parse a single document from the current stream
#
def YAML.parse( io )
yp = YAML::Parser.new( :Model => :Generic ).parse( io )
end
#
# Load all documents from the current stream
#
def YAML.each_document( io, &doc_proc )
yp = YAML::Parser.new.parse_documents( io, &doc_proc )
end
#
# Identical to each_document
#
def YAML.load_documents( io, &doc_proc )
YAML.each_document( io, &doc_proc )
end
#
# Parse all documents from the current stream
#
def YAML.each_node( io, &doc_proc )
yp = YAML::Parser.new( :Model => :Generic ).parse_documents( io, &doc_proc )
end
#
# Parse all documents from the current stream
#
def YAML.parse_documents( io, &doc_proc )
YAML.each_node( io, &doc_proc )
end
#
# Load all documents from the current stream
#
def YAML.load_stream( io )
yp = YAML::Parser.new
d = nil
yp.parse_documents( io ) { |doc|
d = YAML::Stream.new( yp.options ) if not d
d.add( doc )
}
return d
end
end
#
# 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
# YAML.
#
# _why: (listening)
#
# ryan: I know you don't want to urinate all over your users' namespaces.
# But, on the other hand, convenience of dumping for debugging is,
# IMO, a big YAML use case.
#
# _why: Go nuts! Have a pony parade!
#
# ryan: Either way, I certainly will have a pony parade.
#
module Kernel
def y( x )
puts x.to_yaml
end
end
|