1 # Copyright (c) 2006 Ola Bini
3 # Permission is hereby granted, free of charge, to any person obtaining a copy
4 # of this software and associated documentation files (the "Software"), to
5 # deal in the Software without restriction, including without limitation the
6 # rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
7 # sell copies of the Software, and to permit persons to whom the Software is
8 # furnished to do so, subject to the following conditions:
10 # The above copyright notice and this permission notice shall be included in
11 # all copies or substantial portions of the Software.
13 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 require 'rbyaml/stream'
23 require 'rbyaml/dumper'
26 def self.dump(obj, io = nil, opts={})
27 _dump(obj,io,Dumper,opts)
31 _load(io) rescue false
34 def self.load_file( filepath )
35 File.open( filepath ) do |f|
40 # this operation does not make sense in RbYAML (right now)
42 raise NotImplementedError
43 # yp = @@parser.new( :Model => :Generic ).load( io )
46 # this operation does not make sense in RbYAML (right now)
47 def self.parse_file( filepath )
48 raise NotImplementedError
49 # File.open( filepath ) do |f|
54 def self.each_document( io, &block )
58 def self.load_documents( io, &doc_proc )
59 each_document( io, &doc_proc )
62 # this operation does not make sense in RbYAML (right now)
63 def self.each_node( io, &doc_proc )
64 raise NotImplementedError
65 # yp = @@parser.new( :Model => :Generic ).load_documents( io, &doc_proc )
68 # this operation does not make sense in RbYAML (right now)
69 def self.parse_documents( io, &doc_proc )
70 raise NotImplementedError
71 # YAML.each_node( io, &doc_proc )
74 def self.load_stream( io )
76 load_documents(io) { |doc|
77 d = Stream.new( nil ) if not d
83 def self.dump_stream( *objs )
84 d = RbYAML::Stream.new
92 def self.add_builtin_ctor(type_tag, &transfer_proc)
93 BaseConstructor::add_constructor("tag:yaml.org,2002:#{ type_tag }",transfer_proc)
96 # this operation does not make sense in RbYAML (right now)
97 def self.add_domain_type( domain, type_re, &transfer_proc )
98 raise NotImplementedError
99 # @@loader.add_domain_type( domain, type_re, &transfer_proc )
102 # this operation does not make sense in RbYAML (right now)
103 def self.add_builtin_type( type_re, &transfer_proc )
104 raise NotImplementedError
105 # @@loader.add_builtin_type( type_re, &transfer_proc )
108 # this operation does not make sense in RbYAML (right now)
109 def self.add_ruby_type( type_tag, &transfer_proc )
110 raise NotImplementedError
111 # @@loader.add_ruby_type( type, &transfer_proc )
114 # this operation does not make sense in RbYAML (right now)
115 def self.add_private_type( type_re, &transfer_proc )
116 raise NotImplementedError
117 # @@loader.add_private_type( type_re, &transfer_proc )
120 def self.detect_implicit( val )
121 SimpleDetector.detect(val)
124 # Convert a type_id to a taguri
125 def self.tagurize(val)
126 val.class == String ? "#{RbYAML::Parser::DEFAULT_TAGS["!!"]}#{val}" : val
129 # this operation does not make sense in RbYAML (right now)
130 def self.transfer( type_id, obj )
131 raise NotImplementedError
132 # @@loader.transfer( type_id, obj )
135 # this operation does not make sense in RbYAML (right now)
136 def self.try_implicit( obj )
137 raise NotImplementedError
138 # YAML.transfer( YAML.detect_implicit( obj ), obj )
141 def self.read_type_class( type, obj_class )
142 scheme, domain, type, tclass = type.split( ':', 4 )
143 tclass.split( "::" ).each { |c| obj_class = obj_class.const_get( c ) } if tclass
144 return [ type, obj_class ]
147 def self.object_maker( obj_class, val )
149 o = obj_class.allocate
150 val.each_pair { |k,v|
151 o.instance_variable_set("@#{k}", v)
155 raise YAMLError, "Invalid object explicitly tagged !ruby/Object: " + val.inspect
159 def self.quick_emit(oid, opts = {})
160 if Dumper === opts then
163 stream = StringIO.new
164 dumper = Dumper.new stream, opts
165 dumper.serializer.open
166 rep = dumper.representer
169 node = RbYAML.quick_emit_node oid, rep do |out|
173 #to_yaml_properties.each do |m|
174 # map[m[1..-1]] = instance_variable_get(m)
176 #out.map taguri, map, to_yaml_style
179 dumper.serializer.serialize node
181 dumper.serializer.close
185 def self.quick_emit_node( oid, rep, &e )
191 require 'rbyaml/types'
192 require 'rbyaml/rubytypes'