From 0c8723af70b8518c1a9ae43e650afb433e078470 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Mon, 27 Jun 2011 09:11:47 -0700 Subject: visitors can define their own cache strategy for dispatch. fixes #57 --- History.txt | 7 +++++++ lib/arel/visitors/visitor.rb | 12 ++++++++---- test/visitors/test_to_sql.rb | 16 ++++++++++++++++ 3 files changed, 31 insertions(+), 4 deletions(-) diff --git a/History.txt b/History.txt index b5c37193ed..4158f5e968 100644 --- a/History.txt +++ b/History.txt @@ -1,3 +1,10 @@ +== 2.1.2 / Unreleased + +* Bug Fixes + + * Visitors can define their own cache strategey so caches are not shared. + Fixes #57 + == 2.1.1 / 2011/05/14 * Bug fixes diff --git a/lib/arel/visitors/visitor.rb b/lib/arel/visitors/visitor.rb index c9cdf34adb..8f9dd929e1 100644 --- a/lib/arel/visitors/visitor.rb +++ b/lib/arel/visitors/visitor.rb @@ -11,15 +11,19 @@ module Arel hash[klass] = "visit_#{(klass.name || '').gsub('::', '_')}" end + def dispatch + DISPATCH + end + def visit object - send DISPATCH[object.class], object + send dispatch[object.class], object rescue NoMethodError => e - raise e if respond_to?(DISPATCH[object.class], true) + raise e if respond_to?(dispatch[object.class], true) superklass = object.class.ancestors.find { |klass| - respond_to?(DISPATCH[klass], true) + respond_to?(dispatch[klass], true) } raise(TypeError, "Cannot visit #{object.class}") unless superklass - DISPATCH[object.class] = DISPATCH[superklass] + dispatch[object.class] = dispatch[superklass] retry end end diff --git a/test/visitors/test_to_sql.rb b/test/visitors/test_to_sql.rb index d046e543e1..b52722ddd6 100644 --- a/test/visitors/test_to_sql.rb +++ b/test/visitors/test_to_sql.rb @@ -13,6 +13,22 @@ module Arel @attr = @table[:id] end + it 'can define a dispatch method' do + visited = false + viz = Class.new(Arel::Visitors::Visitor) { + define_method(:hello) do |node| + visited = true + end + + def dispatch + { Arel::Table => 'hello' } + end + }.new + + viz.accept(@table) + assert visited, 'hello method was called' + end + it "should be thread safe around usage of last_column" do visit_integer_column = Thread.new do Thread.stop -- cgit v1.2.3