Merged updates from trunk into stable branch
[feedcatcher.git] / vendor / rails / activesupport / lib / active_support / core_ext / module / attribute_accessors.rb
1 # Extends the module object with module and instance accessors for class attributes,
2 # just like the native attr* accessors for instance attributes.
3 #
4 # module AppConfiguration
5 # mattr_accessor :google_api_key
6 # self.google_api_key = "123456789"
7 #
8 # mattr_accessor :paypal_url
9 # self.paypal_url = "www.sandbox.paypal.com"
10 # end
11 #
12 # AppConfiguration.google_api_key = "overriding the api key!"
13 class Module
14 def mattr_reader(*syms)
15 syms.each do |sym|
16 next if sym.is_a?(Hash)
17 class_eval(<<-EOS, __FILE__, __LINE__)
18 unless defined? @@#{sym} # unless defined? @@pagination_options
19 @@#{sym} = nil # @@pagination_options = nil
20 end # end
21 #
22 def self.#{sym} # def self.pagination_options
23 @@#{sym} # @@pagination_options
24 end # end
25 #
26 def #{sym} # def pagination_options
27 @@#{sym} # @@pagination_options
28 end # end
29 EOS
30 end
31 end
32
33 def mattr_writer(*syms)
34 options = syms.extract_options!
35 syms.each do |sym|
36 class_eval(<<-EOS, __FILE__, __LINE__)
37 unless defined? @@#{sym} # unless defined? @@pagination_options
38 @@#{sym} = nil # @@pagination_options = nil
39 end # end
40 #
41 def self.#{sym}=(obj) # def self.pagination_options=(obj)
42 @@#{sym} = obj # @@pagination_options = obj
43 end # end
44 #
45 #{" #
46 def #{sym}=(obj) # def pagination_options=(obj)
47 @@#{sym} = obj # @@pagination_options = obj
48 end # end
49 " unless options[:instance_writer] == false } # # instance writer above is generated unless options[:instance_writer] == false
50 EOS
51 end
52 end
53
54 def mattr_accessor(*syms)
55 mattr_reader(*syms)
56 mattr_writer(*syms)
57 end
58 end