1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Use Object#singleton_class instead of #metaclass. Prefer Ruby's choice.

This commit is contained in:
Jeremy Kemper 2010-02-25 09:32:29 -08:00
parent 6b12d74026
commit f7b0a857e9
14 changed files with 44 additions and 35 deletions

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

@ -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
Rails::Generators.configure! if Rails.respond_to?(:application) && Rails.application