提交 d5bddc1b 编写于 作者: K Kir Shatrov

Use Module#prepend instead of alias_method_chain

Thanks @fbernier for suggestion! <3
At this moment we can use Module#prepend in all all cases
except of Range because of the bug [1] in MRI 2.2
[1] https://bugs.ruby-lang.org/issues/10847
上级 b19990c8
require 'active_support/core_ext/module/aliasing' module ActiveSupport
module MarshalWithAutoloading
module Marshal def load(source)
class << self super(source)
def load_with_autoloading(source)
load_without_autoloading(source)
rescue ArgumentError, NameError => exc rescue ArgumentError, NameError => exc
if exc.message.match(%r|undefined class/module (.+)|) if exc.message.match(%r|undefined class/module (.+)|)
# try loading the class/module # try loading the class/module
...@@ -15,7 +13,7 @@ def load_with_autoloading(source) ...@@ -15,7 +13,7 @@ def load_with_autoloading(source)
raise exc raise exc
end end
end end
alias_method_chain :load, :autoloading
end end
end end
Marshal.singleton_class.prepend(ActiveSupport::MarshalWithAutoloading)
...@@ -9,7 +9,6 @@ ...@@ -9,7 +9,6 @@
require 'active_support/core_ext/time/conversions' require 'active_support/core_ext/time/conversions'
require 'active_support/core_ext/date_time/conversions' require 'active_support/core_ext/date_time/conversions'
require 'active_support/core_ext/date/conversions' require 'active_support/core_ext/date/conversions'
require 'active_support/core_ext/module/aliasing'
# The JSON gem adds a few modules to Ruby core classes containing :to_json definition, overwriting # The JSON gem adds a few modules to Ruby core classes containing :to_json definition, overwriting
# their default behavior. That said, we need to define the basic to_json method in all of them, # their default behavior. That said, we need to define the basic to_json method in all of them,
...@@ -26,22 +25,27 @@ ...@@ -26,22 +25,27 @@
# bypassed completely. This means that as_json won't be invoked and the JSON gem will simply # bypassed completely. This means that as_json won't be invoked and the JSON gem will simply
# ignore any options it does not natively understand. This also means that ::JSON.{generate,dump} # ignore any options it does not natively understand. This also means that ::JSON.{generate,dump}
# should give exactly the same results with or without active support. # should give exactly the same results with or without active support.
[Object, Array, FalseClass, Float, Hash, Integer, NilClass, String, TrueClass, Enumerable].each do |klass|
klass.class_eval do module ActiveSupport
def to_json_with_active_support_encoder(options = nil) module CoreExt
if options.is_a?(::JSON::State) module ToJsonWithActiveSupportEncoder
# Called from JSON.{generate,dump}, forward it to JSON gem's to_json def to_json(options = nil)
self.to_json_without_active_support_encoder(options) if options.is_a?(::JSON::State)
else # Called from JSON.{generate,dump}, forward it to JSON gem's to_json
# to_json is being invoked directly, use ActiveSupport's encoder super(options)
ActiveSupport::JSON.encode(self, options) else
# to_json is being invoked directly, use ActiveSupport's encoder
ActiveSupport::JSON.encode(self, options)
end
end end
end end
alias_method_chain :to_json, :active_support_encoder
end end
end end
[Object, Array, FalseClass, Float, Hash, Integer, NilClass, String, TrueClass, Enumerable].reverse_each do |klass|
klass.prepend(ActiveSupport::CoreExt::ToJsonWithActiveSupportEncoder)
end
class Object class Object
def as_json(options = nil) #:nodoc: def as_json(options = nil) #:nodoc:
if respond_to?(:to_hash) if respond_to?(:to_hash)
......
...@@ -15,7 +15,7 @@ def teardown ...@@ -15,7 +15,7 @@ def teardown
sanity_data = ["test", [1, 2, 3], {a: [1, 2, 3]}, ActiveSupport::TestCase] sanity_data = ["test", [1, 2, 3], {a: [1, 2, 3]}, ActiveSupport::TestCase]
sanity_data.each do |obj| sanity_data.each do |obj|
dumped = Marshal.dump(obj) dumped = Marshal.dump(obj)
assert_equal Marshal.load_without_autoloading(dumped), Marshal.load(dumped) assert_equal Marshal.method(:load).super_method.call(dumped), Marshal.load(dumped)
end end
end end
...@@ -121,4 +121,4 @@ class SomeClass ...@@ -121,4 +121,4 @@ class SomeClass
end end
end end
end end
end end
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册