我想写一点"Deprecate-It"lib并且经常使用"method_added"回调.但是现在我注意到,当包含模块时,不会触发此回调.
是否存在任何回调或变通办法,以便在将某些事物包括在内时通知"Foobar"课程?
小演示演示:
# Including Moduls won't trigger method_added callback
module InvisibleMethod
def invisible
"You won't get a callback from me"
end
end
class Foobar
def self.method_added(m)
puts "InstanceMethod: '#{m}' added to '#{self}'"
end
def visible
"You will get a callback from me"
end
include InvisibleMethod
end
[:invisible, :visible, :wont_exist].each do |meth|
puts "#{meth}: #{Foobar.public_method_defined? meth}"
end
Run Code Online (Sandbox Code Playgroud)
这就是结果:
InstanceMethod: 'visible' added to 'Foobar'
invisible: true
visible: true
wont_exist: false
Run Code Online (Sandbox Code Playgroud)
附加信息:
我真的需要使用像method_added这样的钩子.
ActiveModel通过匿名模块在运行时将class_instance_methods添加到Class.