From db94143ea1869ebba11dea5f70fa271d44affd86 Mon Sep 17 00:00:00 2001 From: Kabari Hendrick Date: Sun, 14 Mar 2010 01:33:45 -0600 Subject: [PATCH] fixing inconsistency with cattr_reader and matter_reader --- .../core_ext/class/attribute_accessors.rb | 13 ++++++++----- .../core_ext/module/attribute_accessors.rb | 14 +++++++++----- .../test/core_ext/class/attribute_accessor_test.rb | 8 +++++++- .../core_ext/module/attribute_accessor_test.rb | 6 ++++++ 4 files changed, 30 insertions(+), 11 deletions(-) diff --git a/activesupport/lib/active_support/core_ext/class/attribute_accessors.rb b/activesupport/lib/active_support/core_ext/class/attribute_accessors.rb index 1602a60..83d154b 100644 --- a/activesupport/lib/active_support/core_ext/class/attribute_accessors.rb +++ b/activesupport/lib/active_support/core_ext/class/attribute_accessors.rb @@ -10,8 +10,8 @@ require 'active_support/core_ext/array/extract_options' # 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 @@ -20,11 +20,14 @@ class Class 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 diff --git a/activesupport/lib/active_support/core_ext/module/attribute_accessors.rb b/activesupport/lib/active_support/core_ext/module/attribute_accessors.rb index 131b512..c315fd5 100644 --- a/activesupport/lib/active_support/core_ext/module/attribute_accessors.rb +++ b/activesupport/lib/active_support/core_ext/module/attribute_accessors.rb @@ -2,7 +2,7 @@ require 'active_support/core_ext/array/extract_options' 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 @@ -12,11 +12,15 @@ class Module 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 diff --git a/activesupport/test/core_ext/class/attribute_accessor_test.rb b/activesupport/test/core_ext/class/attribute_accessor_test.rb index 2214ba9..0f579d1 100644 --- a/activesupport/test/core_ext/class/attribute_accessor_test.rb +++ b/activesupport/test/core_ext/class/attribute_accessor_test.rb @@ -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 @@ -29,4 +30,9 @@ class ClassAttributeAccessorTest < Test::Unit::TestCase 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 diff --git a/activesupport/test/core_ext/module/attribute_accessor_test.rb b/activesupport/test/core_ext/module/attribute_accessor_test.rb index bd9461e..263e78f 100644 --- a/activesupport/test/core_ext/module/attribute_accessor_test.rb +++ b/activesupport/test/core_ext/module/attribute_accessor_test.rb @@ -6,6 +6,7 @@ class ModuleAttributeAccessorTest < Test::Unit::TestCase 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 } @@ -31,4 +32,9 @@ class ModuleAttributeAccessorTest < Test::Unit::TestCase 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 -- 1.6.6.rc3