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

root/tags/rel_1-2-3/activesupport/lib/active_support/core_ext/class/attribute_accessors.rb

Revision 6108, 1.0 kB (checked in by rick, 2 years ago)

Added :instance_writer option to #mattr_writer/accessor, #cattr_writer/accessor, and #class_inheritable_writer to skip the creation of the instance writer. [Rick]

Line 
1 # Extends the class object with class and instance accessors for class attributes,
2 # just like the native attr* accessors for instance attributes.
3 class Class # :nodoc:
4   def cattr_reader(*syms)
5     syms.flatten.each do |sym|
6       next if sym.is_a?(Hash)
7       class_eval(<<-EOS, __FILE__, __LINE__)
8         unless defined? @@#{sym}
9           @@#{sym} = nil
10         end
11
12         def self.#{sym}
13           @@#{sym}
14         end
15
16         def #{sym}
17           @@#{sym}
18         end
19       EOS
20     end
21   end
22
23   def cattr_writer(*syms)
24     options = syms.last.is_a?(Hash) ? syms.pop : {}
25     syms.flatten.each do |sym|
26       class_eval(<<-EOS, __FILE__, __LINE__)
27         unless defined? @@#{sym}
28           @@#{sym} = nil
29         end
30
31         def self.#{sym}=(obj)
32           @@#{sym} = obj
33         end
34
35         #{"
36         def #{sym}=(obj)
37           @@#{sym} = obj
38         end
39         " unless options[:instance_writer] == false }
40       EOS
41     end
42   end
43
44   def cattr_accessor(*syms)
45     cattr_reader(*syms)
46     cattr_writer(*syms)
47   end
48 end
Note: See TracBrowser for help on using the browser.