Skip to content

Commit

Permalink
Merge pull request #5156 from avakhov/class-attribute-instance-accessor
Browse files Browse the repository at this point in the history
Add instance_accessor option to class_attribute
  • Loading branch information
carlosantoniodasilva committed May 21, 2012
2 parents 776ea10 + e4569b5 commit f260dc5
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 4 deletions.
3 changes: 1 addition & 2 deletions actionpack/lib/abstract_controller/layouts.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -203,8 +203,7 @@ module Layouts
include Rendering include Rendering


included do included do
class_attribute :_layout, :_layout_conditions, class_attribute :_layout, :_layout_conditions, :instance_accessor => false
:instance_reader => false, :instance_writer => false
self._layout = nil self._layout = nil
self._layout_conditions = {} self._layout_conditions = {}
_write_layout_method _write_layout_method
Expand Down
2 changes: 2 additions & 0 deletions activesupport/CHANGELOG.md
Original file line number Original file line Diff line number Diff line change
@@ -1,5 +1,7 @@
## Rails 4.0.0 (unreleased) ## ## 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* * `constantize` now looks in the ancestor chain. *Marc-Andre Lafortune & Andrew White*


* `Object#try` can't call private methods. *Vasiliy Ermolovich* * `Object#try` can't call private methods. *Vasiliy Ermolovich*
Expand Down
6 changes: 4 additions & 2 deletions activesupport/lib/active_support/core_ext/class/attribute.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -65,10 +65,12 @@ class Class
# To opt out of the instance writer method, pass :instance_writer => false. # To opt out of the instance writer method, pass :instance_writer => false.
# #
# object.setting = false # => NoMethodError # object.setting = false # => NoMethodError
#
# To opt out of both instance methods, pass :instance_accessor => false.
def class_attribute(*attrs) def class_attribute(*attrs)
options = attrs.extract_options! options = attrs.extract_options!
instance_reader = options.fetch(:instance_reader, true) instance_reader = options.fetch(:instance_accessor, true) && options.fetch(:instance_reader, true)
instance_writer = options.fetch(:instance_writer, true) instance_writer = options.fetch(:instance_accessor, true) && options.fetch(:instance_writer, true)


attrs.each do |name| attrs.each do |name|
class_eval <<-RUBY, __FILE__, __LINE__ + 1 class_eval <<-RUBY, __FILE__, __LINE__ + 1
Expand Down
7 changes: 7 additions & 0 deletions activesupport/test/core_ext/class/attribute_test.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -66,6 +66,13 @@ def setup
assert_raise(NoMethodError) { object.setting? } assert_raise(NoMethodError) { object.setting? }
end 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 test 'works well with singleton classes' do
object = @klass.new object = @klass.new
object.singleton_class.setting = 'foo' object.singleton_class.setting = 'foo'
Expand Down

0 comments on commit f260dc5

Please sign in to comment.