Skip to content
This repository
Browse code

hash lookup and delegate to a method

  • Loading branch information...
commit 5fd82a0c6df91d901082c9734ce36ed2263e2777 1 parent 4be2aef
Aaron Patterson authored October 16, 2012
4  lib/psych/scalar_scanner.rb
@@ -38,9 +38,9 @@ def tokenize string
38 38
           string
39 39
         when '~', /^null$/i
40 40
           nil
41  
-        when /^(yes|true|on)$/i
  41
+        when /^(?:yes|true|on)$/i
42 42
           true
43  
-        when /^(no|false|off)$/i
  43
+        when /^(?:no|false|off)$/i
44 44
           false
45 45
         else
46 46
           @string_cache[string] = true
41  lib/psych/visitors/to_ruby.rb
@@ -141,6 +141,12 @@ def visit_Psych_Nodes_Mapping o
141 141
         return revive_hash({}, o) unless o.tag
142 142
 
143 143
         case o.tag
  144
+        when %r{
  145
+          ^!ruby/object:(?:Complex|Rational)$ |
  146
+          ^!ruby/object
  147
+        }x
  148
+          send METHODS[$&], o, $~
  149
+
144 150
         when /^!(?:str|ruby\/string)(?::(.*))?/, 'tag:yaml.org,2002:str'
145 151
           klass = resolve_class($1)
146 152
           members = Hash[*o.children.map { |c| accept c }]
@@ -206,19 +212,6 @@ def visit_Psych_Nodes_Mapping o
206 212
           end
207 213
           set
208 214
 
209  
-        when '!ruby/object:Complex'
210  
-          h = Hash[*o.children.map { |c| accept c }]
211  
-          register o, Complex(h['real'], h['image'])
212  
-
213  
-        when '!ruby/object:Rational'
214  
-          h = Hash[*o.children.map { |c| accept c }]
215  
-          register o, Rational(h['numerator'], h['denominator'])
216  
-
217  
-        when /^!ruby\/object:?(.*)?$/
218  
-          name = $1 || 'Object'
219  
-          obj = revive((resolve_class(name) || Object), o)
220  
-          obj
221  
-
222 215
         when /^!map:(.*)$/, /^!ruby\/hash:(.*)$/
223 216
           revive_hash resolve_class($1).new, o
224 217
 
@@ -247,6 +240,28 @@ def visit_Psych_Nodes_Alias o
247 240
       end
248 241
 
249 242
       private
  243
+
  244
+      METHODS = {
  245
+        '!ruby/object:Complex'  => :complex,
  246
+        '!ruby/object:Rational' => :rational,
  247
+        '!ruby/object'          => :object,
  248
+      }
  249
+
  250
+      def complex o, match
  251
+        h = Hash[*o.children.map { |c| accept c }]
  252
+        register o, Complex(h['real'], h['image'])
  253
+      end
  254
+
  255
+      def rational o, match
  256
+        h = Hash[*o.children.map { |c| accept c }]
  257
+        register o, Rational(h['numerator'], h['denominator'])
  258
+      end
  259
+
  260
+      def object o, match
  261
+        name = match.string.split(':', 2)[1] || 'Object'
  262
+        revive((resolve_class(name) || Object), o)
  263
+      end
  264
+
250 265
       def register node, object
251 266
         @st[node.anchor] = object if node.anchor
252 267
         object

0 notes on commit 5fd82a0

Please sign in to comment.
Something went wrong with that request. Please try again.