From cfd9e05a2edf53bc92220cd91795a6b8f1029630 Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Mon, 7 Aug 2006 01:16:37 +0000 Subject: attr_internal to support namespacing and deprecation git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@4692 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- activesupport/CHANGELOG | 6 +++ .../lib/active_support/core_ext/module.rb | 1 + .../core_ext/module/attr_internal.rb | 31 +++++++++++++ .../test/core_ext/module/attr_internal_test.rb | 52 ++++++++++++++++++++++ 4 files changed, 90 insertions(+) create mode 100644 activesupport/lib/active_support/core_ext/module/attr_internal.rb create mode 100644 activesupport/test/core_ext/module/attr_internal_test.rb (limited to 'activesupport') diff --git a/activesupport/CHANGELOG b/activesupport/CHANGELOG index 13fb1521f2..cc79c9d7c4 100644 --- a/activesupport/CHANGELOG +++ b/activesupport/CHANGELOG @@ -1,5 +1,11 @@ *SVN* +* attr_internal to support namespacing and deprecation. Like attr_* except backed by internally-named instance variable. Set attr_internal_naming_format to change the format from the default '@_%s'. [Jeremy Kemper] + # def foo() @foo__rofl end + # def foo=(v) @foo__rofl = v end + self.attr_internal_naming_format = '@%s__rofl' + attr_internal :foo + * Raise fully qualified names upon name errors. #5533 [lars@pinds.com, Nicholas Seckar] * Add extention to obtain the missing constant from NameError instances. [Nicholas Seckar] diff --git a/activesupport/lib/active_support/core_ext/module.rb b/activesupport/lib/active_support/core_ext/module.rb index 45069f52af..8d48de53dd 100644 --- a/activesupport/lib/active_support/core_ext/module.rb +++ b/activesupport/lib/active_support/core_ext/module.rb @@ -1,5 +1,6 @@ require File.dirname(__FILE__) + '/module/inclusion' require File.dirname(__FILE__) + '/module/attribute_accessors' +require File.dirname(__FILE__) + '/module/attr_internal' require File.dirname(__FILE__) + '/module/delegation' require File.dirname(__FILE__) + '/module/introspection' require File.dirname(__FILE__) + '/module/loading' diff --git a/activesupport/lib/active_support/core_ext/module/attr_internal.rb b/activesupport/lib/active_support/core_ext/module/attr_internal.rb new file mode 100644 index 0000000000..c793da7159 --- /dev/null +++ b/activesupport/lib/active_support/core_ext/module/attr_internal.rb @@ -0,0 +1,31 @@ +class Module + # Declare an attribute reader backed by an internally-named instance variable. + def attr_internal_reader(*attrs) + attrs.each do |attr| + module_eval "def #{attr}() #{attr_internal_ivar_name(attr)} end" + end + end + + # Declare an attribute writer backed by an internally-named instance variable. + def attr_internal_writer(*attrs) + attrs.each do |attr| + module_eval "def #{attr}=(v) #{attr_internal_ivar_name(attr)} = v end" + end + end + + # Declare attributes backed by 'internal' instance variables names. + def attr_internal_accessor(*attrs) + attr_internal_reader *attrs + attr_internal_writer *attrs + end + + alias_method :attr_internal, :attr_internal_accessor + + private + mattr_accessor :attr_internal_naming_format + self.attr_internal_naming_format = '@_%s' + + def attr_internal_ivar_name(attr) + attr_internal_naming_format % attr + end +end diff --git a/activesupport/test/core_ext/module/attr_internal_test.rb b/activesupport/test/core_ext/module/attr_internal_test.rb new file mode 100644 index 0000000000..25b049f619 --- /dev/null +++ b/activesupport/test/core_ext/module/attr_internal_test.rb @@ -0,0 +1,52 @@ +require File.dirname(__FILE__) + '/../../abstract_unit' + +class AttrInternalTest < Test::Unit::TestCase + def setup + @target = Class.new + @instance = @target.new + end + + def test_attr_internal_reader + assert_nothing_raised { @target.attr_internal_reader :foo } + + assert !@instance.instance_variables.include?('@_foo') + assert_raise(NoMethodError) { @instance.foo = 1 } + + @instance.instance_variable_set('@_foo', 1) + assert_nothing_raised { assert_equal 1, @instance.foo } + end + + def test_attr_internal_writer + assert_nothing_raised { @target.attr_internal_writer :foo } + + assert !@instance.instance_variables.include?('@_foo') + assert_nothing_raised { assert_equal 1, @instance.foo = 1 } + + assert_equal 1, @instance.instance_variable_get('@_foo') + assert_raise(NoMethodError) { @instance.foo } + end + + def test_attr_internal_accessor + assert_nothing_raised { @target.attr_internal :foo } + + assert !@instance.instance_variables.include?('@_foo') + assert_nothing_raised { assert_equal 1, @instance.foo = 1 } + + assert_equal 1, @instance.instance_variable_get('@_foo') + assert_nothing_raised { assert_equal 1, @instance.foo } + end + + def test_attr_internal_naming_format + assert_equal '@_%s', @target.attr_internal_naming_format + assert_nothing_raised { @target.attr_internal_naming_format = '@abc%sdef' } + @target.attr_internal :foo + + assert !@instance.instance_variables.include?('@_foo') + assert !@instance.instance_variables.include?('@abcfoodef') + assert_nothing_raised { @instance.foo = 1 } + assert !@instance.instance_variables.include?('@_foo') + assert @instance.instance_variables.include?('@abcfoodef') + ensure + @target.attr_internal_naming_format = '@_%s' + end +end -- cgit v1.2.3