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 --- .../lib/active_support/core_ext/module.rb | 1 + .../core_ext/module/attr_internal.rb | 31 ++++++++++++++++++++++ 2 files changed, 32 insertions(+) create mode 100644 activesupport/lib/active_support/core_ext/module/attr_internal.rb (limited to 'activesupport/lib/active_support/core_ext') 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 -- cgit v1.2.3