Class: Psych::Visitors::YAMLTree

Inherits:
Visitor show all
Defined in:
lib/psych/visitors/yaml_tree.rb,
psych_yaml_tree.c

Overview

YAMLTree builds a YAML ast given a ruby object. For example:

builder = Psych::Visitors::YAMLTree.new
builder << { :foo => 'bar' }
builder.tree # => #<Psych::Nodes::Stream .. }

Direct Known Subclasses

Stream, JSONTree

Defined Under Namespace

Classes: Registrar

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}, emitter = TreeBuilder.new, ss = ScalarScanner.new) ⇒ YAMLTree

Returns a new instance of YAMLTree.



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/psych/visitors/yaml_tree.rb', line 39

def initialize options = {}, emitter = TreeBuilder.new, ss = ScalarScanner.new
  super()
  @started  = false
  @finished = false
  @emitter  = emitter
  @st       = Registrar.new
  @ss       = ss
  @options  = options
  @coders   = []

  @dispatch_cache = Hash.new do |h,klass|
    method = "visit_#{(klass.name || '').split('::').join('_')}"

    method = respond_to?(method) ? method : h[klass.superclass]

    raise(TypeError, "Can't dump #{target.class}") unless method

    h[klass] = method
  end
end

Instance Attribute Details

#finishedObject (readonly) Also known as: finished?

Returns the value of attribute finished



35
36
37
# File 'lib/psych/visitors/yaml_tree.rb', line 35

def finished
  @finished
end

#startedObject (readonly) Also known as: started?

Returns the value of attribute started



35
36
37
# File 'lib/psych/visitors/yaml_tree.rb', line 35

def started
  @started
end

Instance Method Details

#accept(target) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/psych/visitors/yaml_tree.rb', line 97

def accept target
  # return any aliases we find
  if @st.key? target
    oid         = @st.id_for target
    node        = @st.node_for target
    anchor      = oid.to_s
    node.anchor = anchor
    return @emitter.alias anchor
  end

  if target.respond_to?(:to_yaml)
    begin
      loc = target.method(:to_yaml).source_location.first
      if loc !~ /(syck\/rubytypes.rb|psych\/core_ext.rb)/
        unless target.respond_to?(:encode_with)
          if $VERBOSE
            warn "implementing to_yaml is deprecated, please implement \"encode_with\""
          end

          target.to_yaml(:nodump => true)
        end
      end
    rescue
      # public_method or source_location might be overridden,
      # and it's OK to skip it since it's only to emit a warning
    end
  end

  if target.respond_to?(:encode_with)
    dump_coder target
  else
    send(@dispatch_cache[target.class], target)
  end
end

#finishObject



66
67
68
69
70
# File 'lib/psych/visitors/yaml_tree.rb', line 66

def finish
  @emitter.end_stream.tap do
    @finished = true
  end
end

#push(object) ⇒ Object Also known as: <<



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/psych/visitors/yaml_tree.rb', line 77

def push object
  start unless started?
  version = []
  version = [1,1] if @options[:header]

  case @options[:version]
  when Array
    version = @options[:version]
  when String
    version = @options[:version].split('.').map { |x| x.to_i }
  else
    version = [1,1]
  end if @options.key? :version

  @emitter.start_document version, [], false
  accept object
  @emitter.end_document !@emitter.streaming?
end

#start(encoding = Nodes::Stream::UTF8) ⇒ Object



60
61
62
63
64
# File 'lib/psych/visitors/yaml_tree.rb', line 60

def start encoding = Nodes::Stream::UTF8
  @emitter.start_stream(encoding).tap do
    @started = true
  end
end

#treeObject



72
73
74
75
# File 'lib/psych/visitors/yaml_tree.rb', line 72

def tree
  finish unless finished?
  @emitter.root
end

#visit_Array(o) ⇒ Object



341
342
343
344
345
346
347
348
349
# File 'lib/psych/visitors/yaml_tree.rb', line 341

def visit_Array o
  if o.class == ::Array
    register o, @emitter.start_sequence(nil, nil, true, Nodes::Sequence::BLOCK)
    o.each { |c| accept c }
    @emitter.end_sequence
  else
    visit_array_subclass o
  end
end

#visit_BigDecimal(o) ⇒ Object



243
244
245
# File 'lib/psych/visitors/yaml_tree.rb', line 243

def visit_BigDecimal o
  @emitter.scalar o._dump, nil, '!ruby/object:BigDecimal', false, false, Nodes::Scalar::ANY
end

#visit_Class(o) ⇒ Object

Raises:

  • (TypeError)


303
304
305
306
# File 'lib/psych/visitors/yaml_tree.rb', line 303

def visit_Class o
  raise TypeError, "can't dump anonymous class: #{o}" unless o.name
  register o, @emitter.scalar(o.name, nil, '!ruby/class', false, false, Nodes::Scalar::SINGLE_QUOTED)
end

#visit_Complex(o) ⇒ Object



215
216
217
218
219
220
221
222
223
# File 'lib/psych/visitors/yaml_tree.rb', line 215

def visit_Complex o
  register o, @emitter.start_mapping(nil, '!ruby/object:Complex', false, Nodes::Mapping::BLOCK)

  ['real', o.real.to_s, 'image', o.imag.to_s].each do |m|
    @emitter.scalar m, nil, nil, true, false, Nodes::Scalar::ANY
  end

  @emitter.end_mapping
end

#visit_DateTime(o) ⇒ Object



191
192
193
194
195
# File 'lib/psych/visitors/yaml_tree.rb', line 191

def visit_DateTime o
  formatted = format_time o.to_time
  tag = '!ruby/object:DateTime'
  register o, @emitter.scalar(formatted, nil, tag, false, false, Nodes::Scalar::ANY)
end

#visit_Exception(o) ⇒ Object



168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/psych/visitors/yaml_tree.rb', line 168

def visit_Exception o
  tag = ['!ruby/exception', o.class.name].join ':'

  @emitter.start_mapping nil, tag, false, Nodes::Mapping::BLOCK

  {
    'message'   => private_iv_get(o, 'mesg'),
    'backtrace' => private_iv_get(o, 'backtrace'),
  }.each do |k,v|
    next unless v
    @emitter.scalar k, nil, nil, true, false, Nodes::Scalar::ANY
    accept v
  end

  dump_ivars o

  @emitter.end_mapping
end

#visit_Float(o) ⇒ Object



232
233
234
235
236
237
238
239
240
241
# File 'lib/psych/visitors/yaml_tree.rb', line 232

def visit_Float o
  if o.nan?
    @emitter.scalar '.nan', nil, nil, true, false, Nodes::Scalar::ANY
  elsif o.infinite?
    @emitter.scalar((o.infinite? > 0 ? '.inf' : '-.inf'),
      nil, nil, true, false, Nodes::Scalar::ANY)
  else
    @emitter.scalar o.to_s, nil, nil, true, false, Nodes::Scalar::ANY
  end
end

#visit_Hash(o) ⇒ Object



316
317
318
319
320
321
322
323
324
325
326
327
328
# File 'lib/psych/visitors/yaml_tree.rb', line 316

def visit_Hash o
  tag      = o.class == ::Hash ? nil : "!ruby/hash:#{o.class}"
  implicit = !tag

  register(o, @emitter.start_mapping(nil, tag, implicit, Psych::Nodes::Mapping::BLOCK))

  o.each do |k,v|
    accept k
    accept v
  end

  @emitter.end_mapping
end

#visit_Integer(o) ⇒ Object Also known as: visit_TrueClass, visit_FalseClass, visit_Date



225
226
227
# File 'lib/psych/visitors/yaml_tree.rb', line 225

def visit_Integer o
  @emitter.scalar o.to_s, nil, nil, true, false, Nodes::Scalar::ANY
end

#visit_Module(o) ⇒ Object

Raises:

  • (TypeError)


298
299
300
301
# File 'lib/psych/visitors/yaml_tree.rb', line 298

def visit_Module o
  raise TypeError, "can't dump anonymous module: #{o}" unless o.name
  register o, @emitter.scalar(o.name, nil, '!ruby/module', false, false, Nodes::Scalar::SINGLE_QUOTED)
end

#visit_NilClass(o) ⇒ Object



351
352
353
# File 'lib/psych/visitors/yaml_tree.rb', line 351

def visit_NilClass o
  @emitter.scalar('', nil, 'tag:yaml.org,2002:null', true, false, Nodes::Scalar::ANY)
end

#visit_Object(o) ⇒ Object



140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/psych/visitors/yaml_tree.rb', line 140

def visit_Object o
  tag = Psych.dump_tags[o.class]
  unless tag
    klass = o.class == Object ? nil : o.class.name
    tag   = ['!ruby/object', klass].compact.join(':')
  end

  map = @emitter.start_mapping(nil, tag, false, Nodes::Mapping::BLOCK)
  register(o, map)

  dump_ivars o
  @emitter.end_mapping
end

#visit_Psych_Omap(o) ⇒ Object



132
133
134
135
136
137
138
# File 'lib/psych/visitors/yaml_tree.rb', line 132

def visit_Psych_Omap o
  seq = @emitter.start_sequence(nil, '!omap', false, Nodes::Sequence::BLOCK)
  register(o, seq)

  o.each { |k,v| visit_Hash k => v }
  @emitter.end_sequence
end

#visit_Psych_Set(o) ⇒ Object



330
331
332
333
334
335
336
337
338
339
# File 'lib/psych/visitors/yaml_tree.rb', line 330

def visit_Psych_Set o
  register(o, @emitter.start_mapping(nil, '!set', false, Psych::Nodes::Mapping::BLOCK))

  o.each do |k,v|
    accept k
    accept v
  end

  @emitter.end_mapping
end

#visit_Range(o) ⇒ Object



308
309
310
311
312
313
314
# File 'lib/psych/visitors/yaml_tree.rb', line 308

def visit_Range o
  register o, @emitter.start_mapping(nil, '!ruby/range', false, Nodes::Mapping::BLOCK)
  ['begin', o.begin, 'end', o.end, 'excl', o.exclude_end?].each do |m|
    accept m
  end
  @emitter.end_mapping
end

#visit_Rational(o) ⇒ Object



202
203
204
205
206
207
208
209
210
211
212
213
# File 'lib/psych/visitors/yaml_tree.rb', line 202

def visit_Rational o
  register o, @emitter.start_mapping(nil, '!ruby/object:Rational', false, Nodes::Mapping::BLOCK)

  [
    'denominator', o.denominator.to_s,
    'numerator', o.numerator.to_s
  ].each do |m|
    @emitter.scalar m, nil, nil, true, false, Nodes::Scalar::ANY
  end

  @emitter.end_mapping
end

#visit_Regexp(o) ⇒ Object



187
188
189
# File 'lib/psych/visitors/yaml_tree.rb', line 187

def visit_Regexp o
  register o, @emitter.scalar(o.inspect, nil, '!ruby/regexp', false, false, Nodes::Scalar::ANY)
end

#visit_String(o) ⇒ Object



255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
# File 'lib/psych/visitors/yaml_tree.rb', line 255

def visit_String o
  plain = true
  quote = true
  style = Nodes::Scalar::PLAIN
  tag   = nil
  str   = o

  if binary?(o)
    str   = [o].pack('m').chomp
    tag   = '!binary' # FIXME: change to below when syck is removed
    #tag   = 'tag:yaml.org,2002:binary'
    style = Nodes::Scalar::LITERAL
    plain = false
    quote = false
  elsif o =~ /\n/
    style = Nodes::Scalar::LITERAL
  else
    unless String === @ss.tokenize(o)
      style = Nodes::Scalar::SINGLE_QUOTED
    end
  end

  ivars = find_ivars o

  if ivars.empty?
    unless o.class == ::String
      tag = "!ruby/string:#{o.class}"
    end
    @emitter.scalar str, nil, tag, plain, quote, style
  else
    maptag = '!ruby/string'
    maptag << ":#{o.class}" unless o.class == ::String

    register o, @emitter.start_mapping(nil, maptag, false, Nodes::Mapping::BLOCK)
    @emitter.scalar 'str', nil, nil, true, false, Nodes::Scalar::ANY
    @emitter.scalar str, nil, tag, plain, quote, style

    dump_ivars o

    @emitter.end_mapping
  end
end

#visit_Struct(o) ⇒ Object



154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/psych/visitors/yaml_tree.rb', line 154

def visit_Struct o
  tag = ['!ruby/struct', o.class.name].compact.join(':')

  register o, @emitter.start_mapping(nil, tag, false, Nodes::Mapping::BLOCK)
  o.members.each do |member|
    @emitter.scalar member.to_s, nil, nil, true, false, Nodes::Scalar::ANY
    accept o[member]
  end

  dump_ivars o

  @emitter.end_mapping
end

#visit_Symbol(o) ⇒ Object



355
356
357
# File 'lib/psych/visitors/yaml_tree.rb', line 355

def visit_Symbol o
  @emitter.scalar ":#{o}", nil, nil, true, false, Nodes::Scalar::ANY
end

#visit_Time(o) ⇒ Object



197
198
199
200
# File 'lib/psych/visitors/yaml_tree.rb', line 197

def visit_Time o
  formatted = format_time o
  @emitter.scalar formatted, nil, nil, true, false, Nodes::Scalar::ANY
end