diff options
author | Aaron Patterson <aaron.patterson@gmail.com> | 2014-04-08 17:01:53 -0700 |
---|---|---|
committer | Aaron Patterson <aaron.patterson@gmail.com> | 2014-04-08 17:01:53 -0700 |
commit | a6a7c75ff486657909e20e2f48764136caa5e87e (patch) | |
tree | 9fce597cf0536bf94cdeeffe837091083de85fbf /lib/arel/visitors/reduce.rb | |
parent | 6ae60fd2cbbf836dac01f6d2d56c38fb9deb225a (diff) | |
download | rails-a6a7c75ff486657909e20e2f48764136caa5e87e.tar.gz rails-a6a7c75ff486657909e20e2f48764136caa5e87e.tar.bz2 rails-a6a7c75ff486657909e20e2f48764136caa5e87e.zip |
push reduction visitors to a reduction base class
this lets our old depth first and dot visitors to work normally
Diffstat (limited to 'lib/arel/visitors/reduce.rb')
-rw-r--r-- | lib/arel/visitors/reduce.rb | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/lib/arel/visitors/reduce.rb b/lib/arel/visitors/reduce.rb new file mode 100644 index 0000000000..9670cad27c --- /dev/null +++ b/lib/arel/visitors/reduce.rb @@ -0,0 +1,25 @@ +require 'arel/visitors/visitor' + +module Arel + module Visitors + class Reduce < Arel::Visitors::Visitor + def accept object, collector + visit object, collector + end + + private + + def visit object, collector + send dispatch[object.class], object, collector + rescue NoMethodError => e + raise e if respond_to?(dispatch[object.class], true) + superklass = object.class.ancestors.find { |klass| + respond_to?(dispatch[klass], true) + } + raise(TypeError, "Cannot visit #{object.class}") unless superklass + dispatch[object.class] = dispatch[superklass] + retry + end + end + end +end |