summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAaron Patterson <aaron.patterson@gmail.com>2009-12-22 16:37:52 -0800
committerAaron Patterson <aaron.patterson@gmail.com>2009-12-22 16:37:52 -0800
commit07d75b2cc4723eb895a409a263c9ce4afb557241 (patch)
tree7c783318f726eea6b893cab0eb76a8b8108fd9b6
parent2733fe0c9db438036c061f92ae3070503e88d14a (diff)
downloadpsych-07d75b2cc4723eb895a409a263c9ce4afb557241.zip
caching dispatch table
-rw-r--r--lib/psych/visitors/yaml_tree.rb19
1 files changed, 11 insertions, 8 deletions
diff --git a/lib/psych/visitors/yaml_tree.rb b/lib/psych/visitors/yaml_tree.rb
index 5646b4b..3604ba4 100644
--- a/lib/psych/visitors/yaml_tree.rb
+++ b/lib/psych/visitors/yaml_tree.rb
@@ -9,6 +9,16 @@ module Psych
@tree.children << Nodes::Document.new
@stack = @tree.children.dup
@st = {}
+
+ @dispatch_cache = Hash.new do |h,klass|
+ method = klass.ancestors.map { |ancestor|
+ "visit_#{(ancestor.name || '').split('::').join('_')}"
+ }.find { |sig| respond_to? sig }
+
+ raise(TypeError, "Can't dump #{target.class}") unless method
+
+ h[klass] = method
+ end
end
def accept target
@@ -18,14 +28,7 @@ module Psych
return append Nodes::Alias.new target.object_id.to_s
end
- target.class.ancestors.find do |klass|
- next unless klass.name
- method_name = :"visit_#{klass.name.split('::').join('_')}"
-
- return send(method_name, target) if respond_to?(method_name)
- end
-
- raise TypeError, "Can't dump #{target.class}"
+ send(@dispatch_cache[target.class], target)
end
def visit_Psych_Omap o