Skip to content
This repository
Browse code

backport of inconsistency with cattr_reader and matter_reader with so…

…me tweaks

Signed-off-by: wycats <wycats@gmail.com>
  • Loading branch information...
commit bc2af911f9f85191b573975c8421591d6f68c567 1 parent 0f18904
Santiago Pastorino authored March 27, 2010 wycats committed March 28, 2010
59  activesupport/lib/active_support/core_ext/class/attribute_accessors.rb
@@ -8,42 +8,49 @@
8 8
 #  Person.hair_colors = [:brown, :black, :blonde, :red]
9 9
 class Class
10 10
   def cattr_reader(*syms)
  11
+    options = syms.extract_options!
11 12
     syms.each do |sym|
12 13
       next if sym.is_a?(Hash)
13  
-      class_eval(<<-EOS, __FILE__, __LINE__)
14  
-        unless defined? @@#{sym}  # unless defined? @@hair_colors
15  
-          @@#{sym} = nil          #   @@hair_colors = nil
16  
-        end                       # end
17  
-                                  #
18  
-        def self.#{sym}           # def self.hair_colors
19  
-          @@#{sym}                #   @@hair_colors
20  
-        end                       # end
21  
-                                  #
22  
-        def #{sym}                # def hair_colors
23  
-          @@#{sym}                #   @@hair_colors
24  
-        end                       # end
  14
+      class_eval(<<-EOS, __FILE__, __LINE__ + 1)
  15
+        unless defined? @@#{sym}
  16
+          @@#{sym} = nil
  17
+        end
  18
+
  19
+        def self.#{sym}
  20
+          @@#{sym}
  21
+        end
25 22
       EOS
  23
+
  24
+      unless options[:instance_reader] == false
  25
+        class_eval(<<-EOS, __FILE__, __LINE__ + 1)
  26
+          def #{sym}
  27
+            @@#{sym}
  28
+          end
  29
+        EOS
  30
+      end
26 31
     end
27 32
   end
28 33
 
29 34
   def cattr_writer(*syms)
30 35
     options = syms.extract_options!
31 36
     syms.each do |sym|
32  
-      class_eval(<<-EOS, __FILE__, __LINE__)
33  
-        unless defined? @@#{sym}                       # unless defined? @@hair_colors
34  
-          @@#{sym} = nil                               #   @@hair_colors = nil
35  
-        end                                            # end
36  
-                                                       #
37  
-        def self.#{sym}=(obj)                          # def self.hair_colors=(obj)
38  
-          @@#{sym} = obj                               #   @@hair_colors = obj
39  
-        end                                            # end
40  
-                                                       #
41  
-        #{"                                            #
42  
-        def #{sym}=(obj)                               # def hair_colors=(obj)
43  
-          @@#{sym} = obj                               #   @@hair_colors = obj
44  
-        end                                            # end
45  
-        " unless options[:instance_writer] == false }  # # instance writer above is generated unless options[:instance_writer] == false
  37
+      class_eval(<<-EOS, __FILE__, __LINE__ + 1)
  38
+        unless defined? @@#{sym}
  39
+          @@#{sym} = nil
  40
+        end
  41
+
  42
+        def self.#{sym}=(obj)
  43
+          @@#{sym} = obj
  44
+        end
46 45
       EOS
  46
+
  47
+      unless options[:instance_writer] == false
  48
+        class_eval(<<-EOS, __FILE__, __LINE__ + 1)
  49
+          def #{sym}=(obj)
  50
+            @@#{sym} = obj
  51
+          end
  52
+        EOS
  53
+      end
47 54
     end
48 55
   end
49 56
 
59  activesupport/lib/active_support/core_ext/module/attribute_accessors.rb
@@ -14,42 +14,49 @@
14 14
 #  AppConfiguration.google_api_key = "overriding the api key!"
15 15
 class Module
16 16
   def mattr_reader(*syms)
  17
+    options = syms.extract_options!
17 18
     syms.each do |sym|
18 19
       next if sym.is_a?(Hash)
19  
-      class_eval(<<-EOS, __FILE__, __LINE__)
20  
-        unless defined? @@#{sym}  # unless defined? @@pagination_options
21  
-          @@#{sym} = nil          #   @@pagination_options = nil
22  
-        end                       # end
23  
-                                  #
24  
-        def self.#{sym}           # def self.pagination_options
25  
-          @@#{sym}                #   @@pagination_options
26  
-        end                       # end
27  
-                                  #
28  
-        def #{sym}                # def pagination_options
29  
-          @@#{sym}                #   @@pagination_options
30  
-        end                       # end
  20
+      class_eval(<<-EOS, __FILE__, __LINE__ + 1)
  21
+        unless defined? @@#{sym}
  22
+          @@#{sym} = nil
  23
+        end
  24
+
  25
+        def self.#{sym}
  26
+          @@#{sym}
  27
+        end
31 28
       EOS
  29
+
  30
+      unless options[:instance_reader] == false
  31
+        class_eval(<<-EOS, __FILE__, __LINE__ + 1)
  32
+          def #{sym}
  33
+            @@#{sym}
  34
+          end
  35
+        EOS
  36
+      end
32 37
     end
33 38
   end
34 39
   
35 40
   def mattr_writer(*syms)
36 41
     options = syms.extract_options!
37 42
     syms.each do |sym|
38  
-      class_eval(<<-EOS, __FILE__, __LINE__)
39  
-        unless defined? @@#{sym}                       # unless defined? @@pagination_options
40  
-          @@#{sym} = nil                               #   @@pagination_options = nil
41  
-        end                                            # end
42  
-                                                       #
43  
-        def self.#{sym}=(obj)                          # def self.pagination_options=(obj)
44  
-          @@#{sym} = obj                               #   @@pagination_options = obj
45  
-        end                                            # end
46  
-                                                       #
47  
-        #{"                                            #
48  
-        def #{sym}=(obj)                               # def pagination_options=(obj)
49  
-          @@#{sym} = obj                               #   @@pagination_options = obj
50  
-        end                                            # end
51  
-        " unless options[:instance_writer] == false }  # # instance writer above is generated unless options[:instance_writer] == false
  43
+      class_eval(<<-EOS, __FILE__, __LINE__ + 1)
  44
+        unless defined? @@#{sym}
  45
+          @@#{sym} = nil
  46
+        end
  47
+
  48
+        def self.#{sym}=(obj)
  49
+          @@#{sym} = obj
  50
+        end
52 51
       EOS
  52
+
  53
+      unless options[:instance_writer] == false
  54
+        class_eval(<<-EOS, __FILE__, __LINE__ + 1)
  55
+          def #{sym}=(obj)
  56
+            @@#{sym} = obj
  57
+          end
  58
+        EOS
  59
+      end
53 60
     end
54 61
   end
55 62
   
9  activesupport/test/core_ext/class/attribute_accessor_test.rb
@@ -4,7 +4,8 @@ class ClassAttributeAccessorTest < Test::Unit::TestCase
4 4
   def setup
5 5
     @class = Class.new do
6 6
       cattr_accessor :foo
7  
-      cattr_accessor :bar, :instance_writer => false
  7
+      cattr_accessor :bar,  :instance_writer => false
  8
+      cattr_reader   :shaq, :instance_reader => false
8 9
     end
9 10
     @object = @class.new
10 11
   end
@@ -28,4 +29,10 @@ def test_should_not_create_instance_writer
28 29
     assert @object.respond_to?(:bar)
29 30
     assert !@object.respond_to?(:bar=)
30 31
   end
  32
+
  33
+
  34
+  def test_should_not_create_instance_reader
  35
+    assert @class.respond_to?(:shaq)
  36
+    assert !@object.respond_to?(:shaq)
  37
+  end
31 38
 end
8  activesupport/test/core_ext/module/attribute_accessor_test.rb
@@ -4,7 +4,8 @@ class ModuleAttributeAccessorTest < Test::Unit::TestCase
4 4
   def setup
5 5
     m = @module = Module.new do
6 6
       mattr_accessor :foo
7  
-      mattr_accessor :bar, :instance_writer => false
  7
+      mattr_accessor :bar,  :instance_writer => false
  8
+      mattr_reader   :shaq, :instance_reader => false
8 9
     end
9 10
     @class = Class.new
10 11
     @class.instance_eval { include m }
@@ -30,4 +31,9 @@ def test_should_not_create_instance_writer
30 31
     assert @object.respond_to?(:bar)
31 32
     assert !@object.respond_to?(:bar=)
32 33
   end
  34
+
  35
+  def test_should_not_create_instance_reader
  36
+    assert @module.respond_to?(:shaq)
  37
+    assert !@object.respond_to?(:shaq)
  38
+  end
33 39
 end

0 notes on commit bc2af91

Please sign in to comment.
Something went wrong with that request. Please try again.