Added instance_accessor: false as an option to Class#cattr_accessor and friends [DHH]

上级 276618c6
*Rails 3.2.0 (unreleased)*
* Added instance_accessor: false as an option to Class#cattr_accessor and friends [DHH]
* Removed ActiveSupport::SecureRandom in favour of SecureRandom from the standard library [Jon Leighton]
* ActiveSupport::OrderedHash now has different behavior for #each and
......
......@@ -17,6 +17,7 @@
#
# To opt out of the instance writer method, pass :instance_writer => false.
# To opt out of the instance reader method, pass :instance_reader => false.
# To opt out of both instance methods, pass :instance_accessor => false.
#
# class Person
# cattr_accessor :hair_colors, :instance_writer => false, :instance_reader => false
......@@ -38,7 +39,7 @@ def self.#{sym}
end
EOS
unless options[:instance_reader] == false
unless options[:instance_reader] == false || options[:instance_accessor] == false
class_eval(<<-EOS, __FILE__, __LINE__ + 1)
def #{sym}
@@#{sym}
......@@ -61,7 +62,7 @@ def self.#{sym}=(obj)
end
EOS
unless options[:instance_writer] == false
unless options[:instance_writer] == false || options[:instance_accessor] == false
class_eval(<<-EOS, __FILE__, __LINE__ + 1)
def #{sym}=(obj)
@@#{sym} = obj
......
......@@ -5,8 +5,9 @@ class ClassAttributeAccessorTest < Test::Unit::TestCase
def setup
@class = Class.new do
cattr_accessor :foo
cattr_accessor :bar, :instance_writer => false
cattr_reader :shaq, :instance_reader => false
cattr_accessor :bar, :instance_writer => false
cattr_reader :shaq, :instance_reader => false
cattr_accessor :camp, :instance_accessor => false
end
@object = @class.new
end
......@@ -35,4 +36,10 @@ def test_should_not_create_instance_reader
assert_respond_to @class, :shaq
assert !@object.respond_to?(:shaq)
end
def test_should_not_create_instance_accessors
assert_respond_to @class, :camp
assert !@object.respond_to?(:camp)
assert !@object.respond_to?(:camp=)
end
end
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册