mirror of
https://github.com/github/rails.git
synced 2026-04-04 03:00:58 -04:00
Add method punctuation handling to #synchronize
This commit is contained in:
@@ -19,13 +19,15 @@ class Module
|
||||
end
|
||||
|
||||
methods.each do |method|
|
||||
if instance_methods.include?("#{method}_without_synchronization")
|
||||
aliased_method, punctuation = method.to_s.sub(/([?!=])$/, ''), $1
|
||||
|
||||
if instance_methods.include?("#{aliased_method}_without_synchronization#{punctuation}")
|
||||
raise ArgumentError, "#{method} is already synchronized. Double synchronization is not currently supported."
|
||||
end
|
||||
module_eval(<<-EOS, __FILE__, __LINE__)
|
||||
def #{method}_with_synchronization(*args, &block)
|
||||
def #{aliased_method}_with_synchronization#{punctuation}(*args, &block)
|
||||
#{with}.synchronize do
|
||||
#{method}_without_synchronization(*args,&block)
|
||||
#{aliased_method}_without_synchronization#{punctuation}(*args,&block)
|
||||
end
|
||||
end
|
||||
EOS
|
||||
|
||||
@@ -54,4 +54,18 @@ class SynchronizationTest < Test::Unit::TestCase
|
||||
@instance.to_s
|
||||
assert_equal 2, dummy.sync_count
|
||||
end
|
||||
|
||||
def test_can_synchronize_method_with_punctuation
|
||||
@target.module_eval do
|
||||
def dangerous?
|
||||
@dangerous
|
||||
end
|
||||
def dangerous!
|
||||
@dangerous = true
|
||||
end
|
||||
end
|
||||
@target.synchronize :dangerous?, :dangerous!, :with => :mutex
|
||||
@instance.dangerous!
|
||||
assert @instance.dangerous?
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user