Permalink
Browse files

Add instance_accessor option to class_attribute

  • Loading branch information...
1 parent 776ea10 commit e4569b50d1d20f52633a3fe036014acd5117fa73 @avakhov avakhov committed Feb 24, 2012
@@ -203,8 +203,7 @@ module Layouts
include Rendering
included do
- class_attribute :_layout, :_layout_conditions,
- :instance_reader => false, :instance_writer => false
+ class_attribute :_layout, :_layout_conditions, :instance_accessor => false
self._layout = nil
self._layout_conditions = {}
_write_layout_method
@@ -1,5 +1,7 @@
## Rails 4.0.0 (unreleased) ##
+* Add `:instance_accessor` option for `class_attribute`. *Alexey Vakhov*
+
* `constantize` now looks in the ancestor chain. *Marc-Andre Lafortune & Andrew White*
* `Object#try` can't call private methods. *Vasiliy Ermolovich*
@@ -65,10 +65,12 @@ class Class
# To opt out of the instance writer method, pass :instance_writer => false.
#
# object.setting = false # => NoMethodError
+ #
+ # To opt out of both instance methods, pass :instance_accessor => false.
def class_attribute(*attrs)
options = attrs.extract_options!
- instance_reader = options.fetch(:instance_reader, true)
- instance_writer = options.fetch(:instance_writer, true)
+ instance_reader = options.fetch(:instance_accessor, true) && options.fetch(:instance_reader, true)
+ instance_writer = options.fetch(:instance_accessor, true) && options.fetch(:instance_writer, true)
attrs.each do |name|
class_eval <<-RUBY, __FILE__, __LINE__ + 1
@@ -66,6 +66,13 @@ def setup
assert_raise(NoMethodError) { object.setting? }
end
+ test 'disabling both instance writer and reader' do
+ object = Class.new { class_attribute :setting, :instance_accessor => false }.new
+ assert_raise(NoMethodError) { object.setting }
+ assert_raise(NoMethodError) { object.setting? }
+ assert_raise(NoMethodError) { object.setting = 'boom' }
+ end
+
test 'works well with singleton classes' do
object = @klass.new
object.singleton_class.setting = 'foo'

0 comments on commit e4569b5

Please sign in to comment.