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

more descriptive NameError's messages for class attributes

This commit is contained in:
Hrvoje Šimić 2013-04-17 16:12:21 +02:00
parent 46287d6ada
commit 4037fc83c1
2 changed files with 8 additions and 6 deletions

View file

@ -32,7 +32,7 @@ class Class
def cattr_reader(*syms)
options = syms.extract_options!
syms.each do |sym|
raise NameError.new('invalid attribute name') unless sym =~ /^[_A-Za-z]\w*$/
raise NameError.new("invalid class attribute name: #{sym}") unless sym =~ /^[_A-Za-z]\w*$/
class_eval(<<-EOS, __FILE__, __LINE__ + 1)
unless defined? @@#{sym}
@@#{sym} = nil
@ -93,7 +93,7 @@ class Class
def cattr_writer(*syms)
options = syms.extract_options!
syms.each do |sym|
raise NameError.new('invalid attribute name') unless sym =~ /^[_A-Za-z]\w*$/
raise NameError.new("invalid class attribute name: #{sym}") unless sym =~ /^[_A-Za-z]\w*$/
class_eval(<<-EOS, __FILE__, __LINE__ + 1)
unless defined? @@#{sym}
@@#{sym} = nil

View file

@ -44,16 +44,18 @@ class ClassAttributeAccessorTest < ActiveSupport::TestCase
end
def test_should_raise_name_error_if_attribute_name_is_invalid
assert_raises NameError do
exception = assert_raises NameError do
Class.new do
cattr_reader "invalid attribute name"
cattr_reader "1nvalid"
end
end
assert_equal "invalid class attribute name: 1nvalid", exception.message
assert_raises NameError do
exception = assert_raises NameError do
Class.new do
cattr_writer "invalid attribute name"
cattr_writer "1nvalid"
end
end
assert_equal "invalid class attribute name: 1nvalid", exception.message
end
end