Ticket #10822: document_attribute_accessors.diff
| File document_attribute_accessors.diff, 8.1 kB (added by chuyeow, 9 months ago) |
|---|
-
activesupport/test/core_ext/module/attribute_accessor_test.rb
old new 5 5 m = @module = Module.new do 6 6 mattr_accessor :foo 7 7 mattr_accessor :bar, :instance_writer => false 8 mattr_accessor :first, :second 8 9 end 9 10 @class = Class.new 10 11 @class.instance_eval { include m } 11 12 @object = @class.new 12 13 end 13 14 14 def test_should_use_mattr_default 15 def test_should_use_mattr_default_of_nil 15 16 assert_nil @module.foo 16 17 assert_nil @object.foo 17 18 end … … 30 31 assert @object.respond_to?(:bar) 31 32 assert !@object.respond_to?(:bar=) 32 33 end 34 35 def test_should_work_with_multiple_args 36 assert @module.respond_to?(:first) 37 assert @module.respond_to?(:first=) 38 assert @object.respond_to?(:first) 39 assert @object.respond_to?(:first=) 40 41 assert @module.respond_to?(:second=) 42 assert @module.respond_to?(:second) 43 assert @object.respond_to?(:second=) 44 assert @object.respond_to?(:second) 45 end 46 47 def test_should_not_create_class_accessors 48 assert !@class.respond_to?(:foo) 49 assert !@class.respond_to?(:foo=) 50 end 33 51 end -
activesupport/test/core_ext/class/attribute_accessor_test.rb
old new 5 5 @class = Class.new do 6 6 cattr_accessor :foo 7 7 cattr_accessor :bar, :instance_writer => false 8 cattr_accessor :first, :second 8 9 end 9 10 @object = @class.new 10 11 end 11 12 def test_should_use_ mattr_default12 13 def test_should_use_cattr_default_of_nil 13 14 assert_nil @class.foo 14 15 assert_nil @object.foo 15 16 end 16 17 def test_should_set_ mattr_value17 18 def test_should_set_cattr_value 18 19 @class.foo = :test 19 20 assert_equal :test, @object.foo 20 21 21 22 @object.foo = :test2 22 23 assert_equal :test2, @class.foo 23 24 end 24 25 25 26 def test_should_not_create_instance_writer 26 27 assert @class.respond_to?(:foo) 27 28 assert @class.respond_to?(:foo=) 28 29 assert @object.respond_to?(:bar) 29 30 assert !@object.respond_to?(:bar=) 30 31 end 32 33 def test_should_work_with_multiple_args 34 assert @class.respond_to?(:first) 35 assert @class.respond_to?(:first=) 36 assert @object.respond_to?(:first) 37 assert @object.respond_to?(:first=) 38 39 assert @class.respond_to?(:second=) 40 assert @class.respond_to?(:second) 41 assert @object.respond_to?(:second=) 42 assert @object.respond_to?(:second) 43 end 31 44 end -
activesupport/lib/active_support/core_ext/module/attribute_accessors.rb
old new 1 1 # Extends the module object with module and instance accessors for class attributes, 2 2 # just like the native attr* accessors for instance attributes. 3 class Module # :nodoc: 3 class Module 4 # Creates class attributes and corresponding module and instance methods that return 5 # the value of each class attribute. 6 # 7 # ==== Example 8 # module Mod 9 # mattr_reader :size 10 # end 11 # class Foo 12 # include Mod 13 # end 14 # 15 # is equivalent to: 16 # 17 # module Mod 18 # def self.size 19 # @@size 20 # end 21 # def size 22 # @@size 23 # end 24 # end 25 # class Foo 26 # def size 27 # @@size 28 # end 29 # end 30 # 31 # The <code>@@size</code> class variable is set to <code>nil</code> (unless it is already defined). 4 32 def mattr_reader(*syms) 5 33 syms.each do |sym| 6 34 next if sym.is_a?(Hash) … … 8 36 unless defined? @@#{sym} 9 37 @@#{sym} = nil 10 38 end 11 39 12 40 def self.#{sym} 13 41 @@#{sym} 14 42 end … … 19 47 EOS 20 48 end 21 49 end 22 50 51 # Creates module attributes and corresponding module and instance methods that allow 52 # assignment to each class attribute. 53 # 54 # ==== Options 55 # * <tt>:instance_writer</tt> - Set to false if you don't want the instance method to be defined. Defaults to true. 56 # 57 # ==== Example 58 # module Mod 59 # mattr_writer :size 60 # end 61 # class Foo 62 # include Mod 63 # end 64 # 65 # is equivalent to: 66 # 67 # module Mod 68 # def self.size=(val) 69 # @@size = val 70 # end 71 # def size=(val) 72 # @@size = val 73 # end 74 # end 75 # class Foo 76 # def size=(val) 77 # @@size = val 78 # end 79 # end 80 # 81 # The <code>@@size</code> class variable is set to <code>nil</code> (unless it is already defined). 23 82 def mattr_writer(*syms) 24 83 options = syms.extract_options! 25 84 syms.each do |sym| … … 27 86 unless defined? @@#{sym} 28 87 @@#{sym} = nil 29 88 end 30 89 31 90 def self.#{sym}=(obj) 32 91 @@#{sym} = obj 33 92 end 34 93 35 94 #{" 36 95 def #{sym}=(obj) 37 96 @@#{sym} = obj … … 40 99 EOS 41 100 end 42 101 end 43 102 103 # Creates module attributes and corresponding class and instance accessor methods for 104 # reading and writing each class attribute. 105 # 106 # This is equivalent to calling <code>mattr_reader</code> and <code>mattr_accessor</code> 107 # with the same arguments. 44 108 def mattr_accessor(*syms) 45 109 mattr_reader(*syms) 46 110 mattr_writer(*syms) -
activesupport/lib/active_support/core_ext/class/attribute_accessors.rb
old new 1 1 # Extends the class object with class and instance accessors for class attributes, 2 2 # just like the native attr* accessors for instance attributes. 3 class Class # :nodoc: 3 class Class 4 # Creates class attributes and corresponding class and instance methods that return 5 # the value of each class attribute. 6 # 7 # ==== Example 8 # class Foo 9 # cattr_reader :size 10 # end 11 # 12 # is equivalent to: 13 # 14 # class Foo 15 # def self.size 16 # @@size 17 # end 18 # def size 19 # @@size 20 # end 21 # end 22 # 23 # The <code>@@size</code> class variable is set to <code>nil</code> (unless it is already defined). 4 24 def cattr_reader(*syms) 5 syms. flatten.each do |sym|25 syms.each do |sym| 6 26 next if sym.is_a?(Hash) 7 27 class_eval(<<-EOS, __FILE__, __LINE__) 8 28 unless defined? @@#{sym} … … 20 40 end 21 41 end 22 42 43 # Creates class attributes and corresponding class and instance methods that allow 44 # assignment to each class attribute. 45 # 46 # ==== Options 47 # * <tt>:instance_writer</tt> - Set to false if you don't want the instance method to be defined. Defaults to true. 48 # 49 # ==== Example 50 # class Foo 51 # cattr_writer :size 52 # end 53 # 54 # is equivalent to: 55 # 56 # class Foo 57 # def self.size=(val) 58 # @@size = val 59 # end 60 # def size=(val) 61 # @@size = val 62 # end 63 # end 64 # 65 # The <code>@@size</code> class variable is set to <code>nil</code> (unless it is already defined). 23 66 def cattr_writer(*syms) 24 67 options = syms.extract_options! 25 syms. flatten.each do |sym|68 syms.each do |sym| 26 69 class_eval(<<-EOS, __FILE__, __LINE__) 27 70 unless defined? @@#{sym} 28 71 @@#{sym} = nil … … 41 84 end 42 85 end 43 86 87 # Creates class attributes and corresponding class and instance accessor methods for 88 # reading and writing each class attribute. 89 # 90 # This is equivalent to calling <code>cattr_reader</code> and <code>cattr_accessor</code> 91 # with the same arguments. 44 92 def cattr_accessor(*syms) 45 93 cattr_reader(*syms) 46 94 cattr_writer(*syms)