From e7920a3bde47f1f788fc8d6b8cb00861a78047f8 Mon Sep 17 00:00:00 2001 From: Neeraj Singh Date: Tue, 27 Jul 2010 16:30:56 -0400 Subject: clarifying description for class_attribute method --- activesupport/lib/active_support/core_ext/class/attribute.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'activesupport/lib/active_support/core_ext/class/attribute.rb') diff --git a/activesupport/lib/active_support/core_ext/class/attribute.rb b/activesupport/lib/active_support/core_ext/class/attribute.rb index 576366e496..83b2695606 100644 --- a/activesupport/lib/active_support/core_ext/class/attribute.rb +++ b/activesupport/lib/active_support/core_ext/class/attribute.rb @@ -2,8 +2,8 @@ require 'active_support/core_ext/kernel/singleton_class' require 'active_support/core_ext/module/remove_method' class Class - # Declare a class-level attribute whose value is inheritable and - # overwritable by subclasses: + # Declare a class-level attribute whose value is inheritable by subclasses. + # Subclasses can change their own value and it will not impact parent class. # # class Base # class_attribute :setting -- cgit v1.2.3 From f3b50b11c7cba1ae21888c755cbeabf3b3aca072 Mon Sep 17 00:00:00 2001 From: Neeraj Singh Date: Wed, 28 Jul 2010 09:53:24 -0400 Subject: updating description of how class_attribute works --- activesupport/lib/active_support/core_ext/class/attribute.rb | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'activesupport/lib/active_support/core_ext/class/attribute.rb') diff --git a/activesupport/lib/active_support/core_ext/class/attribute.rb b/activesupport/lib/active_support/core_ext/class/attribute.rb index 83b2695606..cee3f04354 100644 --- a/activesupport/lib/active_support/core_ext/class/attribute.rb +++ b/activesupport/lib/active_support/core_ext/class/attribute.rb @@ -18,6 +18,11 @@ class Class # Subclass.setting # => false # Base.setting # => true # + # In the above case as long as Subclass does not assign a value to setting + # by performing Subclass.setting = _something_ , Subclass.setting + # would read value assigned to parent class. Once Subclass assigns a value then + # the value assigned by Subclass would be returned. + # # This matches normal Ruby method inheritance: think of writing an attribute # on a subclass as overriding the reader method. # -- cgit v1.2.3 From eb902d6c00f64e7060f24fa3b243d5b6dea98cac Mon Sep 17 00:00:00 2001 From: Neeraj Singh Date: Wed, 28 Jul 2010 10:08:00 -0400 Subject: clarifying the instance_write option with an example --- activesupport/lib/active_support/core_ext/class/attribute.rb | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'activesupport/lib/active_support/core_ext/class/attribute.rb') diff --git a/activesupport/lib/active_support/core_ext/class/attribute.rb b/activesupport/lib/active_support/core_ext/class/attribute.rb index cee3f04354..af8202ae2c 100644 --- a/activesupport/lib/active_support/core_ext/class/attribute.rb +++ b/activesupport/lib/active_support/core_ext/class/attribute.rb @@ -41,7 +41,11 @@ class Class # # To opt out of the instance writer method, pass :instance_writer => false. # - # object.setting = false # => NoMethodError + # class Base + # class_attribute :setting, :instance_write => false + # end + # + # Base.new.setting = false # => NoMethodError def class_attribute(*attrs) instance_writer = !attrs.last.is_a?(Hash) || attrs.pop[:instance_writer] -- cgit v1.2.3 From 7c3d479c4b8ae5667ead1163d4ce713c5ae73629 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Wed, 28 Jul 2010 08:49:54 -0700 Subject: More documentation to class_attribute. --- .../lib/active_support/core_ext/class/attribute.rb | 35 +++++++++++++++------- 1 file changed, 24 insertions(+), 11 deletions(-) (limited to 'activesupport/lib/active_support/core_ext/class/attribute.rb') diff --git a/activesupport/lib/active_support/core_ext/class/attribute.rb b/activesupport/lib/active_support/core_ext/class/attribute.rb index af8202ae2c..bfa57fe1f7 100644 --- a/activesupport/lib/active_support/core_ext/class/attribute.rb +++ b/activesupport/lib/active_support/core_ext/class/attribute.rb @@ -18,17 +18,34 @@ class Class # Subclass.setting # => false # Base.setting # => true # - # In the above case as long as Subclass does not assign a value to setting - # by performing Subclass.setting = _something_ , Subclass.setting - # would read value assigned to parent class. Once Subclass assigns a value then - # the value assigned by Subclass would be returned. + # In the above case as long as Subclass does not assign a value to setting + # by performing Subclass.setting = _something_ , Subclass.setting + # would read value assigned to parent class. Once Subclass assigns a value then + # the value assigned by Subclass would be returned. # # This matches normal Ruby method inheritance: think of writing an attribute - # on a subclass as overriding the reader method. + # on a subclass as overriding the reader method. However, you need to be aware + # when using +class_attribute+ with mutable structures as +Array+ or +Hash+. + # In such cases, you don't want to do changes in places but use setters: + # + # Base.setting = [] + # Base.setting #=> [] + # Subclass.setting #=> [] + # + # # Appending in child changes both parent and child because it is the same object: + # Subclass.setting << :foo + # Base.setting #=> [:foo] + # Subclass.setting #=> [:foo] + # + # # Use setters to not propagate changes: + # Base.setting = [] + # Subclass.setting += [:foo] + # Base.setting #=> [] + # Subclass.setting #=> [:foo] # # For convenience, a query method is defined as well: # - # Subclass.setting? # => false + # Subclass.setting? # => false # # Instances may overwrite the class value in the same way: # @@ -41,11 +58,7 @@ class Class # # To opt out of the instance writer method, pass :instance_writer => false. # - # class Base - # class_attribute :setting, :instance_write => false - # end - # - # Base.new.setting = false # => NoMethodError + # object.setting = false # => NoMethodError def class_attribute(*attrs) instance_writer = !attrs.last.is_a?(Hash) || attrs.pop[:instance_writer] -- cgit v1.2.3