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') 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') 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 ff7e17d33e8042fa430fd637ef3433ef7367dc30 Mon Sep 17 00:00:00 2001 From: Neeraj Singh Date: Wed, 28 Jul 2010 10:05:10 -0400 Subject: adding some comments to cattr_accessor method --- .../lib/active_support/core_ext/class/attribute_accessors.rb | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'activesupport/lib/active_support/core_ext/class') diff --git a/activesupport/lib/active_support/core_ext/class/attribute_accessors.rb b/activesupport/lib/active_support/core_ext/class/attribute_accessors.rb index feef5d2d57..cdc7cfc1d4 100644 --- a/activesupport/lib/active_support/core_ext/class/attribute_accessors.rb +++ b/activesupport/lib/active_support/core_ext/class/attribute_accessors.rb @@ -3,11 +3,17 @@ require 'active_support/core_ext/array/extract_options' # Extends the class object with class and instance accessors for class attributes, # just like the native attr* accessors for instance attributes. # +# Note that unlike +class_attribute+, if a subclass changes the value then that would +# also change the value for parent class. Similarly if parent class changes the value +# then that would change the value of subclasses too. +# # class Person # cattr_accessor :hair_colors # end # # Person.hair_colors = [:brown, :black, :blonde, :red] +# Person.hair_colors #=> [:brown, :black, :blonde, :red] +# Person.new.hair_colors #=> [:brown, :black, :blonde, :red] class Class def cattr_reader(*syms) options = syms.extract_options! -- 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') 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 5b10e47593ed21fedba2401e3ad1f360e2cf0706 Mon Sep 17 00:00:00 2001 From: Neeraj Singh Date: Wed, 28 Jul 2010 10:13:11 -0400 Subject: adding comment specifying that cattr_accessor also supports instance_write and instance_rader option --- .../lib/active_support/core_ext/class/attribute_accessors.rb | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'activesupport/lib/active_support/core_ext/class') diff --git a/activesupport/lib/active_support/core_ext/class/attribute_accessors.rb b/activesupport/lib/active_support/core_ext/class/attribute_accessors.rb index cdc7cfc1d4..4e35b1b488 100644 --- a/activesupport/lib/active_support/core_ext/class/attribute_accessors.rb +++ b/activesupport/lib/active_support/core_ext/class/attribute_accessors.rb @@ -14,6 +14,16 @@ require 'active_support/core_ext/array/extract_options' # Person.hair_colors = [:brown, :black, :blonde, :red] # Person.hair_colors #=> [:brown, :black, :blonde, :red] # Person.new.hair_colors #=> [:brown, :black, :blonde, :red] +# +# To opt out of the instance writer method, pass :instance_writer => false. +# To opt out of the instance reader method, pass :instance_reader => false. +# +# class Person +# cattr_accessor :hair_colors, :instance_writer => false, :instance_reader => false +# end +# +# Person.new.hair_colors = [:brown] # => NoMethodError +# Person.new.hair_colors # => NoMethodError class Class def cattr_reader(*syms) options = syms.extract_options! -- 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') 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