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

Ticket #10822: document_cattr.diff

File document_cattr.diff, 2.2 kB (added by chuyeow, 9 months ago)
  • 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) 
    525    syms.flatten.each do |sym| 
    626      next if sym.is_a?(Hash) 
     
    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! 
    2568    syms.flatten.each do |sym| 
     
    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)