refactor evals and adds some __FILE__ and __LINE__

Signed-off-by: José Valim <jose.valim@gmail.com>
This commit is contained in:
Santiago Pastorino 2010-05-19 16:37:41 -03:00 committed by José Valim
parent 7f07cc364a
commit aacf2581cd
17 changed files with 55 additions and 62 deletions

@ -245,7 +245,7 @@ def _write_layout_method
case defined?(@_layout) ? @_layout : nil
when String
self.class_eval %{def _layout; #{@_layout.inspect} end}
self.class_eval %{def _layout; #{@_layout.inspect} end}, __FILE__, __LINE__
when Symbol
self.class_eval <<-ruby_eval, __FILE__, __LINE__ + 1
def _layout
@ -259,9 +259,9 @@ def _layout
ruby_eval
when Proc
define_method :_layout_from_proc, &@_layout
self.class_eval %{def _layout; _layout_from_proc(self) end}
self.class_eval %{def _layout; _layout_from_proc(self) end}, __FILE__, __LINE__
when false
self.class_eval %{def _layout; end}
self.class_eval %{def _layout; end}, __FILE__, __LINE__
when true
raise ArgumentError, "Layouts must be specified as a String, Symbol, false, or nil"
when nil

@ -43,7 +43,7 @@ def object
end
%w(tag content_tag to_date_select_tag to_datetime_select_tag to_time_select_tag).each do |meth|
module_eval "def #{meth}(*) error_wrapping(super) end"
module_eval "def #{meth}(*) error_wrapping(super) end", __FILE__, __LINE__
end
def error_wrapping(html_tag)

@ -1111,7 +1111,7 @@ def initialize(object_name, object, template, options, proc)
end
(field_helpers - %w(label check_box radio_button fields_for hidden_field)).each do |selector|
src, line = <<-end_src, __LINE__ + 1
class_eval <<-RUBY_EVAL, __FILE__, __LINE__ + 1
def #{selector}(method, options = {}) # def text_field(method, options = {})
@template.send( # @template.send(
#{selector.inspect}, # "text_field",
@ -1119,8 +1119,7 @@ def #{selector}(method, options = {}) # def text_field(method, options = {})
method, # method,
objectify_options(options)) # objectify_options(options))
end # end
end_src
class_eval src, __FILE__, line
RUBY_EVAL
end
def fields_for(record_or_name_or_array, *args, &block)

@ -82,7 +82,7 @@ def determine_default_helper_class(name)
def helper_method(*methods)
# Almost a duplicate from ActionController::Helpers
methods.flatten.each do |method|
_helpers.module_eval <<-end_eval
_helpers.module_eval <<-end_eval, __FILE__, __LINE__ + 1
def #{method}(*args, &block) # def current_user(*args, &block)
_test_case.send(%(#{method}), *args, &block) # test_case.send(%(current_user), *args, &block)
end # end

@ -1425,12 +1425,11 @@ def test_form_for_and_fields_for_with_object
class LabelledFormBuilder < ActionView::Helpers::FormBuilder
(field_helpers - %w(hidden_field)).each do |selector|
src, line = <<-END_SRC, __LINE__ + 1
class_eval <<-RUBY_EVAL, __FILE__, __LINE__ + 1
def #{selector}(field, *args, &proc)
("<label for='\#{field}'>\#{field.to_s.humanize}:</label> " + super + "<br/>").html_safe
end
END_SRC
class_eval src, __FILE__, line
RUBY_EVAL
end
end

@ -378,7 +378,7 @@ def find_associated_records(ids, reflection, preload_options)
def interpolate_sql_for_preload(sql)
instance_eval("%@#{sql.gsub('@', '\@')}@")
instance_eval("%@#{sql.gsub('@', '\@')}@", __FILE__, __LINE__)
end
def append_conditions(reflection, preload_options)

@ -1461,7 +1461,7 @@ def add_counter_cache_callbacks(reflection)
before_destroy(method_name)
module_eval(
"#{reflection.class_name}.send(:attr_readonly,\"#{cache_column}\".intern) if defined?(#{reflection.class_name}) && #{reflection.class_name}.respond_to?(:attr_readonly)"
"#{reflection.class_name}.send(:attr_readonly,\"#{cache_column}\".intern) if defined?(#{reflection.class_name}) && #{reflection.class_name}.respond_to?(:attr_readonly)", __FILE__, __LINE__
)
end
@ -2130,7 +2130,7 @@ def table_name_and_alias
end
def interpolate_sql(sql)
instance_eval("%@#{sql.gsub('@', '\@')}@")
instance_eval("%@#{sql.gsub('@', '\@')}@", __FILE__, __LINE__)
end
end
end

@ -1786,7 +1786,7 @@ def quote_value(value, column = nil)
# Interpolate custom SQL string in instance context.
# Optional record argument is meant for custom insert_sql.
def interpolate_sql(sql, record = nil)
instance_eval("%@#{sql.gsub('@', '\@')}@")
instance_eval("%@#{sql.gsub('@', '\@')}@", __FILE__, __LINE__)
end
# Instantiates objects for all attribute classes that needs more than one constructor parameter. This is done

@ -582,12 +582,13 @@ def prefix=(value = '/')
# Clear prefix parameters in case they have been cached
@prefix_parameters = nil
# Redefine the new methods.
code, line = <<-end_code, __LINE__ + 1
def prefix_source() "#{value}" end
def prefix(options={}) "#{prefix_call}" end
end_code
silence_warnings { instance_eval code, __FILE__, line }
silence_warnings do
# Redefine the new methods.
instance_eval <<-RUBY_EVAL, __FILE__, __LINE__ + 1
def prefix_source() "#{value}" end
def prefix(options={}) "#{prefix_call}" end
RUBY_EVAL
end
rescue
logger.error "Couldn't set prefix: #{$!}\n #{code}" if logger
raise

@ -203,8 +203,8 @@ def start(key=nil, object=nil)
# 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 @@ def #{name}(halted)
end
end
RUBY_EVAL
@klass.class_eval(txt, __FILE__, line)
"#{name}(halted) do"
end
end
@ -312,9 +311,9 @@ def #{method_name}(&blk)
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 @@ def __define_runner(symbol) #:nodoc:
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
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
send(name, &blk)
else
#{body}
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
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"
unless respond_to?(name)
self.class.__create_keyed_callback(name, :#{symbol}, self, &blk)
end
send(name, &blk)
else
#{body}
end
end
private :_run_#{symbol}_callbacks
RUBY_EVAL
end
end

@ -61,10 +61,7 @@ def #{name}?
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

@ -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

@ -66,7 +66,7 @@ def initialize
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)

@ -7,7 +7,7 @@ def self.extended(klass)
unless method_defined?(:describe)
def self.describe(text)
class_eval <<-RUBY_EVAL
class_eval <<-RUBY_EVAL, __FILE__, __LINE__ + 1
def self.name
"#{text}"
end

@ -371,7 +371,7 @@ def test_file_search_uses_first_in_load_path
end
def test_custom_const_missing_should_work
Object.module_eval <<-end_eval
Object.module_eval <<-end_eval, __FILE__, __LINE__ + 1
module ModuleWithCustomConstMissing
def self.const_missing(name)
const_set name, name.to_s.hash

@ -209,14 +209,14 @@ def test_symbol_to_lower_camel
end
%w{plurals singulars uncountables humans}.each do |inflection_type|
class_eval "
class_eval <<-RUBY, __FILE__, __LINE__ + 1
def test_clear_#{inflection_type}
cached_values = ActiveSupport::Inflector.inflections.#{inflection_type}
ActiveSupport::Inflector.inflections.clear :#{inflection_type}
assert ActiveSupport::Inflector.inflections.#{inflection_type}.empty?, \"#{inflection_type} inflections should be empty after clear :#{inflection_type}\"
ActiveSupport::Inflector.inflections.instance_variable_set :@#{inflection_type}, cached_values
end
"
RUBY
end
def test_clear_all

@ -17,7 +17,7 @@ def initialize(generator)
private
%w(template copy_file directory empty_directory inside
empty_directory_with_gitkeep create_file chmod shebang).each do |method|
class_eval <<-RUBY
class_eval <<-RUBY, __FILE__, __LINE__ + 1
def #{method}(*args, &block)
@generator.send(:#{method}, *args, &block)
end