From 9b854ccd77498e5c0f30912596737f0b8efa4654 Mon Sep 17 00:00:00 2001 From: Rob Zolkos Date: Sun, 6 May 2012 21:04:02 +1000 Subject: Add missing public method doc to TimeWithZone.name --- activesupport/lib/active_support/time_with_zone.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'activesupport/lib/active_support') diff --git a/activesupport/lib/active_support/time_with_zone.rb b/activesupport/lib/active_support/time_with_zone.rb index 1cb71012ef..9d89e425fc 100644 --- a/activesupport/lib/active_support/time_with_zone.rb +++ b/activesupport/lib/active_support/time_with_zone.rb @@ -35,8 +35,10 @@ module ActiveSupport # t.is_a?(ActiveSupport::TimeWithZone) # => true # class TimeWithZone + + # Report class name as 'Time' to thwart type checking def self.name - 'Time' # Report class name as 'Time' to thwart type checking + 'Time' end include Comparable -- cgit v1.2.3 From 17ef794299ae10c2a7b8ce7a2da68258267c9720 Mon Sep 17 00:00:00 2001 From: Francesco Rodriguez Date: Mon, 7 May 2012 11:28:03 -0500 Subject: adding example about using cattr_accessor with subclasses --- .../lib/active_support/core_ext/class/attribute_accessors.rb | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'activesupport/lib/active_support') 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 5cb528cfe9..4f8866ce9d 100644 --- a/activesupport/lib/active_support/core_ext/class/attribute_accessors.rb +++ b/activesupport/lib/active_support/core_ext/class/attribute_accessors.rb @@ -15,6 +15,14 @@ require 'active_support/core_ext/array/extract_options' # Person.hair_colors # => [:brown, :black, :blonde, :red] # Person.new.hair_colors # => [:brown, :black, :blonde, :red] # +# class Female < Person +# end +# +# Female.hair_colors << :pink +# Female.hair_colors # => [:brown, :black, :blonde, :red, :pink] +# Female.new.hair_colors # => [:brown, :black, :blonde, :red, :pink] +# Person.hair_colors # => [:brown, :black, :blonde, :red, :pink] +# # To opt out of the instance writer method, pass :instance_writer => false. # To opt out of the instance reader method, pass :instance_reader => false. # To opt out of both instance methods, pass :instance_accessor => false. -- cgit v1.2.3 From 7b487e5dc16bcf7f94c031cc1411f940df8c0fc8 Mon Sep 17 00:00:00 2001 From: Francesco Rodriguez Date: Mon, 7 May 2012 12:39:41 -0500 Subject: added docs to cattr_accessor method --- .../core_ext/class/attribute_accessors.rb | 62 +++++++++++++++++----- 1 file changed, 50 insertions(+), 12 deletions(-) (limited to 'activesupport/lib/active_support') 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 4f8866ce9d..72a918f839 100644 --- a/activesupport/lib/active_support/core_ext/class/attribute_accessors.rb +++ b/activesupport/lib/active_support/core_ext/class/attribute_accessors.rb @@ -7,21 +7,21 @@ require 'active_support/core_ext/array/extract_options' # 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 +# 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] +# Person.hair_colors = [:brown, :black, :blonde, :red] +# Person.hair_colors # => [:brown, :black, :blonde, :red] +# Person.new.hair_colors # => [:brown, :black, :blonde, :red] # -# class Female < Person -# end +# class Female < Person +# end # -# Female.hair_colors << :pink -# Female.hair_colors # => [:brown, :black, :blonde, :red, :pink] -# Female.new.hair_colors # => [:brown, :black, :blonde, :red, :pink] -# Person.hair_colors # => [:brown, :black, :blonde, :red, :pink] +# Female.hair_colors << :pink +# Female.hair_colors # => [:brown, :black, :blonde, :red, :pink] +# Female.new.hair_colors # => [:brown, :black, :blonde, :red, :pink] +# Person.hair_colors # => [:brown, :black, :blonde, :red, :pink] # # To opt out of the instance writer method, pass :instance_writer => false. # To opt out of the instance reader method, pass :instance_reader => false. @@ -83,6 +83,44 @@ class Class end end + # Defines class and instance accessors for class attributes. + # + # 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] + # + # 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 Male < Person + # end + # + # Male.hair_colors << :blue + # Person.hair_colors # => [:brown, :black, :blonde, :red, :blue] + # + # 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 + # + # Or pass :instance_accessor => false, to opt out both instance methods. + # + # class Person + # cattr_accessor :hair_colors, :instance_accessor => false + # end + # + # Person.new.hair_colors = [:brown] # => NoMethodError + # Person.new.hair_colors # => NoMethodError def cattr_accessor(*syms, &blk) cattr_reader(*syms) cattr_writer(*syms, &blk) -- cgit v1.2.3 From 2fb6d12ad3a03c1d9b310fc6a01150f1af2af73b Mon Sep 17 00:00:00 2001 From: Francesco Rodriguez Date: Mon, 7 May 2012 17:33:54 -0500 Subject: added docs and examples to cattr_writer method --- .../core_ext/class/attribute_accessors.rb | 46 +++++++++++++++++++--- 1 file changed, 41 insertions(+), 5 deletions(-) (limited to 'activesupport/lib/active_support') 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 72a918f839..7d85fd512c 100644 --- a/activesupport/lib/active_support/core_ext/class/attribute_accessors.rb +++ b/activesupport/lib/active_support/core_ext/class/attribute_accessors.rb @@ -58,6 +58,42 @@ class Class end end + # Defines a class attribute if it's not defined and creates a writer method to allow + # assignment to the attribute. + # + # class Person + # cattr_writer :hair_colors + # end + # + # Person.hair_colors = [:brown, :black] + # Person.class_variable_get("@@hair_colors") # => [:brown, :black] + # Person.new.hair_colors = [:blonde, :red] + # Person.class_variable_get("@@hair_colors") # => [:blonde, :red] + # + # The attribute name must be any word character starting with a letter or underscore + # and without spaces. + # + # class Person + # cattr_writer :"1_Badname " + # end + # # => NameError: invalid attribute name + # + # If you want to opt out the instance writer method, pass instance_writer: false + # or instance_accessor: false. + # + # class Person + # cattr_writer :hair_colors, instance_writer: false + # end + # + # Person.new.hair_colors = [:blonde, :red] # => NoMethodError + # + # Also, you can pass a block to set up the variable with a default value. + # + # class Person + # cattr_writer(:hair_colors) {[:brown, :black, :blonde, :red]} + # end + # + # Person.class_variable_get("@@hair_colors") # => [:brown, :black, :blonde, :red] def cattr_writer(*syms) options = syms.extract_options! syms.each do |sym| @@ -103,20 +139,20 @@ class Class # Male.hair_colors << :blue # Person.hair_colors # => [:brown, :black, :blonde, :red, :blue] # - # To opt out of the instance writer method, pass :instance_writer => false. - # To opt out of the instance reader method, pass :instance_reader => false. + # 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 + # cattr_accessor :hair_colors, instance_writer: false, instance_reader: false # end # # Person.new.hair_colors = [:brown] # => NoMethodError # Person.new.hair_colors # => NoMethodError # - # Or pass :instance_accessor => false, to opt out both instance methods. + # Or pass instance_accessor: false, to opt out both instance methods. # # class Person - # cattr_accessor :hair_colors, :instance_accessor => false + # cattr_accessor :hair_colors, instance_accessor: false # end # # Person.new.hair_colors = [:brown] # => NoMethodError -- cgit v1.2.3 From 1ff35304885d86800d802748a053e0f4bef2ac91 Mon Sep 17 00:00:00 2001 From: Francesco Rodriguez Date: Mon, 7 May 2012 19:04:27 -0500 Subject: better docs for cattr_accessor and cattr_writer --- .../active_support/core_ext/class/attribute_accessors.rb | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) (limited to 'activesupport/lib/active_support') 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 7d85fd512c..4461cd6608 100644 --- a/activesupport/lib/active_support/core_ext/class/attribute_accessors.rb +++ b/activesupport/lib/active_support/core_ext/class/attribute_accessors.rb @@ -87,10 +87,12 @@ class Class # # Person.new.hair_colors = [:blonde, :red] # => NoMethodError # - # Also, you can pass a block to set up the variable with a default value. + # Also, you can pass a block to set up the attribute with a default value. # # class Person - # cattr_writer(:hair_colors) {[:brown, :black, :blonde, :red]} + # cattr_writer :hair_colors do + # [:brown, :black, :blonde, :red] + # end # end # # Person.class_variable_get("@@hair_colors") # => [:brown, :black, :blonde, :red] @@ -157,6 +159,16 @@ class Class # # Person.new.hair_colors = [:brown] # => NoMethodError # Person.new.hair_colors # => NoMethodError + # + # Also you can pass a block to set up the attribute with a default value. + # + # class Person + # cattr_accessor :hair_colors do + # [:brown, :black, :blonde, :red] + # end + # end + # + # Person.class_variable_get("@@hair_colors") #=> [:brown, :black, :blonde, :red] def cattr_accessor(*syms, &blk) cattr_reader(*syms) cattr_writer(*syms, &blk) -- cgit v1.2.3 From 2805c28e3e9a7386fe144754a9b664c424add2b3 Mon Sep 17 00:00:00 2001 From: Francesco Rodriguez Date: Mon, 7 May 2012 19:37:00 -0500 Subject: added docs to cattr_reader --- .../core_ext/class/attribute_accessors.rb | 27 ++++++++++++++++++++++ 1 file changed, 27 insertions(+) (limited to 'activesupport/lib/active_support') 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 4461cd6608..8b41b10c32 100644 --- a/activesupport/lib/active_support/core_ext/class/attribute_accessors.rb +++ b/activesupport/lib/active_support/core_ext/class/attribute_accessors.rb @@ -34,6 +34,33 @@ require 'active_support/core_ext/array/extract_options' # Person.new.hair_colors = [:brown] # => NoMethodError # Person.new.hair_colors # => NoMethodError class Class + # Defines a class attribute if it's not defined and creates a reader method that + # returns the attribute value. + # + # class Person + # cattr_reader :hair_colors + # end + # + # Person.class_variable_set("@@hair_colors", [:brown, :black]) + # Person.hair_colors # => [:brown, :black] + # Person.new.hair_colors # => [:brown, :black] + # + # The attribute name must be any word character starting with a letter or underscore + # and without spaces. + # + # class Person + # cattr_reader :"1_Badname " + # end + # # => NameError: invalid attribute name + # + # If you want to opt out the instance writer method, pass instance_reader: false + # or instance_accessor: false. + # + # class Person + # cattr_reader :hair_colors, instance_reader: false + # end + # + # Person.new.hair_colors # => NoMethodError def cattr_reader(*syms) options = syms.extract_options! syms.each do |sym| -- cgit v1.2.3 From f31613a0312bfb2529d0ac262337cd7338cb868d Mon Sep 17 00:00:00 2001 From: Vijay Dev Date: Tue, 8 May 2012 23:49:20 +0530 Subject: cut some duplication and minor edits [ci skip] --- .../core_ext/class/attribute_accessors.rb | 41 +++------------------- 1 file changed, 4 insertions(+), 37 deletions(-) (limited to 'activesupport/lib/active_support') 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 8b41b10c32..fa1dbfdf06 100644 --- a/activesupport/lib/active_support/core_ext/class/attribute_accessors.rb +++ b/activesupport/lib/active_support/core_ext/class/attribute_accessors.rb @@ -2,37 +2,6 @@ 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 Female < Person -# end -# -# Female.hair_colors << :pink -# Female.hair_colors # => [:brown, :black, :blonde, :red, :pink] -# Female.new.hair_colors # => [:brown, :black, :blonde, :red, :pink] -# Person.hair_colors # => [:brown, :black, :blonde, :red, :pink] -# -# To opt out of the instance writer method, pass :instance_writer => false. -# To opt out of the instance reader method, pass :instance_reader => false. -# To opt out of both instance methods, pass :instance_accessor => 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 # Defines a class attribute if it's not defined and creates a reader method that # returns the attribute value. @@ -45,15 +14,14 @@ class Class # Person.hair_colors # => [:brown, :black] # Person.new.hair_colors # => [:brown, :black] # - # The attribute name must be any word character starting with a letter or underscore - # and without spaces. + # The attribute name must be a valid method name in Ruby. # # class Person # cattr_reader :"1_Badname " # end # # => NameError: invalid attribute name # - # If you want to opt out the instance writer method, pass instance_reader: false + # If you want to opt out the instance reader method, you can pass instance_reader: false # or instance_accessor: false. # # class Person @@ -97,8 +65,7 @@ class Class # Person.new.hair_colors = [:blonde, :red] # Person.class_variable_get("@@hair_colors") # => [:blonde, :red] # - # The attribute name must be any word character starting with a letter or underscore - # and without spaces. + # The attribute name must be a valid method name in Ruby. # # class Person # cattr_writer :"1_Badname " @@ -148,7 +115,7 @@ class Class end end - # Defines class and instance accessors for class attributes. + # Defines both class and instance accessors for class attributes. # # class Person # cattr_accessor :hair_colors -- cgit v1.2.3