diff options
-rw-r--r-- | actionpack/lib/action_dispatch/testing/integration.rb | 4 | ||||
-rw-r--r-- | activemodel/lib/active_model/attribute_methods.rb | 2 | ||||
-rwxr-xr-x | activerecord/lib/active_record/base.rb | 2 | ||||
-rw-r--r-- | activerecord/lib/active_record/migration.rb | 4 | ||||
-rw-r--r-- | activerecord/lib/active_record/named_scope.rb | 4 | ||||
-rw-r--r-- | activesupport/CHANGELOG | 2 | ||||
-rw-r--r-- | activesupport/lib/active_support/callbacks.rb | 4 | ||||
-rw-r--r-- | activesupport/lib/active_support/core_ext/class/attribute.rb | 11 | ||||
-rw-r--r-- | activesupport/lib/active_support/core_ext/class/delegating_attributes.rb | 12 | ||||
-rw-r--r-- | activesupport/lib/active_support/core_ext/object.rb | 1 | ||||
-rw-r--r-- | activesupport/lib/active_support/core_ext/object/metaclass.rb | 11 | ||||
-rw-r--r-- | activesupport/lib/active_support/memoizable.rb | 2 | ||||
-rw-r--r-- | activesupport/test/core_ext/object_and_class_ext_test.rb | 16 | ||||
-rw-r--r-- | railties/lib/rails/generators.rb | 4 |
14 files changed, 44 insertions, 35 deletions
diff --git a/actionpack/lib/action_dispatch/testing/integration.rb b/actionpack/lib/action_dispatch/testing/integration.rb index f93059759b..14c7ff642b 100644 --- a/actionpack/lib/action_dispatch/testing/integration.rb +++ b/actionpack/lib/action_dispatch/testing/integration.rb @@ -1,6 +1,6 @@ require 'stringio' require 'uri' -require 'active_support/core_ext/object/metaclass' +require 'active_support/core_ext/object/singleton_class' require 'rack/test' module ActionDispatch @@ -187,7 +187,7 @@ module ActionDispatch unless defined? @named_routes_configured # install the named routes in this session instance. - klass = metaclass + klass = singleton_class ActionDispatch::Routing::Routes.install_helpers(klass) # the helpers are made protected by default--we make them public for diff --git a/activemodel/lib/active_model/attribute_methods.rb b/activemodel/lib/active_model/attribute_methods.rb index 200a6afbf0..143eb87f54 100644 --- a/activemodel/lib/active_model/attribute_methods.rb +++ b/activemodel/lib/active_model/attribute_methods.rb @@ -86,7 +86,7 @@ module ActiveModel # AttributePerson.inheritance_column # # => 'address_id' def define_attr_method(name, value=nil, &block) - sing = metaclass + sing = singleton_class sing.send :alias_method, "original_#{name}", name if block_given? sing.send :define_method, name, &block diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index ef5a7d5787..83f0b58e8a 100755 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -11,7 +11,7 @@ require 'active_support/core_ext/hash/deep_merge' 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/object/metaclass' +require 'active_support/core_ext/object/singleton_class' require 'active_support/core_ext/module/delegation' module ActiveRecord #:nodoc: diff --git a/activerecord/lib/active_record/migration.rb b/activerecord/lib/active_record/migration.rb index 068d2a25b2..fd5ffc6d77 100644 --- a/activerecord/lib/active_record/migration.rb +++ b/activerecord/lib/active_record/migration.rb @@ -1,4 +1,4 @@ -require 'active_support/core_ext/object/metaclass' +require 'active_support/core_ext/object/singleton_class' module ActiveRecord # Exception that can be raised to stop migrations from going backwards. @@ -303,7 +303,7 @@ module ActiveRecord case sym when :up, :down - metaclass.send(:alias_method_chain, sym, "benchmarks") + singleton_class.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 ff6c041ef4..f1f56850ae 100644 --- a/activerecord/lib/active_record/named_scope.rb +++ b/activerecord/lib/active_record/named_scope.rb @@ -1,6 +1,6 @@ require 'active_support/core_ext/array' require 'active_support/core_ext/hash/except' -require 'active_support/core_ext/object/metaclass' +require 'active_support/core_ext/object/singleton_class' module ActiveRecord module NamedScope @@ -112,7 +112,7 @@ module ActiveRecord options.call(*args) end, &block) end - metaclass.instance_eval do + singleton_class.instance_eval do define_method name do |*args| scopes[name].call(self, *args) end diff --git a/activesupport/CHANGELOG b/activesupport/CHANGELOG index 8ec903e376..56c81cf63b 100644 --- a/activesupport/CHANGELOG +++ b/activesupport/CHANGELOG @@ -1,5 +1,7 @@ *Rails 3.0 (pending)* +* Use Object#singleton_class instead of #metaclass. Prefer Ruby's choice. [Jeremy Kemper] + * JSON backend for YAJL. Preferred if available. #2666 [Brian Lopez] diff --git a/activesupport/lib/active_support/callbacks.rb b/activesupport/lib/active_support/callbacks.rb index 6727eda811..b230bb8a40 100644 --- a/activesupport/lib/active_support/callbacks.rb +++ b/activesupport/lib/active_support/callbacks.rb @@ -1,7 +1,7 @@ require 'active_support/core_ext/array/wrap' require 'active_support/core_ext/class/inheritable_attributes' require 'active_support/core_ext/kernel/reporting' -require 'active_support/core_ext/object/metaclass' +require 'active_support/core_ext/object/singleton_class' module ActiveSupport # Callbacks are hooks into the lifecycle of an object that allow you to trigger logic @@ -312,7 +312,7 @@ module ActiveSupport def _normalize_legacy_filter(kind, filter) if !filter.respond_to?(kind) && filter.respond_to?(:filter) - filter.metaclass.class_eval( + filter.singleton_class.class_eval( "def #{kind}(context, &block) filter(context, &block) end", __FILE__, __LINE__ - 1) elsif filter.respond_to?(:before) && filter.respond_to?(:after) && kind == :around diff --git a/activesupport/lib/active_support/core_ext/class/attribute.rb b/activesupport/lib/active_support/core_ext/class/attribute.rb index d74219cb93..1bd39a9349 100644 --- a/activesupport/lib/active_support/core_ext/class/attribute.rb +++ b/activesupport/lib/active_support/core_ext/class/attribute.rb @@ -1,4 +1,4 @@ -require 'active_support/core_ext/object/metaclass' +require 'active_support/core_ext/object/singleton_class' require 'active_support/core_ext/module/delegation' class Class @@ -25,11 +25,12 @@ class Class # # Subclass.setting? # => false def class_attribute(*attrs) + s = singleton_class attrs.each do |attr| - metaclass.send(:define_method, attr) { } - metaclass.send(:define_method, "#{attr}?") { !!send(attr) } - metaclass.send(:define_method, "#{attr}=") do |value| - metaclass.send(:define_method, attr) { value } + s.send(:define_method, attr) { } + s.send(:define_method, "#{attr}?") { !!send(attr) } + s.send(:define_method, "#{attr}=") do |value| + singleton_class.send(:define_method, attr) { value } end end end diff --git a/activesupport/lib/active_support/core_ext/class/delegating_attributes.rb b/activesupport/lib/active_support/core_ext/class/delegating_attributes.rb index 19382abb76..b5785bdcd3 100644 --- a/activesupport/lib/active_support/core_ext/class/delegating_attributes.rb +++ b/activesupport/lib/active_support/core_ext/class/delegating_attributes.rb @@ -1,6 +1,6 @@ require 'active_support/core_ext/object/blank' require 'active_support/core_ext/array/extract_options' -require 'active_support/core_ext/object/metaclass' +require 'active_support/core_ext/object/singleton_class' class Class def superclass_delegating_accessor(name, options = {}) @@ -11,9 +11,9 @@ class Class # Generate the public methods name, name=, and name? # These methods dispatch to the private _name, and _name= methods, making them # overridable - metaclass.send(:define_method, name) { send("_#{name}") } - metaclass.send(:define_method, "#{name}?") { !!send("_#{name}") } - metaclass.send(:define_method, "#{name}=") { |value| send("_#{name}=", value) } + singleton_class.send(:define_method, name) { send("_#{name}") } + singleton_class.send(:define_method, "#{name}?") { !!send("_#{name}") } + singleton_class.send(:define_method, "#{name}=") { |value| send("_#{name}=", value) } # If an instance_reader is needed, generate methods for name and name= on the # class itself, so instances will be able to see them @@ -27,12 +27,12 @@ private # inheritance behavior, without having to store the object in an instance # variable and look up the superclass chain manually. def _stash_object_in_method(object, method, instance_reader = true) - metaclass.send(:define_method, method) { object } + singleton_class.send(:define_method, method) { object } define_method(method) { object } if instance_reader end def _superclass_delegating_accessor(name, options = {}) - metaclass.send(:define_method, "#{name}=") do |value| + singleton_class.send(:define_method, "#{name}=") do |value| _stash_object_in_method(value, name, options[:instance_reader] != false) end self.send("#{name}=", nil) diff --git a/activesupport/lib/active_support/core_ext/object.rb b/activesupport/lib/active_support/core_ext/object.rb index db2dac1472..9dc6ca66a4 100644 --- a/activesupport/lib/active_support/core_ext/object.rb +++ b/activesupport/lib/active_support/core_ext/object.rb @@ -6,6 +6,7 @@ require 'active_support/core_ext/object/try' require 'active_support/core_ext/object/conversions' require 'active_support/core_ext/object/instance_variables' require 'active_support/core_ext/object/metaclass' +require 'active_support/core_ext/object/singleton_class' require 'active_support/core_ext/object/misc' require 'active_support/core_ext/object/extending' diff --git a/activesupport/lib/active_support/core_ext/object/metaclass.rb b/activesupport/lib/active_support/core_ext/object/metaclass.rb index 93fb0ad594..4b36a243c9 100644 --- a/activesupport/lib/active_support/core_ext/object/metaclass.rb +++ b/activesupport/lib/active_support/core_ext/object/metaclass.rb @@ -1,13 +1,14 @@ +require 'active_support/deprecation' + class Object - # Get object's meta (ghost, eigenclass, singleton) class + # Get object's meta (ghost, eigenclass, singleton) class. + # + # Deprecated in favor of Object#singleton_class. def metaclass class << self self end end - # If class_eval is called on an object, add those methods to its metaclass - def class_eval(*args, &block) - metaclass.class_eval(*args, &block) - end + deprecate :metaclass => :singleton_class end diff --git a/activesupport/lib/active_support/memoizable.rb b/activesupport/lib/active_support/memoizable.rb index f810f53029..ca1cfedae3 100644 --- a/activesupport/lib/active_support/memoizable.rb +++ b/activesupport/lib/active_support/memoizable.rb @@ -1,4 +1,4 @@ -require 'active_support/core_ext/object/metaclass' +require 'active_support/core_ext/object/singleton_class' require 'active_support/core_ext/module/aliasing' module ActiveSupport diff --git a/activesupport/test/core_ext/object_and_class_ext_test.rb b/activesupport/test/core_ext/object_and_class_ext_test.rb index f31e7774e9..00c59d84b8 100644 --- a/activesupport/test/core_ext/object_and_class_ext_test.rb +++ b/activesupport/test/core_ext/object_and_class_ext_test.rb @@ -89,7 +89,7 @@ class ClassExtTest < Test::Unit::TestCase end end -class ObjectTests < Test::Unit::TestCase +class ObjectTests < ActiveSupport::TestCase class DuckTime def acts_like_time? true @@ -119,12 +119,16 @@ class ObjectTests < Test::Unit::TestCase assert !duck.acts_like?(:date) end - def test_metaclass - string = "Hello" - string.metaclass.instance_eval do - define_method(:foo) { "bar" } + def test_singleton_class + o = Object.new + assert_equal class << o; self end, o.singleton_class + end + + def test_metaclass_deprecated + o = Object.new + assert_deprecated /use singleton_class instead/ do + assert_equal o.singleton_class, o.metaclass end - assert_equal "bar", string.foo end end diff --git a/railties/lib/rails/generators.rb b/railties/lib/rails/generators.rb index c01018aab2..f24dc620de 100644 --- a/railties/lib/rails/generators.rb +++ b/railties/lib/rails/generators.rb @@ -3,7 +3,7 @@ $:.unshift(activesupport_path) if File.directory?(activesupport_path) && !$:.inc require 'active_support' require 'active_support/core_ext/object/blank' -require 'active_support/core_ext/object/metaclass' +require 'active_support/core_ext/object/singleton_class' require 'active_support/core_ext/array/extract_options' require 'active_support/core_ext/hash/deep_merge' require 'active_support/core_ext/module/attribute_accessors' @@ -291,4 +291,4 @@ end # If the application was already defined, configure generators, # otherwise you have to configure it by hand. -Rails::Generators.configure! if Rails.respond_to?(:application) && Rails.application
\ No newline at end of file +Rails::Generators.configure! if Rails.respond_to?(:application) && Rails.application |