Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

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  
    55    m = @module = Module.new do 
    66      mattr_accessor :foo 
    77      mattr_accessor :bar, :instance_writer => false 
     8      mattr_accessor :first, :second 
    89    end 
    910    @class = Class.new 
    1011    @class.instance_eval { include m } 
    1112    @object = @class.new 
    1213  end 
    1314 
    14   def test_should_use_mattr_default 
     15  def test_should_use_mattr_default_of_nil 
    1516    assert_nil @module.foo 
    1617    assert_nil @object.foo 
    1718  end 
     
    3031    assert @object.respond_to?(:bar) 
    3132    assert !@object.respond_to?(:bar=) 
    3233  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 
    3351end 
  • activesupport/test/core_ext/class/attribute_accessor_test.rb

    old new  
    55    @class = Class.new do 
    66      cattr_accessor :foo 
    77      cattr_accessor :bar, :instance_writer => false 
     8      cattr_accessor :first, :second 
    89    end 
    910    @object = @class.new 
    1011  end 
    11    
    12   def test_should_use_mattr_default 
     12 
     13  def test_should_use_cattr_default_of_nil 
    1314    assert_nil @class.foo 
    1415    assert_nil @object.foo 
    1516  end 
    16    
    17   def test_should_set_mattr_value 
     17 
     18  def test_should_set_cattr_value 
    1819    @class.foo = :test 
    1920    assert_equal :test, @object.foo 
    20    
     21 
    2122    @object.foo = :test2 
    2223    assert_equal :test2, @class.foo 
    2324  end 
    24    
     25 
    2526  def test_should_not_create_instance_writer 
    2627    assert @class.respond_to?(:foo) 
    2728    assert @class.respond_to?(:foo=) 
    2829    assert @object.respond_to?(:bar) 
    2930    assert !@object.respond_to?(:bar=) 
    3031  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 
    3144end 
  • activesupport/lib/active_support/core_ext/module/attribute_accessors.rb

    old new  
    11# Extends the module object with module and instance accessors for class attributes,  
    22# just like the native attr* accessors for instance attributes. 
    3 class Module # :nodoc: 
     3class 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). 
    432  def mattr_reader(*syms) 
    533    syms.each do |sym| 
    634      next if sym.is_a?(Hash) 
     
    836        unless defined? @@#{sym} 
    937          @@#{sym} = nil 
    1038        end 
    11          
     39 
    1240        def self.#{sym} 
    1341          @@#{sym} 
    1442        end 
     
    1947      EOS 
    2048    end 
    2149  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). 
    2382  def mattr_writer(*syms) 
    2483    options = syms.extract_options! 
    2584    syms.each do |sym| 
     
    2786        unless defined? @@#{sym} 
    2887          @@#{sym} = nil 
    2988        end 
    30          
     89 
    3190        def self.#{sym}=(obj) 
    3291          @@#{sym} = obj 
    3392        end 
    34          
     93 
    3594        #{" 
    3695        def #{sym}=(obj) 
    3796          @@#{sym} = obj 
     
    4099      EOS 
    41100    end 
    42101  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. 
    44108  def mattr_accessor(*syms) 
    45109    mattr_reader(*syms) 
    46110    mattr_writer(*syms) 
  • activesupport/lib/active_support/core_ext/class/attribute_accessors.rb

    old new  
    11# Extends the class object with class and instance accessors for class attributes, 
    22# just like the native attr* accessors for instance attributes. 
    3 class Class # :nodoc: 
     3class 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). 
    424  def cattr_reader(*syms) 
    5     syms.flatten.each do |sym| 
     25    syms.each do |sym| 
    626      next if sym.is_a?(Hash) 
    727      class_eval(<<-EOS, __FILE__, __LINE__) 
    828        unless defined? @@#{sym} 
     
    2040    end 
    2141  end 
    2242 
     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). 
    2366  def cattr_writer(*syms) 
    2467    options = syms.extract_options! 
    25     syms.flatten.each do |sym| 
     68    syms.each do |sym| 
    2669      class_eval(<<-EOS, __FILE__, __LINE__) 
    2770        unless defined? @@#{sym} 
    2871          @@#{sym} = nil 
     
    4184    end 
    4285  end 
    4386 
     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. 
    4492  def cattr_accessor(*syms) 
    4593    cattr_reader(*syms) 
    4694    cattr_writer(*syms)