From aacf2581cde0147dc66c3eeacf6d7447c2bbafdf Mon Sep 17 00:00:00 2001 From: Santiago Pastorino Date: Wed, 19 May 2010 16:37:41 -0300 Subject: refactor evals and adds some __FILE__ and __LINE__ MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: José Valim --- activesupport/lib/active_support/callbacks.rb | 51 ++++++++++------------ .../lib/active_support/core_ext/class/attribute.rb | 5 +-- .../core_ext/module/attr_internal.rb | 4 +- activesupport/lib/active_support/dependencies.rb | 2 +- .../lib/active_support/testing/declarative.rb | 2 +- 5 files changed, 29 insertions(+), 35 deletions(-) (limited to 'activesupport/lib') diff --git a/activesupport/lib/active_support/callbacks.rb b/activesupport/lib/active_support/callbacks.rb index 5a7b94ead7..933667c909 100644 --- a/activesupport/lib/active_support/callbacks.rb +++ b/activesupport/lib/active_support/callbacks.rb @@ -203,8 +203,8 @@ module ActiveSupport # end # name = "_conditional_callback_#{@kind}_#{next_id}" - txt, line = <<-RUBY_EVAL, __LINE__ + 1 - def #{name}(halted) + @klass.class_eval <<-RUBY_EVAL, __FILE__, __LINE__ + 1 + def #{name}(halted) #{@compiled_options[0] || "if true"} && !halted #{@filter} do yield self @@ -214,7 +214,6 @@ module ActiveSupport end end RUBY_EVAL - @klass.class_eval(txt, __FILE__, line) "#{name}(halted) do" end end @@ -312,9 +311,9 @@ module ActiveSupport def _normalize_legacy_filter(kind, filter) if !filter.respond_to?(kind) && filter.respond_to?(:filter) - filter.singleton_class.class_eval( - "def #{kind}(context, &block) filter(context, &block) end", - __FILE__, __LINE__ - 1) + filter.singleton_class.class_eval <<-RUBY_EVAL, __FILE__, __LINE__ + 1 + def #{kind}(context, &block) filter(context, &block) end + RUBY_EVAL elsif filter.respond_to?(:before) && filter.respond_to?(:after) && kind == :around def filter.around(context) should_continue = before(context) @@ -387,31 +386,29 @@ module ActiveSupport send("_update_#{symbol}_superclass_callbacks") body = send("_#{symbol}_callbacks").compile(nil) - body, line = <<-RUBY_EVAL, __LINE__ + 1 - def _run_#{symbol}_callbacks(key = nil, &blk) - if self.class.send("_update_#{symbol}_superclass_callbacks") - self.class.__define_runner(#{symbol.inspect}) - return _run_#{symbol}_callbacks(key, &blk) - end + silence_warnings do + undef_method "_run_#{symbol}_callbacks" if method_defined?("_run_#{symbol}_callbacks") + class_eval <<-RUBY_EVAL, __FILE__, __LINE__ + 1 + def _run_#{symbol}_callbacks(key = nil, &blk) + if self.class.send("_update_#{symbol}_superclass_callbacks") + self.class.__define_runner(#{symbol.inspect}) + return _run_#{symbol}_callbacks(key, &blk) + end - if key - name = "_run__\#{self.class.name.hash.abs}__#{symbol}__\#{key.hash.abs}__callbacks" + if key + name = "_run__\#{self.class.name.hash.abs}__#{symbol}__\#{key.hash.abs}__callbacks" - unless respond_to?(name) - self.class.__create_keyed_callback(name, :#{symbol}, self, &blk) - end + unless respond_to?(name) + self.class.__create_keyed_callback(name, :#{symbol}, self, &blk) + end - send(name, &blk) - else - #{body} + send(name, &blk) + else + #{body} + end end - end - private :_run_#{symbol}_callbacks - RUBY_EVAL - - silence_warnings do - undef_method "_run_#{symbol}_callbacks" if method_defined?("_run_#{symbol}_callbacks") - class_eval body, __FILE__, line + private :_run_#{symbol}_callbacks + RUBY_EVAL end end diff --git a/activesupport/lib/active_support/core_ext/class/attribute.rb b/activesupport/lib/active_support/core_ext/class/attribute.rb index d2bcd7a778..576366e496 100644 --- a/activesupport/lib/active_support/core_ext/class/attribute.rb +++ b/activesupport/lib/active_support/core_ext/class/attribute.rb @@ -61,10 +61,7 @@ class Class end RUBY - if instance_writer - body = "def #{name}=(value) @#{name} = value end" - class_eval body, __FILE__, __LINE__ - 1 - end + attr_writer name if instance_writer end end end 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 d052bfed2d..28bc30ae26 100644 --- a/activesupport/lib/active_support/core_ext/module/attr_internal.rb +++ b/activesupport/lib/active_support/core_ext/module/attr_internal.rb @@ -2,14 +2,14 @@ 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" + module_eval "def #{attr}() #{attr_internal_ivar_name(attr)} end", __FILE__, __LINE__ end 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" + module_eval "def #{attr}=(v) #{attr_internal_ivar_name(attr)} = v end", __FILE__, __LINE__ end end diff --git a/activesupport/lib/active_support/dependencies.rb b/activesupport/lib/active_support/dependencies.rb index 9c4412c28c..e14e225596 100644 --- a/activesupport/lib/active_support/dependencies.rb +++ b/activesupport/lib/active_support/dependencies.rb @@ -66,7 +66,7 @@ module ActiveSupport #:nodoc: end def self.locked(*methods) - methods.each { |m| class_eval "def #{m}(*) lock { super } end" } + methods.each { |m| class_eval "def #{m}(*) lock { super } end", __FILE__, __LINE__ } end def get(key) diff --git a/activesupport/lib/active_support/testing/declarative.rb b/activesupport/lib/active_support/testing/declarative.rb index a7df473644..70a6c2ca60 100644 --- a/activesupport/lib/active_support/testing/declarative.rb +++ b/activesupport/lib/active_support/testing/declarative.rb @@ -7,7 +7,7 @@ module ActiveSupport unless method_defined?(:describe) def self.describe(text) - class_eval <<-RUBY_EVAL + class_eval <<-RUBY_EVAL, __FILE__, __LINE__ + 1 def self.name "#{text}" end -- cgit v1.2.3