From d32965399ccfa2052a4d52b70db1bae0ca16830b Mon Sep 17 00:00:00 2001 From: Xavier Noria Date: Sat, 13 Jun 2009 01:01:42 +0200 Subject: uses Object#metaclass and Object#class_eval in a few spots [#2797 state:committed] Signed-off-by: Jeremy Kemper --- actionpack/lib/action_controller/testing/integration.rb | 3 ++- activerecord/lib/active_record/base.rb | 3 ++- activerecord/lib/active_record/migration.rb | 5 +++-- activerecord/lib/active_record/named_scope.rb | 3 ++- activesupport/lib/active_support/core_ext/proc.rb | 2 +- activesupport/lib/active_support/new_callbacks.rb | 2 +- 6 files changed, 11 insertions(+), 7 deletions(-) diff --git a/actionpack/lib/action_controller/testing/integration.rb b/actionpack/lib/action_controller/testing/integration.rb index af4ccb7837..37ca93f22c 100644 --- a/actionpack/lib/action_controller/testing/integration.rb +++ b/actionpack/lib/action_controller/testing/integration.rb @@ -1,6 +1,7 @@ require 'stringio' require 'uri' require 'active_support/test_case' +require 'active_support/core_ext/object/metaclass' require 'rack/mock_session' require 'rack/test/cookie_jar' @@ -191,7 +192,7 @@ module ActionController unless defined? @named_routes_configured # install the named routes in this session instance. - klass = class << self; self; end + klass = metaclass Routing::Routes.install_helpers(klass) # the helpers are made protected by default--we make them public for diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index c4d143ab05..1fc0c93732 100755 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -11,6 +11,7 @@ require 'active_support/core_ext/hash/indifferent_access' require 'active_support/core_ext/hash/slice' require 'active_support/core_ext/string/behavior' require 'active_support/core_ext/symbol' +require 'active_support/core_ext/object/metaclass' module ActiveRecord #:nodoc: # Generic Active Record exception class. @@ -2040,7 +2041,7 @@ module ActiveRecord #:nodoc: # end # end def define_attr_method(name, value=nil, &block) - sing = class << self; self; end + sing = metaclass sing.send :alias_method, "original_#{name}", name if block_given? sing.send :define_method, name, &block diff --git a/activerecord/lib/active_record/migration.rb b/activerecord/lib/active_record/migration.rb index a7be3539d5..467d955a49 100644 --- a/activerecord/lib/active_record/migration.rb +++ b/activerecord/lib/active_record/migration.rb @@ -1,3 +1,5 @@ +require 'active_support/core_ext/object/metaclass' + module ActiveRecord class IrreversibleMigration < ActiveRecordError#:nodoc: end @@ -300,8 +302,7 @@ module ActiveRecord case sym when :up, :down - klass = (class << self; self; end) - klass.send(:alias_method_chain, sym, "benchmarks") + metaclass.send(:alias_method_chain, sym, "benchmarks") end ensure @ignore_new_methods = false diff --git a/activerecord/lib/active_record/named_scope.rb b/activerecord/lib/active_record/named_scope.rb index 1b22fa5e24..dd2a90b8e5 100644 --- a/activerecord/lib/active_record/named_scope.rb +++ b/activerecord/lib/active_record/named_scope.rb @@ -1,5 +1,6 @@ require 'active_support/core_ext/array' require 'active_support/core_ext/hash/except' +require 'active_support/core_ext/object/metaclass' module ActiveRecord module NamedScope @@ -99,7 +100,7 @@ module ActiveRecord end end, &block) end - (class << self; self end).instance_eval do + metaclass.instance_eval do define_method name do |*args| scopes[name].call(self, *args) end diff --git a/activesupport/lib/active_support/core_ext/proc.rb b/activesupport/lib/active_support/core_ext/proc.rb index 5c29cc32a2..572fc5f92c 100644 --- a/activesupport/lib/active_support/core_ext/proc.rb +++ b/activesupport/lib/active_support/core_ext/proc.rb @@ -1,7 +1,7 @@ class Proc #:nodoc: def bind(object) block, time = self, Time.now - (class << object; self end).class_eval do + object.class_eval do method_name = "__bind_#{time.to_i}_#{time.usec}" define_method(method_name, &block) # define_method("__bind_1230458026_720454", &block) method = instance_method(method_name) # method = instance_method("__bind_1230458026_720454") diff --git a/activesupport/lib/active_support/new_callbacks.rb b/activesupport/lib/active_support/new_callbacks.rb index bc340fccec..fa22f85d69 100644 --- a/activesupport/lib/active_support/new_callbacks.rb +++ b/activesupport/lib/active_support/new_callbacks.rb @@ -307,7 +307,7 @@ module ActiveSupport def _normalize_legacy_filter(kind, filter) if !filter.respond_to?(kind) && filter.respond_to?(:filter) - filter.metaclass.class_eval( + filter.class_eval( "def #{kind}(context, &block) filter(context, &block) end", __FILE__, __LINE__ - 1) elsif filter.respond_to?(:before) && filter.respond_to?(:after) && kind == :around -- cgit v1.2.3