Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added instance_accessor: false to Module#mattr_accessor

  • Loading branch information...
commit bf526c2dbeb73bf11553004e43889a804b72866d 1 parent 954359b
Vijay Dev vijaydev authored
8 activesupport/lib/active_support/core_ext/module/attribute_accessors.rb
View
@@ -12,7 +12,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}
@@ -31,7 +31,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
@@ -53,6 +53,10 @@ def #{sym}=(obj)
# end
#
# AppConfiguration.google_api_key = "overriding the api key!"
+ #
+ # 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.
def mattr_accessor(*syms)
mattr_reader(*syms)
mattr_writer(*syms)
7 activesupport/test/core_ext/module/attribute_accessor_test.rb
View
@@ -7,6 +7,7 @@ def setup
mattr_accessor :foo
mattr_accessor :bar, :instance_writer => false
mattr_reader :shaq, :instance_reader => false
+ mattr_accessor :camp, :instance_accessor => false
end
@class = Class.new
@class.instance_eval { include m }
@@ -37,4 +38,10 @@ def test_should_not_create_instance_reader
assert_respond_to @module, :shaq
assert !@object.respond_to?(:shaq)
end
+
+ def test_should_not_create_instance_accessors
+ assert_respond_to @module, :camp
+ assert !@object.respond_to?(:camp)
+ assert !@object.respond_to?(:camp=)
+ end
end
Please sign in to comment.
Something went wrong with that request. Please try again.