Fewer object allocations

This commit is contained in:
Jeremy Kemper 2010-08-30 14:08:10 -07:00
parent cb67d16629
commit d79b1aa0ba
2 changed files with 9 additions and 11 deletions

@ -317,7 +317,7 @@ def instance_method_already_implemented?(method_name)
private
class AttributeMethodMatcher
attr_reader :prefix, :suffix
attr_reader :prefix, :suffix, :method_missing_target
AttributeMethodMatch = Struct.new(:target, :attr_name)
@ -325,22 +325,20 @@ def initialize(options = {})
options.symbolize_keys!
@prefix, @suffix = options[:prefix] || '', options[:suffix] || ''
@regex = /^(#{Regexp.escape(@prefix)})(.+?)(#{Regexp.escape(@suffix)})$/
@method_missing_target = :"#{@prefix}attribute#{@suffix}"
@method_name = "#{prefix}%s#{suffix}"
end
def match(method_name)
if matchdata = @regex.match(method_name)
AttributeMethodMatch.new(method_missing_target, matchdata[2])
if @regex =~ method_name
AttributeMethodMatch.new(method_missing_target, $2)
else
nil
end
end
def method_name(attr_name)
"#{prefix}#{attr_name}#{suffix}"
end
def method_missing_target
:"#{prefix}attribute#{suffix}"
@method_name % attr_name
end
end
end

@ -541,7 +541,7 @@ def test_read_attributes_respect_access_control
topic = @target.new(:title => "The pros and cons of programming naked.")
assert !topic.respond_to?(:title)
exception = assert_raise(NoMethodError) { topic.title }
assert_equal "Attempt to call private method", exception.message
assert_match %r(^Attempt to call private method), exception.message
assert_equal "I'm private", topic.send(:title)
end
@ -551,7 +551,7 @@ def test_write_attributes_respect_access_control
topic = @target.new
assert !topic.respond_to?(:title=)
exception = assert_raise(NoMethodError) { topic.title = "Pants"}
assert_equal "Attempt to call private method", exception.message
assert_match %r(^Attempt to call private method), exception.message
topic.send(:title=, "Very large pants")
end
@ -561,7 +561,7 @@ def test_question_attributes_respect_access_control
topic = @target.new(:title => "Isaac Newton's pants")
assert !topic.respond_to?(:title?)
exception = assert_raise(NoMethodError) { topic.title? }
assert_equal "Attempt to call private method", exception.message
assert_match %r(^Attempt to call private method), exception.message
assert topic.send(:title?)
end