diff options
author | Aaron Patterson <aaron.patterson@gmail.com> | 2009-12-22 16:19:51 -0800 |
---|---|---|
committer | Aaron Patterson <aaron.patterson@gmail.com> | 2009-12-22 16:19:51 -0800 |
commit | 2733fe0c9db438036c061f92ae3070503e88d14a (patch) | |
tree | 3b52e028d3fda0f9a70e6731dd5718d107ac5331 | |
parent | 94567441ca6355d3550d46ec9ad3f558e6735e05 (diff) | |
download | psych-2733fe0c9db438036c061f92ae3070503e88d14a.zip |
refactoring dispatch table
-rw-r--r-- | lib/psych/visitors/to_ruby.rb | 19 | ||||
-rw-r--r-- | lib/psych/visitors/visitor.rb | 10 |
2 files changed, 15 insertions, 14 deletions
diff --git a/lib/psych/visitors/to_ruby.rb b/lib/psych/visitors/to_ruby.rb index a197ee2..e4c7e2f 100644 --- a/lib/psych/visitors/to_ruby.rb +++ b/lib/psych/visitors/to_ruby.rb @@ -5,18 +5,13 @@ module Psych ### # This class walks a YAML AST, converting each node to ruby class ToRuby < Psych::Visitors::Visitor - YAML_NODE_DISPATCH_TABLE = Hash[ - *Nodes.constants.map { |k| - [Nodes.const_get(k), :"visit_#{k}"] - }.flatten - ] def initialize super @st = {} end def accept target - result = send(YAML_NODE_DISPATCH_TABLE[target.class], target) + result = super return result if Psych.domain_types.empty? if target.respond_to?(:tag) && target.tag @@ -29,7 +24,7 @@ module Psych result end - def visit_Scalar o + def visit_Psych_Nodes_Scalar o @st[o.anchor] = o.value if o.anchor return o.value if o.quoted @@ -71,7 +66,7 @@ module Psych end end - def visit_Sequence o + def visit_Psych_Nodes_Sequence o case o.tag when '!omap', 'tag:yaml.org,2002:omap' map = Psych::Omap.new @@ -88,7 +83,7 @@ module Psych end end - def visit_Mapping o + def visit_Psych_Nodes_Mapping o case o.tag when '!str', 'tag:yaml.org,2002:str' members = Hash[*o.children.map { |c| accept c }] @@ -169,15 +164,15 @@ module Psych end end - def visit_Document o + def visit_Psych_Nodes_Document o accept o.root end - def visit_Stream o + def visit_Psych_Nodes_Stream o o.children.map { |c| accept c } end - def visit_Alias o + def visit_Psych_Nodes_Alias o @st[o.anchor] end diff --git a/lib/psych/visitors/visitor.rb b/lib/psych/visitors/visitor.rb index ccbd0d0..56be391 100644 --- a/lib/psych/visitors/visitor.rb +++ b/lib/psych/visitors/visitor.rb @@ -1,9 +1,15 @@ module Psych module Visitors class Visitor + YAML_NODE_DISPATCH_TABLE = Hash[ + *Nodes.constants.map { |k| + k = Nodes.const_get k + [k, :"visit_#{k.name.split('::').join('_')}"] + }.flatten + ] + def accept target - method_name = target.class.name.split('::').join('_') - send(:"visit_#{method_name}", target) + send(YAML_NODE_DISPATCH_TABLE[target.class], target) end end end |