1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

visitors can define their own cache strategy for dispatch. fixes #57

This commit is contained in:
Aaron Patterson 2011-06-27 09:11:47 -07:00
parent ec86deadf0
commit 0c8723af70
3 changed files with 31 additions and 4 deletions

View file

@ -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

View file

@ -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

View file

@ -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