Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

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
@spastorino spastorino authored wycats committed
View
59 activesupport/lib/active_support/core_ext/class/attribute_accessors.rb
@@ -8,42 +8,49 @@
# Person.hair_colors = [:brown, :black, :blonde, :red]
class Class
def cattr_reader(*syms)
+ options = syms.extract_options!
syms.each do |sym|
next if sym.is_a?(Hash)
- class_eval(<<-EOS, __FILE__, __LINE__)
- unless defined? @@#{sym} # unless defined? @@hair_colors
- @@#{sym} = nil # @@hair_colors = nil
- end # end
- #
- def self.#{sym} # def self.hair_colors
- @@#{sym} # @@hair_colors
- end # end
- #
- def #{sym} # def hair_colors
- @@#{sym} # @@hair_colors
- end # end
+ class_eval(<<-EOS, __FILE__, __LINE__ + 1)
+ unless defined? @@#{sym}
+ @@#{sym} = nil
+ end
+
+ def self.#{sym}
+ @@#{sym}
+ end
EOS
+
+ unless options[:instance_reader] == false
+ class_eval(<<-EOS, __FILE__, __LINE__ + 1)
+ def #{sym}
+ @@#{sym}
+ end
+ EOS
+ end
end
end
def cattr_writer(*syms)
options = syms.extract_options!
syms.each do |sym|
- class_eval(<<-EOS, __FILE__, __LINE__)
- unless defined? @@#{sym} # unless defined? @@hair_colors
- @@#{sym} = nil # @@hair_colors = nil
- end # end
- #
- def self.#{sym}=(obj) # def self.hair_colors=(obj)
- @@#{sym} = obj # @@hair_colors = obj
- end # end
- #
- #{" #
- def #{sym}=(obj) # def hair_colors=(obj)
- @@#{sym} = obj # @@hair_colors = obj
- end # end
- " unless options[:instance_writer] == false } # # instance writer above is generated unless options[:instance_writer] == false
+ class_eval(<<-EOS, __FILE__, __LINE__ + 1)
+ unless defined? @@#{sym}
+ @@#{sym} = nil
+ end
+
+ def self.#{sym}=(obj)
+ @@#{sym} = obj
+ end
EOS
+
+ unless options[:instance_writer] == false
+ class_eval(<<-EOS, __FILE__, __LINE__ + 1)
+ def #{sym}=(obj)
+ @@#{sym} = obj
+ end
+ EOS
+ end
end
end
View
59 activesupport/lib/active_support/core_ext/module/attribute_accessors.rb
@@ -14,42 +14,49 @@
# AppConfiguration.google_api_key = "overriding the api key!"
class Module
def mattr_reader(*syms)
+ options = syms.extract_options!
syms.each do |sym|
next if sym.is_a?(Hash)
- class_eval(<<-EOS, __FILE__, __LINE__)
- unless defined? @@#{sym} # unless defined? @@pagination_options
- @@#{sym} = nil # @@pagination_options = nil
- end # end
- #
- def self.#{sym} # def self.pagination_options
- @@#{sym} # @@pagination_options
- end # end
- #
- def #{sym} # def pagination_options
- @@#{sym} # @@pagination_options
- end # end
+ class_eval(<<-EOS, __FILE__, __LINE__ + 1)
+ unless defined? @@#{sym}
+ @@#{sym} = nil
+ end
+
+ def self.#{sym}
+ @@#{sym}
+ end
EOS
+
+ unless options[:instance_reader] == false
+ class_eval(<<-EOS, __FILE__, __LINE__ + 1)
+ def #{sym}
+ @@#{sym}
+ end
+ EOS
+ end
end
end
def mattr_writer(*syms)
options = syms.extract_options!
syms.each do |sym|
- class_eval(<<-EOS, __FILE__, __LINE__)
- unless defined? @@#{sym} # unless defined? @@pagination_options
- @@#{sym} = nil # @@pagination_options = nil
- end # end
- #
- def self.#{sym}=(obj) # def self.pagination_options=(obj)
- @@#{sym} = obj # @@pagination_options = obj
- end # end
- #
- #{" #
- def #{sym}=(obj) # def pagination_options=(obj)
- @@#{sym} = obj # @@pagination_options = obj
- end # end
- " unless options[:instance_writer] == false } # # instance writer above is generated unless options[:instance_writer] == false
+ class_eval(<<-EOS, __FILE__, __LINE__ + 1)
+ unless defined? @@#{sym}
+ @@#{sym} = nil
+ end
+
+ def self.#{sym}=(obj)
+ @@#{sym} = obj
+ end
EOS
+
+ unless options[:instance_writer] == false
+ class_eval(<<-EOS, __FILE__, __LINE__ + 1)
+ def #{sym}=(obj)
+ @@#{sym} = obj
+ end
+ EOS
+ end
end
end
View
9 activesupport/test/core_ext/class/attribute_accessor_test.rb
@@ -4,7 +4,8 @@ class ClassAttributeAccessorTest < Test::Unit::TestCase
def setup
@class = Class.new do
cattr_accessor :foo
- cattr_accessor :bar, :instance_writer => false
+ cattr_accessor :bar, :instance_writer => false
+ cattr_reader :shaq, :instance_reader => false
end
@object = @class.new
end
@@ -28,4 +29,10 @@ def test_should_not_create_instance_writer
assert @object.respond_to?(:bar)
assert !@object.respond_to?(:bar=)
end
+
+
+ def test_should_not_create_instance_reader
+ assert @class.respond_to?(:shaq)
+ assert !@object.respond_to?(:shaq)
+ end
end
View
8 activesupport/test/core_ext/module/attribute_accessor_test.rb
@@ -4,7 +4,8 @@ class ModuleAttributeAccessorTest < Test::Unit::TestCase
def setup
m = @module = Module.new do
mattr_accessor :foo
- mattr_accessor :bar, :instance_writer => false
+ mattr_accessor :bar, :instance_writer => false
+ mattr_reader :shaq, :instance_reader => false
end
@class = Class.new
@class.instance_eval { include m }
@@ -30,4 +31,9 @@ def test_should_not_create_instance_writer
assert @object.respond_to?(:bar)
assert !@object.respond_to?(:bar=)
end
+
+ def test_should_not_create_instance_reader
+ assert @module.respond_to?(:shaq)
+ assert !@object.respond_to?(:shaq)
+ end
end
Please sign in to comment.
Something went wrong with that request. Please try again.