我目前有一个超类,它有一个函数,我希望所有子类在每个函数内调用.该函数应该像rails中的before_filter函数一样,但我不确定如何实现before_filter.这是一个例子
class Superclass
def before_each_method
puts "Before Method" #this is supposed to be invoked by each extending class' method
end
end
class Subclass < Superclass
def my_method
#when this method is called, before_each_method method is supposed to get invoked
end
end
Run Code Online (Sandbox Code Playgroud)
小智 11
这是一种方法:
class Superclass
def before_each_method name
p [:before_method, name]
end
def self.method_added name
return if @__last_methods_added && @__last_methods_added.include?(name)
with = :"#{name}_with_before_each_method"
without = :"#{name}_without_before_each_method"
@__last_methods_added = [name, with, without]
define_method with do |*args, &block|
before_each_method name
send without, *args, &block
end
alias_method without, name
alias_method name, with
@__last_methods_added = nil
end
end
class SubclassA < Superclass
def my_new_method
p :my_new_method
end
def my_new_other_method
p :my_new_other_method
end
end
SubclassA.new.my_new_method
SubclassA.new.my_new_other_method
Run Code Online (Sandbox Code Playgroud)
只要在子类中定义了要包装的方法,就会使用alias_method_chaining方法创建一个包装器方法.
这是我的解决方案:
require 'active_support/all'
module BeforeEach
extend ActiveSupport::Concern
module InstanceMethods
def before_each
raise NotImplementedError('Please define before_each method')
end
end
module ClassMethods
def method_added(method)
method = method.to_s.gsub(/_with(out)?_before$/, '')
with_method, without_method = "#{method}_with_before", "#{method}_without_before"
return if method == 'before_each' or method_defined?(with_method)
define_method(with_method) do |*args, &block|
before_each
send(without_method, *args, &block)
end
alias_method_chain(method, :before)
end
end
end
Run Code Online (Sandbox Code Playgroud)
要使用它,只需BeforeEach
在您的类中包含如下:
class Superclass
include BeforeEach
def before_each
puts "Before Method" #this is supposed to be invoked by each extending class' method
end
end
class Subclass < Superclass
def my_method
#when this method is called, before_each_method method is supposed to get invoked
end
end
Subclass.new.my_method
# => Before Method
Run Code Online (Sandbox Code Playgroud)
希望这对你有用!
归档时间: |
|
查看次数: |
6375 次 |
最近记录: |