Class: Psych::Visitors::ToRuby

Inherits:
Visitor show all
Defined in:
lib/psych/visitors/to_ruby.rb,
ext/psych/to_ruby.c

Overview

This class walks a YAML AST, converting each node to ruby

Instance Method Summary collapse

Constructor Details

#initializeToRuby

Returns a new instance of ToRuby.



8
9
10
11
12
13
# File 'lib/psych/visitors/to_ruby.rb', line 8

def initialize
  super
  @st = {}
  @ss = ScalarScanner.new
  @domain_types = Psych.domain_types
end

Instance Method Details

#accept(target) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/psych/visitors/to_ruby.rb', line 15

def accept target
  result = super
  return result if @domain_types.empty? || !target.tag

  key = target.tag.sub(/^[!\/]*/, '').sub(/(,\d+)\//, '\1:')
  key = "tag:#{key}" unless key =~ /^(tag:|x-private)/

  if @domain_types.key? key
    value, block = @domain_types[key]
    return block.call value, result
  end

  result
end

#visit_Psych_Nodes_Alias(o) ⇒ Object



219
220
221
# File 'lib/psych/visitors/to_ruby.rb', line 219

def visit_Psych_Nodes_Alias o
  @st[o.anchor]
end

#visit_Psych_Nodes_Document(o) ⇒ Object



211
212
213
# File 'lib/psych/visitors/to_ruby.rb', line 211

def visit_Psych_Nodes_Document o
  accept o.root
end

#visit_Psych_Nodes_Mapping(o) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/psych/visitors/to_ruby.rb', line 119

def visit_Psych_Nodes_Mapping o
  return revive(Psych.load_tags[o.tag], o) if Psych.load_tags[o.tag]

  case o.tag
  when '!str', 'tag:yaml.org,2002:str'
    members = Hash[*o.children.map { |c| accept c }]
    string = members.delete 'str'
    init_with(string, members.map { |k,v| [k.to_s.sub(/^@/, ''),v] }, o)
  when /^!ruby\/struct:?(.*)?$/
    klass = resolve_class($1)

    if klass
      s = klass.allocate
      @st[o.anchor] = s if o.anchor

      members = {}
      struct_members = s.members.map { |x| x.to_sym }
      o.children.each_slice(2) do |k,v|
        member = accept(k)
        value  = accept(v)
        if struct_members.include?(member.to_sym)
          s.send("#{member}=", value)
        else
          members[member.to_s.sub(/^@/, '')] = value
        end
      end
      init_with(s, members, o)
    else
      members = o.children.map { |c| accept c }
      h = Hash[*members]
      Struct.new(*h.map { |k,v| k.to_sym }).new(*h.map { |k,v| v })
    end

  when '!ruby/range'
    h = Hash[*o.children.map { |c| accept c }]
    Range.new(h['begin'], h['end'], h['excl'])

  when /^!ruby\/exception:?(.*)?$/
    h = Hash[*o.children.map { |c| accept c }]

    e = build_exception((resolve_class($1) || Exception),
                        h.delete('message'))
    init_with(e, h, o)

  when '!set', 'tag:yaml.org,2002:set'
    set = Psych::Set.new
    @st[o.anchor] = set if o.anchor
    o.children.each_slice(2) do |k,v|
      set[accept(k)] = accept(v)
    end
    set

  when '!ruby/object:Complex'
    h = Hash[*o.children.map { |c| accept c }]
    Complex(h['real'], h['image'])

  when '!ruby/object:Rational'
    h = Hash[*o.children.map { |c| accept c }]
    Rational(h['numerator'], h['denominator'])

  when /^!ruby\/object:?(.*)?$/
    name = $1 || 'Object'
    obj = revive((resolve_class(name) || Object), o)
    @st[o.anchor] = obj if o.anchor
    obj
  else
    hash = {}
    @st[o.anchor] = hash if o.anchor

    o.children.each_slice(2) { |k,v|
      key = accept(k)

      if key == '<<'
        case v
        when Nodes::Alias
          hash.merge! accept(v)
        when Nodes::Sequence
          accept(v).reverse_each do |value|
            hash.merge! value
          end
        else
          hash[key] = accept(v)
        end
      else
        hash[key] = accept(v)
      end

    }
    hash
  end
end

#visit_Psych_Nodes_Scalar(o) ⇒ Object



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
# File 'lib/psych/visitors/to_ruby.rb', line 30

def visit_Psych_Nodes_Scalar o
  @st[o.anchor] = o.value if o.anchor

  if klass = Psych.load_tags[o.tag]
    instance = klass.allocate

    if instance.respond_to?(:init_with)
      coder = Psych::Coder.new(o.tag)
      coder.scalar = o.value
      instance.init_with coder
    end

    return instance
  end

  return o.value if o.quoted
  return @ss.tokenize(o.value) unless o.tag

  case o.tag
  when '!binary', 'tag:yaml.org,2002:binary'
    o.value.unpack('m').first
  when '!str', 'tag:yaml.org,2002:str'
    o.value
  when "!ruby/object:DateTime"
    require 'date'
    @ss.parse_time(o.value).to_datetime
  when "!ruby/object:Complex"
    Complex(o.value)
  when "!ruby/object:Rational"
    Rational(o.value)
  when "tag:yaml.org,2002:float", "!float"
    Float(@ss.tokenize(o.value))
  when "!ruby/regexp"
    o.value =~ /^\/(.*)\/([mixn]*)$/
    source  = $1
    options = 0
    lang    = nil
    ($2 || '').split('').each do |option|
      case option
      when 'x' then options |= Regexp::EXTENDED
      when 'i' then options |= Regexp::IGNORECASE
      when 'm' then options |= Regexp::MULTILINE
      when 'n' then options |= Regexp::NOENCODING
      else lang = option
      end
    end
    Regexp.new(*[source, options, lang].compact)
  when "!ruby/range"
    args = o.value.split(/([.]{2,3})/, 2).map { |s|
      accept Nodes::Scalar.new(s)
    }
    args.push(args.delete_at(1) == '...')
    Range.new(*args)
  when /^!ruby\/sym(bol)?:?(.*)?$/
    o.value.to_sym
  else
    @ss.tokenize o.value
  end
end

#visit_Psych_Nodes_Sequence(o) ⇒ Object



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
# File 'lib/psych/visitors/to_ruby.rb', line 90

def visit_Psych_Nodes_Sequence o
  if klass = Psych.load_tags[o.tag]
    instance = klass.allocate

    if instance.respond_to?(:init_with)
      coder = Psych::Coder.new(o.tag)
      coder.seq = o.children.map { |c| accept c }
      instance.init_with coder
    end

    return instance
  end

  case o.tag
  when '!omap', 'tag:yaml.org,2002:omap'
    map = Psych::Omap.new
    @st[o.anchor] = map if o.anchor
    o.children.each { |a|
      map[accept(a.children.first)] = accept a.children.last
    }
    map
  else
    list = []
    @st[o.anchor] = list if o.anchor
    o.children.each { |c| list.push accept c }
    list
  end
end

#visit_Psych_Nodes_Stream(o) ⇒ Object



215
216
217
# File 'lib/psych/visitors/to_ruby.rb', line 215

def visit_Psych_Nodes_Stream o
  o.children.map { |c| accept c }
end