diff options
author | Vijay Dev <vijaydev.cse@gmail.com> | 2011-06-20 00:36:52 +0530 |
---|---|---|
committer | Vijay Dev <vijaydev.cse@gmail.com> | 2011-06-20 00:36:52 +0530 |
commit | 427b07baa16ac4f61e0762e52e716fe9937b498a (patch) | |
tree | 430d16ef7376696b582ecd6224a4a03903b7e6b6 | |
parent | d0c4fd75f2b4dee42552ef3148422561acb7fca1 (diff) | |
download | rails-427b07baa16ac4f61e0762e52e716fe9937b498a.tar.gz rails-427b07baa16ac4f61e0762e52e716fe9937b498a.tar.bz2 rails-427b07baa16ac4f61e0762e52e716fe9937b498a.zip |
document the instance_reader option for class_attribute
-rw-r--r-- | railties/guides/source/active_support_core_extensions.textile | 18 |
1 files changed, 15 insertions, 3 deletions
diff --git a/railties/guides/source/active_support_core_extensions.textile b/railties/guides/source/active_support_core_extensions.textile index 221d20fee6..7f33cc4df5 100644 --- a/railties/guides/source/active_support_core_extensions.textile +++ b/railties/guides/source/active_support_core_extensions.textile @@ -947,7 +947,7 @@ h4. Class Attributes h5. +class_attribute+ -The method +class_attribute+ declares one or more inheritable class attributes that can be overridden at any level down the hierarchy: +The method +class_attribute+ declares one or more inheritable class attributes that can be overridden at any level down the hierarchy. <ruby> class A @@ -983,7 +983,7 @@ self.default_params = { }.freeze </ruby> -They can be also accessed and overridden at the instance level: +They can be also accessed and overridden at the instance level. <ruby> A.x = 1 @@ -996,7 +996,7 @@ a1.x # => 1, comes from A a2.x # => 2, overridden in a2 </ruby> -The generation of the writer instance method can be prevented by setting the option +:instance_writer+ to false, as in +The generation of the writer instance method can be prevented by setting the option +:instance_writer+ to +false+. <ruby> module ActiveRecord @@ -1009,8 +1009,20 @@ end A model may find that option useful as a way to prevent mass-assignment from setting the attribute. +The generation of the reader instance method can be prevented by setting the option +:instance_reader+ to +false+. + +<ruby> +class A + class_attribute :x, :instance_reader => false +end + +A.x = 1 # NoMethodError +</ruby> + For convenience +class_attribute+ also defines an instance predicate which is the double negation of what the instance reader returns. In the examples above it would be called +x?+. +When +:instance_reader+ is +false+, the instance predicate returns a +NoMethodError+ just like the reader method. + NOTE: Defined in +active_support/core_ext/class/attribute.rb+ h5. +cattr_reader+, +cattr_writer+, and +cattr_accessor+ |