Skip to content

Commit

Permalink
fixing inconsistency with cattr_reader and matter_reader [#4172 state…
Browse files Browse the repository at this point in the history
…:resolved]

Signed-off-by: wycats <wycats@gmail.com>
  • Loading branch information
probablykabari authored and wycats committed Mar 27, 2010
1 parent 334983e commit b081948
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 11 deletions.
Expand Up @@ -10,21 +10,24 @@
# Person.hair_colors = [:brown, :black, :blonde, :red]
class Class
def cattr_reader(*syms)
options = syms.extract_options!
syms.flatten.each do |sym|
next if sym.is_a?(Hash)
class_eval(<<-EOS, __FILE__, __LINE__ + 1)
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
EOS
unless options[:instance_reader] == false
class_eval(<<-EOS, __FILE__, __LINE__)
def #{sym} # def hair_colors
@@#{sym} # @@hair_colors
end # end
EOS
end
end
end

Expand Down
Expand Up @@ -2,7 +2,7 @@

class Module
def mattr_reader(*syms)
syms.extract_options!
options = syms.extract_options!
syms.each do |sym|
class_eval(<<-EOS, __FILE__, __LINE__ + 1)
unless defined? @@#{sym} # unless defined? @@pagination_options
Expand All @@ -12,11 +12,15 @@ def mattr_reader(*syms)
def self.#{sym} # def self.pagination_options
@@#{sym} # @@pagination_options
end # end
def #{sym} # def pagination_options
@@#{sym} # @@pagination_options
end # end
EOS

unless options[:instance_reader] == false
class_eval(<<-EOS, __FILE__, __LINE__)
def #{sym} # def hair_colors
@@#{sym} # @@hair_colors
end # end
EOS
end
end
end

Expand Down
8 changes: 7 additions & 1 deletion activesupport/test/core_ext/class/attribute_accessor_test.rb
Expand Up @@ -5,7 +5,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
Expand All @@ -29,4 +30,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 @class.respond_to?(:shaq)
assert !@object.respond_to?(:shaq)
end
end
6 changes: 6 additions & 0 deletions activesupport/test/core_ext/module/attribute_accessor_test.rb
Expand Up @@ -6,6 +6,7 @@ def setup
m = @module = Module.new do
mattr_accessor :foo
mattr_accessor :bar, :instance_writer => false
mattr_reader :shaq, :instance_reader => false
end
@class = Class.new
@class.instance_eval { include m }
Expand All @@ -31,4 +32,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

0 comments on commit b081948

Please sign in to comment.