diff options
author | thedarkone <thedarkone2@gmail.com> | 2010-09-26 17:05:54 +0200 |
---|---|---|
committer | thedarkone <thedarkone2@gmail.com> | 2010-09-27 17:45:58 +0200 |
commit | f2e0b3575e8c84cd23b75fcbfee83e7c683781ef (patch) | |
tree | d8311843525e22f80e60ce9c59f5285c837bbb89 /activesupport/lib/active_support | |
parent | 320382ccd359397f2c83f8c0c622b296dcfb472b (diff) | |
download | rails-f2e0b3575e8c84cd23b75fcbfee83e7c683781ef.tar.gz rails-f2e0b3575e8c84cd23b75fcbfee83e7c683781ef.tar.bz2 rails-f2e0b3575e8c84cd23b75fcbfee83e7c683781ef.zip |
Use native attr_* macros for performance reasons.
Diffstat (limited to 'activesupport/lib/active_support')
-rw-r--r-- | activesupport/lib/active_support/core_ext/module/attr_internal.rb | 19 |
1 files changed, 13 insertions, 6 deletions
diff --git a/activesupport/lib/active_support/core_ext/module/attr_internal.rb b/activesupport/lib/active_support/core_ext/module/attr_internal.rb index 28bc30ae26..00db75bfec 100644 --- a/activesupport/lib/active_support/core_ext/module/attr_internal.rb +++ b/activesupport/lib/active_support/core_ext/module/attr_internal.rb @@ -1,16 +1,12 @@ class Module # Declares 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", __FILE__, __LINE__ - end + attrs.each {|attr_name| attr_internal_define(attr_name, :reader)} end # Declares 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", __FILE__, __LINE__ - end + attrs.each {|attr_name| attr_internal_define(attr_name, :writer)} end # Declares an attribute reader and writer backed by an internally-named instance @@ -29,4 +25,15 @@ class Module def attr_internal_ivar_name(attr) Module.attr_internal_naming_format % attr end + + def attr_internal_define(attr_name, type) + internal_name = attr_internal_ivar_name(attr_name).sub(/\A@/, '') + class_eval do # class_eval is necessary on 1.9 or else the methods a made private + # use native attr_* methods as they are faster on some Ruby implementations + send("attr_#{type}", internal_name) + end + attr_name, internal_name = "#{attr_name}=", "#{internal_name}=" if type == :writer + alias_method attr_name, internal_name + remove_method internal_name + end end |