psych-shopifork 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +15 -0
- data/.autotest +18 -0
- data/.gemtest +0 -0
- data/.travis.yml +9 -0
- data/CHANGELOG.rdoc +414 -0
- data/Manifest.txt +113 -0
- data/README.rdoc +71 -0
- data/Rakefile +72 -0
- data/ext/psych/depend +3 -0
- data/ext/psych/extconf.rb +36 -0
- data/ext/psych/psych.c +34 -0
- data/ext/psych/psych.h +20 -0
- data/ext/psych/psych_emitter.c +538 -0
- data/ext/psych/psych_emitter.h +8 -0
- data/ext/psych/psych_parser.c +579 -0
- data/ext/psych/psych_parser.h +6 -0
- data/ext/psych/psych_to_ruby.c +43 -0
- data/ext/psych/psych_to_ruby.h +8 -0
- data/ext/psych/psych_yaml_tree.c +24 -0
- data/ext/psych/psych_yaml_tree.h +8 -0
- data/ext/psych/yaml/LICENSE +19 -0
- data/ext/psych/yaml/api.c +1392 -0
- data/ext/psych/yaml/config.h +11 -0
- data/ext/psych/yaml/dumper.c +394 -0
- data/ext/psych/yaml/emitter.c +2335 -0
- data/ext/psych/yaml/loader.c +432 -0
- data/ext/psych/yaml/parser.c +1374 -0
- data/ext/psych/yaml/reader.c +465 -0
- data/ext/psych/yaml/scanner.c +3570 -0
- data/ext/psych/yaml/writer.c +141 -0
- data/ext/psych/yaml/yaml.h +1971 -0
- data/ext/psych/yaml/yaml_private.h +643 -0
- data/lib/psych.rb +497 -0
- data/lib/psych/class_loader.rb +101 -0
- data/lib/psych/coder.rb +94 -0
- data/lib/psych/core_ext.rb +35 -0
- data/lib/psych/deprecated.rb +85 -0
- data/lib/psych/exception.rb +13 -0
- data/lib/psych/handler.rb +249 -0
- data/lib/psych/handlers/document_stream.rb +22 -0
- data/lib/psych/handlers/recorder.rb +39 -0
- data/lib/psych/json/ruby_events.rb +19 -0
- data/lib/psych/json/stream.rb +16 -0
- data/lib/psych/json/tree_builder.rb +12 -0
- data/lib/psych/json/yaml_events.rb +29 -0
- data/lib/psych/nodes.rb +77 -0
- data/lib/psych/nodes/alias.rb +18 -0
- data/lib/psych/nodes/document.rb +60 -0
- data/lib/psych/nodes/mapping.rb +56 -0
- data/lib/psych/nodes/node.rb +55 -0
- data/lib/psych/nodes/scalar.rb +67 -0
- data/lib/psych/nodes/sequence.rb +81 -0
- data/lib/psych/nodes/stream.rb +37 -0
- data/lib/psych/omap.rb +4 -0
- data/lib/psych/parser.rb +51 -0
- data/lib/psych/scalar_scanner.rb +149 -0
- data/lib/psych/set.rb +4 -0
- data/lib/psych/stream.rb +37 -0
- data/lib/psych/streaming.rb +27 -0
- data/lib/psych/syntax_error.rb +21 -0
- data/lib/psych/tree_builder.rb +96 -0
- data/lib/psych/visitors.rb +6 -0
- data/lib/psych/visitors/depth_first.rb +26 -0
- data/lib/psych/visitors/emitter.rb +51 -0
- data/lib/psych/visitors/json_tree.rb +24 -0
- data/lib/psych/visitors/to_ruby.rb +372 -0
- data/lib/psych/visitors/visitor.rb +19 -0
- data/lib/psych/visitors/yaml_tree.rb +507 -0
- data/lib/psych/y.rb +9 -0
- data/test/psych/handlers/test_recorder.rb +25 -0
- data/test/psych/helper.rb +114 -0
- data/test/psych/json/test_stream.rb +109 -0
- data/test/psych/nodes/test_enumerable.rb +43 -0
- data/test/psych/test_alias_and_anchor.rb +96 -0
- data/test/psych/test_array.rb +57 -0
- data/test/psych/test_boolean.rb +36 -0
- data/test/psych/test_class.rb +36 -0
- data/test/psych/test_coder.rb +184 -0
- data/test/psych/test_date_time.rb +25 -0
- data/test/psych/test_deprecated.rb +214 -0
- data/test/psych/test_document.rb +46 -0
- data/test/psych/test_emitter.rb +94 -0
- data/test/psych/test_encoding.rb +254 -0
- data/test/psych/test_engine_manager.rb +47 -0
- data/test/psych/test_exception.rb +151 -0
- data/test/psych/test_hash.rb +44 -0
- data/test/psych/test_json_tree.rb +65 -0
- data/test/psych/test_merge_keys.rb +132 -0
- data/test/psych/test_nil.rb +18 -0
- data/test/psych/test_null.rb +19 -0
- data/test/psych/test_numeric.rb +45 -0
- data/test/psych/test_object.rb +44 -0
- data/test/psych/test_object_references.rb +67 -0
- data/test/psych/test_omap.rb +75 -0
- data/test/psych/test_parser.rb +339 -0
- data/test/psych/test_psych.rb +168 -0
- data/test/psych/test_safe_load.rb +97 -0
- data/test/psych/test_scalar.rb +11 -0
- data/test/psych/test_scalar_scanner.rb +106 -0
- data/test/psych/test_serialize_subclasses.rb +38 -0
- data/test/psych/test_set.rb +49 -0
- data/test/psych/test_stream.rb +93 -0
- data/test/psych/test_string.rb +153 -0
- data/test/psych/test_struct.rb +49 -0
- data/test/psych/test_symbol.rb +17 -0
- data/test/psych/test_tainted.rb +130 -0
- data/test/psych/test_to_yaml_properties.rb +63 -0
- data/test/psych/test_tree_builder.rb +79 -0
- data/test/psych/test_yaml.rb +1289 -0
- data/test/psych/test_yamldbm.rb +197 -0
- data/test/psych/test_yamlstore.rb +87 -0
- data/test/psych/visitors/test_depth_first.rb +49 -0
- data/test/psych/visitors/test_emitter.rb +144 -0
- data/test/psych/visitors/test_to_ruby.rb +326 -0
- data/test/psych/visitors/test_yaml_tree.rb +173 -0
- metadata +257 -0
@@ -0,0 +1,19 @@
|
|
1
|
+
module Psych
|
2
|
+
module Visitors
|
3
|
+
class Visitor
|
4
|
+
def accept target
|
5
|
+
visit target
|
6
|
+
end
|
7
|
+
|
8
|
+
private
|
9
|
+
|
10
|
+
DISPATCH = Hash.new do |hash, klass|
|
11
|
+
hash[klass] = "visit_#{klass.name.gsub('::', '_')}"
|
12
|
+
end
|
13
|
+
|
14
|
+
def visit target
|
15
|
+
send DISPATCH[target.class], target
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,507 @@
|
|
1
|
+
require 'psych/tree_builder'
|
2
|
+
require 'psych/scalar_scanner'
|
3
|
+
require 'psych/class_loader'
|
4
|
+
|
5
|
+
module Psych
|
6
|
+
module Visitors
|
7
|
+
###
|
8
|
+
# YAMLTree builds a YAML ast given a Ruby object. For example:
|
9
|
+
#
|
10
|
+
# builder = Psych::Visitors::YAMLTree.new
|
11
|
+
# builder << { :foo => 'bar' }
|
12
|
+
# builder.tree # => #<Psych::Nodes::Stream .. }
|
13
|
+
#
|
14
|
+
class YAMLTree < Psych::Visitors::Visitor
|
15
|
+
class Registrar # :nodoc:
|
16
|
+
def initialize
|
17
|
+
@obj_to_id = {}
|
18
|
+
@obj_to_node = {}
|
19
|
+
@counter = 0
|
20
|
+
end
|
21
|
+
|
22
|
+
def register target, node
|
23
|
+
@obj_to_node[target.object_id] = node
|
24
|
+
end
|
25
|
+
|
26
|
+
def key? target
|
27
|
+
@obj_to_node.key? target.object_id
|
28
|
+
end
|
29
|
+
|
30
|
+
def id_for target
|
31
|
+
@obj_to_id[target.object_id] ||= (@counter += 1)
|
32
|
+
end
|
33
|
+
|
34
|
+
def node_for target
|
35
|
+
@obj_to_node[target.object_id]
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
attr_reader :started, :finished
|
40
|
+
alias :finished? :finished
|
41
|
+
alias :started? :started
|
42
|
+
|
43
|
+
def self.create options = {}, emitter = nil
|
44
|
+
emitter ||= TreeBuilder.new
|
45
|
+
class_loader = ClassLoader.new
|
46
|
+
ss = ScalarScanner.new class_loader
|
47
|
+
new(emitter, ss, options)
|
48
|
+
end
|
49
|
+
|
50
|
+
def self.new emitter = nil, ss = nil, options = nil
|
51
|
+
return super if emitter && ss && options
|
52
|
+
|
53
|
+
if $VERBOSE
|
54
|
+
warn "This API is deprecated, please pass an emitter, scalar scanner, and options or call #{self}.create() (#{caller.first})"
|
55
|
+
end
|
56
|
+
create emitter, ss
|
57
|
+
end
|
58
|
+
|
59
|
+
def initialize emitter, ss, options
|
60
|
+
super()
|
61
|
+
@started = false
|
62
|
+
@finished = false
|
63
|
+
@emitter = emitter
|
64
|
+
@st = Registrar.new
|
65
|
+
@ss = ss
|
66
|
+
@options = options
|
67
|
+
@coders = []
|
68
|
+
|
69
|
+
@dispatch_cache = Hash.new do |h,klass|
|
70
|
+
method = "visit_#{(klass.name || '').split('::').join('_')}"
|
71
|
+
|
72
|
+
method = respond_to?(method) ? method : h[klass.superclass]
|
73
|
+
|
74
|
+
raise(TypeError, "Can't dump #{target.class}") unless method
|
75
|
+
|
76
|
+
h[klass] = method
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
def start encoding = Nodes::Stream::UTF8
|
81
|
+
@emitter.start_stream(encoding).tap do
|
82
|
+
@started = true
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
def finish
|
87
|
+
@emitter.end_stream.tap do
|
88
|
+
@finished = true
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
def tree
|
93
|
+
finish unless finished?
|
94
|
+
@emitter.root
|
95
|
+
end
|
96
|
+
|
97
|
+
def push object
|
98
|
+
start unless started?
|
99
|
+
version = []
|
100
|
+
version = [1,1] if @options[:header]
|
101
|
+
|
102
|
+
case @options[:version]
|
103
|
+
when Array
|
104
|
+
version = @options[:version]
|
105
|
+
when String
|
106
|
+
version = @options[:version].split('.').map { |x| x.to_i }
|
107
|
+
else
|
108
|
+
version = [1,1]
|
109
|
+
end if @options.key? :version
|
110
|
+
|
111
|
+
@emitter.start_document version, [], false
|
112
|
+
accept object
|
113
|
+
@emitter.end_document !@emitter.streaming?
|
114
|
+
end
|
115
|
+
alias :<< :push
|
116
|
+
|
117
|
+
def accept target
|
118
|
+
# return any aliases we find
|
119
|
+
if @st.key? target
|
120
|
+
oid = @st.id_for target
|
121
|
+
node = @st.node_for target
|
122
|
+
anchor = oid.to_s
|
123
|
+
node.anchor = anchor
|
124
|
+
return @emitter.alias anchor
|
125
|
+
end
|
126
|
+
|
127
|
+
if target.respond_to?(:to_yaml)
|
128
|
+
begin
|
129
|
+
loc = target.method(:to_yaml).source_location.first
|
130
|
+
if loc !~ /(syck\/rubytypes.rb|psych\/core_ext.rb)/
|
131
|
+
unless target.respond_to?(:encode_with)
|
132
|
+
if $VERBOSE
|
133
|
+
warn "implementing to_yaml is deprecated, please implement \"encode_with\""
|
134
|
+
end
|
135
|
+
|
136
|
+
target.to_yaml(:nodump => true)
|
137
|
+
end
|
138
|
+
end
|
139
|
+
rescue
|
140
|
+
# public_method or source_location might be overridden,
|
141
|
+
# and it's OK to skip it since it's only to emit a warning
|
142
|
+
end
|
143
|
+
end
|
144
|
+
|
145
|
+
if target.respond_to?(:encode_with)
|
146
|
+
dump_coder target
|
147
|
+
else
|
148
|
+
send(@dispatch_cache[target.class], target)
|
149
|
+
end
|
150
|
+
end
|
151
|
+
|
152
|
+
def visit_Psych_Omap o
|
153
|
+
seq = @emitter.start_sequence(nil, '!omap', false, Nodes::Sequence::BLOCK)
|
154
|
+
register(o, seq)
|
155
|
+
|
156
|
+
o.each { |k,v| visit_Hash k => v }
|
157
|
+
@emitter.end_sequence
|
158
|
+
end
|
159
|
+
|
160
|
+
def visit_Object o
|
161
|
+
tag = Psych.dump_tags[o.class]
|
162
|
+
unless tag
|
163
|
+
klass = o.class == Object ? nil : o.class.name
|
164
|
+
tag = ['!ruby/object', klass].compact.join(':')
|
165
|
+
end
|
166
|
+
|
167
|
+
map = @emitter.start_mapping(nil, tag, false, Nodes::Mapping::BLOCK)
|
168
|
+
register(o, map)
|
169
|
+
|
170
|
+
dump_ivars o
|
171
|
+
@emitter.end_mapping
|
172
|
+
end
|
173
|
+
|
174
|
+
def visit_Struct o
|
175
|
+
tag = ['!ruby/struct', o.class.name].compact.join(':')
|
176
|
+
|
177
|
+
register o, @emitter.start_mapping(nil, tag, false, Nodes::Mapping::BLOCK)
|
178
|
+
o.members.each do |member|
|
179
|
+
@emitter.scalar member.to_s, nil, nil, true, false, Nodes::Scalar::ANY
|
180
|
+
accept o[member]
|
181
|
+
end
|
182
|
+
|
183
|
+
dump_ivars o
|
184
|
+
|
185
|
+
@emitter.end_mapping
|
186
|
+
end
|
187
|
+
|
188
|
+
def visit_Exception o
|
189
|
+
tag = ['!ruby/exception', o.class.name].join ':'
|
190
|
+
|
191
|
+
@emitter.start_mapping nil, tag, false, Nodes::Mapping::BLOCK
|
192
|
+
|
193
|
+
{
|
194
|
+
'message' => private_iv_get(o, 'mesg'),
|
195
|
+
'backtrace' => private_iv_get(o, 'backtrace'),
|
196
|
+
}.each do |k,v|
|
197
|
+
next unless v
|
198
|
+
@emitter.scalar k, nil, nil, true, false, Nodes::Scalar::ANY
|
199
|
+
accept v
|
200
|
+
end
|
201
|
+
|
202
|
+
dump_ivars o
|
203
|
+
|
204
|
+
@emitter.end_mapping
|
205
|
+
end
|
206
|
+
|
207
|
+
def visit_Regexp o
|
208
|
+
register o, @emitter.scalar(o.inspect, nil, '!ruby/regexp', false, false, Nodes::Scalar::ANY)
|
209
|
+
end
|
210
|
+
|
211
|
+
def visit_DateTime o
|
212
|
+
formatted = format_time o.to_time
|
213
|
+
tag = '!ruby/object:DateTime'
|
214
|
+
register o, @emitter.scalar(formatted, nil, tag, false, false, Nodes::Scalar::ANY)
|
215
|
+
end
|
216
|
+
|
217
|
+
def visit_Time o
|
218
|
+
formatted = format_time o
|
219
|
+
register o, @emitter.scalar(formatted, nil, nil, true, false, Nodes::Scalar::ANY)
|
220
|
+
end
|
221
|
+
|
222
|
+
def visit_Rational o
|
223
|
+
register o, @emitter.start_mapping(nil, '!ruby/object:Rational', false, Nodes::Mapping::BLOCK)
|
224
|
+
|
225
|
+
[
|
226
|
+
'denominator', o.denominator.to_s,
|
227
|
+
'numerator', o.numerator.to_s
|
228
|
+
].each do |m|
|
229
|
+
@emitter.scalar m, nil, nil, true, false, Nodes::Scalar::ANY
|
230
|
+
end
|
231
|
+
|
232
|
+
@emitter.end_mapping
|
233
|
+
end
|
234
|
+
|
235
|
+
def visit_Complex o
|
236
|
+
register o, @emitter.start_mapping(nil, '!ruby/object:Complex', false, Nodes::Mapping::BLOCK)
|
237
|
+
|
238
|
+
['real', o.real.to_s, 'image', o.imag.to_s].each do |m|
|
239
|
+
@emitter.scalar m, nil, nil, true, false, Nodes::Scalar::ANY
|
240
|
+
end
|
241
|
+
|
242
|
+
@emitter.end_mapping
|
243
|
+
end
|
244
|
+
|
245
|
+
def visit_Integer o
|
246
|
+
@emitter.scalar o.to_s, nil, nil, true, false, Nodes::Scalar::ANY
|
247
|
+
end
|
248
|
+
alias :visit_TrueClass :visit_Integer
|
249
|
+
alias :visit_FalseClass :visit_Integer
|
250
|
+
alias :visit_Date :visit_Integer
|
251
|
+
|
252
|
+
def visit_Float o
|
253
|
+
if o.nan?
|
254
|
+
@emitter.scalar '.nan', nil, nil, true, false, Nodes::Scalar::ANY
|
255
|
+
elsif o.infinite?
|
256
|
+
@emitter.scalar((o.infinite? > 0 ? '.inf' : '-.inf'),
|
257
|
+
nil, nil, true, false, Nodes::Scalar::ANY)
|
258
|
+
else
|
259
|
+
@emitter.scalar o.to_s, nil, nil, true, false, Nodes::Scalar::ANY
|
260
|
+
end
|
261
|
+
end
|
262
|
+
|
263
|
+
def visit_BigDecimal o
|
264
|
+
@emitter.scalar o._dump, nil, '!ruby/object:BigDecimal', false, false, Nodes::Scalar::ANY
|
265
|
+
end
|
266
|
+
|
267
|
+
def binary? string
|
268
|
+
(string.encoding == Encoding::ASCII_8BIT && !string.ascii_only?) ||
|
269
|
+
string.index("\x00") ||
|
270
|
+
string.count("\x00-\x7F", "^ -~\t\r\n").fdiv(string.length) > 0.3 ||
|
271
|
+
string.class != String
|
272
|
+
end
|
273
|
+
private :binary?
|
274
|
+
|
275
|
+
def visit_String o
|
276
|
+
plain = true
|
277
|
+
quote = true
|
278
|
+
style = Nodes::Scalar::PLAIN
|
279
|
+
tag = nil
|
280
|
+
str = o
|
281
|
+
|
282
|
+
if binary?(o)
|
283
|
+
str = [o].pack('m').chomp
|
284
|
+
tag = '!binary' # FIXME: change to below when syck is removed
|
285
|
+
#tag = 'tag:yaml.org,2002:binary'
|
286
|
+
style = Nodes::Scalar::LITERAL
|
287
|
+
plain = false
|
288
|
+
quote = false
|
289
|
+
elsif o =~ /\n/
|
290
|
+
style = Nodes::Scalar::LITERAL
|
291
|
+
elsif o =~ /^\W/
|
292
|
+
style = Nodes::Scalar::DOUBLE_QUOTED
|
293
|
+
else
|
294
|
+
unless String === @ss.tokenize(o)
|
295
|
+
style = Nodes::Scalar::SINGLE_QUOTED
|
296
|
+
end
|
297
|
+
end
|
298
|
+
|
299
|
+
ivars = find_ivars o
|
300
|
+
|
301
|
+
if ivars.empty?
|
302
|
+
unless o.class == ::String
|
303
|
+
tag = "!ruby/string:#{o.class}"
|
304
|
+
end
|
305
|
+
@emitter.scalar str, nil, tag, plain, quote, style
|
306
|
+
else
|
307
|
+
maptag = '!ruby/string'
|
308
|
+
maptag << ":#{o.class}" unless o.class == ::String
|
309
|
+
|
310
|
+
register o, @emitter.start_mapping(nil, maptag, false, Nodes::Mapping::BLOCK)
|
311
|
+
@emitter.scalar 'str', nil, nil, true, false, Nodes::Scalar::ANY
|
312
|
+
@emitter.scalar str, nil, tag, plain, quote, style
|
313
|
+
|
314
|
+
dump_ivars o
|
315
|
+
|
316
|
+
@emitter.end_mapping
|
317
|
+
end
|
318
|
+
end
|
319
|
+
|
320
|
+
def visit_Module o
|
321
|
+
raise TypeError, "can't dump anonymous module: #{o}" unless o.name
|
322
|
+
register o, @emitter.scalar(o.name, nil, '!ruby/module', false, false, Nodes::Scalar::SINGLE_QUOTED)
|
323
|
+
end
|
324
|
+
|
325
|
+
def visit_Class o
|
326
|
+
raise TypeError, "can't dump anonymous class: #{o}" unless o.name
|
327
|
+
register o, @emitter.scalar(o.name, nil, '!ruby/class', false, false, Nodes::Scalar::SINGLE_QUOTED)
|
328
|
+
end
|
329
|
+
|
330
|
+
def visit_Range o
|
331
|
+
register o, @emitter.start_mapping(nil, '!ruby/range', false, Nodes::Mapping::BLOCK)
|
332
|
+
['begin', o.begin, 'end', o.end, 'excl', o.exclude_end?].each do |m|
|
333
|
+
accept m
|
334
|
+
end
|
335
|
+
@emitter.end_mapping
|
336
|
+
end
|
337
|
+
|
338
|
+
def visit_Hash o
|
339
|
+
tag = o.class == ::Hash ? nil : "!ruby/hash:#{o.class}"
|
340
|
+
implicit = !tag
|
341
|
+
|
342
|
+
register(o, @emitter.start_mapping(nil, tag, implicit, Psych::Nodes::Mapping::BLOCK))
|
343
|
+
|
344
|
+
o.each do |k,v|
|
345
|
+
accept k
|
346
|
+
accept v
|
347
|
+
end
|
348
|
+
|
349
|
+
@emitter.end_mapping
|
350
|
+
end
|
351
|
+
|
352
|
+
def visit_Psych_Set o
|
353
|
+
register(o, @emitter.start_mapping(nil, '!set', false, Psych::Nodes::Mapping::BLOCK))
|
354
|
+
|
355
|
+
o.each do |k,v|
|
356
|
+
accept k
|
357
|
+
accept v
|
358
|
+
end
|
359
|
+
|
360
|
+
@emitter.end_mapping
|
361
|
+
end
|
362
|
+
|
363
|
+
def visit_Array o
|
364
|
+
if o.class == ::Array
|
365
|
+
register o, @emitter.start_sequence(nil, nil, true, Nodes::Sequence::BLOCK)
|
366
|
+
o.each { |c| accept c }
|
367
|
+
@emitter.end_sequence
|
368
|
+
else
|
369
|
+
visit_array_subclass o
|
370
|
+
end
|
371
|
+
end
|
372
|
+
|
373
|
+
def visit_NilClass o
|
374
|
+
@emitter.scalar('', nil, 'tag:yaml.org,2002:null', true, false, Nodes::Scalar::ANY)
|
375
|
+
end
|
376
|
+
|
377
|
+
def visit_Symbol o
|
378
|
+
@emitter.scalar ":#{o}", nil, nil, true, false, Nodes::Scalar::ANY
|
379
|
+
end
|
380
|
+
|
381
|
+
private
|
382
|
+
def visit_array_subclass o
|
383
|
+
tag = "!ruby/array:#{o.class}"
|
384
|
+
if o.instance_variables.empty?
|
385
|
+
node = @emitter.start_sequence(nil, tag, false, Nodes::Sequence::BLOCK)
|
386
|
+
register o, node
|
387
|
+
o.each { |c| accept c }
|
388
|
+
@emitter.end_sequence
|
389
|
+
else
|
390
|
+
node = @emitter.start_mapping(nil, tag, false, Nodes::Sequence::BLOCK)
|
391
|
+
register o, node
|
392
|
+
|
393
|
+
# Dump the internal list
|
394
|
+
accept 'internal'
|
395
|
+
@emitter.start_sequence(nil, nil, true, Nodes::Sequence::BLOCK)
|
396
|
+
o.each { |c| accept c }
|
397
|
+
@emitter.end_sequence
|
398
|
+
|
399
|
+
# Dump the ivars
|
400
|
+
accept 'ivars'
|
401
|
+
@emitter.start_mapping(nil, nil, true, Nodes::Sequence::BLOCK)
|
402
|
+
o.instance_variables.each do |ivar|
|
403
|
+
accept ivar
|
404
|
+
accept o.instance_variable_get ivar
|
405
|
+
end
|
406
|
+
@emitter.end_mapping
|
407
|
+
|
408
|
+
@emitter.end_mapping
|
409
|
+
end
|
410
|
+
end
|
411
|
+
|
412
|
+
def dump_list o
|
413
|
+
end
|
414
|
+
|
415
|
+
# '%:z' was no defined until 1.9.3
|
416
|
+
if RUBY_VERSION < '1.9.3'
|
417
|
+
def format_time time
|
418
|
+
formatted = time.strftime("%Y-%m-%d %H:%M:%S.%9N")
|
419
|
+
|
420
|
+
if time.utc?
|
421
|
+
formatted += " Z"
|
422
|
+
else
|
423
|
+
zone = time.strftime('%z')
|
424
|
+
formatted += " #{zone[0,3]}:#{zone[3,5]}"
|
425
|
+
end
|
426
|
+
|
427
|
+
formatted
|
428
|
+
end
|
429
|
+
else
|
430
|
+
def format_time time
|
431
|
+
if time.utc?
|
432
|
+
time.strftime("%Y-%m-%d %H:%M:%S.%9N Z")
|
433
|
+
else
|
434
|
+
time.strftime("%Y-%m-%d %H:%M:%S.%9N %:z")
|
435
|
+
end
|
436
|
+
end
|
437
|
+
end
|
438
|
+
|
439
|
+
# FIXME: remove this method once "to_yaml_properties" is removed
|
440
|
+
def find_ivars target
|
441
|
+
begin
|
442
|
+
loc = target.method(:to_yaml_properties).source_location.first
|
443
|
+
unless loc.start_with?(Psych::DEPRECATED) || loc.end_with?('rubytypes.rb')
|
444
|
+
if $VERBOSE
|
445
|
+
warn "#{loc}: to_yaml_properties is deprecated, please implement \"encode_with(coder)\""
|
446
|
+
end
|
447
|
+
return target.to_yaml_properties
|
448
|
+
end
|
449
|
+
rescue
|
450
|
+
# public_method or source_location might be overridden,
|
451
|
+
# and it's OK to skip it since it's only to emit a warning.
|
452
|
+
end
|
453
|
+
|
454
|
+
target.instance_variables
|
455
|
+
end
|
456
|
+
|
457
|
+
def register target, yaml_obj
|
458
|
+
@st.register target, yaml_obj
|
459
|
+
yaml_obj
|
460
|
+
end
|
461
|
+
|
462
|
+
def dump_coder o
|
463
|
+
@coders << o
|
464
|
+
tag = Psych.dump_tags[o.class]
|
465
|
+
unless tag
|
466
|
+
klass = o.class == Object ? nil : o.class.name
|
467
|
+
tag = ['!ruby/object', klass].compact.join(':')
|
468
|
+
end
|
469
|
+
|
470
|
+
c = Psych::Coder.new(tag)
|
471
|
+
o.encode_with(c)
|
472
|
+
emit_coder c
|
473
|
+
end
|
474
|
+
|
475
|
+
def emit_coder c
|
476
|
+
case c.type
|
477
|
+
when :scalar
|
478
|
+
@emitter.scalar c.scalar, nil, c.tag, c.tag.nil?, false, Nodes::Scalar::ANY
|
479
|
+
when :seq
|
480
|
+
@emitter.start_sequence nil, c.tag, c.tag.nil?, Nodes::Sequence::BLOCK
|
481
|
+
c.seq.each do |thing|
|
482
|
+
accept thing
|
483
|
+
end
|
484
|
+
@emitter.end_sequence
|
485
|
+
when :map
|
486
|
+
@emitter.start_mapping nil, c.tag, c.implicit, c.style
|
487
|
+
c.map.each do |k,v|
|
488
|
+
accept k
|
489
|
+
accept v
|
490
|
+
end
|
491
|
+
@emitter.end_mapping
|
492
|
+
when :object
|
493
|
+
accept c.object
|
494
|
+
end
|
495
|
+
end
|
496
|
+
|
497
|
+
def dump_ivars target
|
498
|
+
ivars = find_ivars target
|
499
|
+
|
500
|
+
ivars.each do |iv|
|
501
|
+
@emitter.scalar("#{iv.to_s.sub(/^@/, '')}", nil, nil, true, false, Nodes::Scalar::ANY)
|
502
|
+
accept target.instance_variable_get(iv)
|
503
|
+
end
|
504
|
+
end
|
505
|
+
end
|
506
|
+
end
|
507
|
+
end
|