Ruby on rails 重写模块类方法
我试图重写第三方提供的模块中的类方法 我的目标是检查第二个参数,如果它是散列,则实例化一个新的自定义对象,否则调用原始实现:Ruby on rails 重写模块类方法,ruby-on-rails,ruby,Ruby On Rails,Ruby,我试图重写第三方提供的模块中的类方法 我的目标是检查第二个参数,如果它是散列,则实例化一个新的自定义对象,否则调用原始实现: module ThirdParty def self.login(email, password_or_options) if password_or_options.is_a?(Hash) SafeSession.new(email, password_or_options['sid'], password_or_options['master_
module ThirdParty
def self.login(email, password_or_options)
if password_or_options.is_a?(Hash)
SafeSession.new(email, password_or_options['sid'], password_or_options['master_key'], password_or_options['shared_keys'], password_or_options['rsa_privk']).storage
else
super(email, password_or_options)
end
end
原始方法签名:
module ThirdParty
def self.login(email, password)
# Library doing its own stuff
end
end
目前,这是失败的
ThirdParty.login('email', { test: true })
NoMethodError: super: no superclass method `login' for ThirdParty:Module
我也在使用ActiveSupport,以防在此框架中有此问题的解决方案。尝试:
module ThirdParty
class << self
def login_with_change(email, password_or_options)
if password_or_options.is_a?(Hash)
SafeSession.new(email, options['sid'], password_or_options['master_key'], password_or_options['shared_keys'], password_or_options['rsa_privk']).storage
else
login_without_change(email, password_or_options)
end
end
alias_method_chain :login, :change
end
end
模块第三方
类尝试:
模块第三方
class也许这会奏效
使用alias_方法在monkey_补丁模块中对原始方法进行别名
module ThirdParty
class << self
alias_method :login_ori, :login
end
def self.login(email, password_or_options)
if password_or_options.is_a?(Hash)
SafeSession.new(email, password_or_options['sid'], password_or_options['master_key'], password_or_options['shared_keys'], password_or_options['rsa_privk']).storage
else
login_ori(email, password_or_options)
end
end
end
模块第三方
class也许这会奏效
使用alias_方法在monkey_补丁模块中对原始方法进行别名
module ThirdParty
class << self
alias_method :login_ori, :login
end
def self.login(email, password_or_options)
if password_or_options.is_a?(Hash)
SafeSession.new(email, password_or_options['sid'], password_or_options['master_key'], password_or_options['shared_keys'], password_or_options['rsa_privk']).storage
else
login_ori(email, password_or_options)
end
end
end
模块第三方
类我在alias\u method\u chain
行中获得类“Module”的未定义方法“login”。谢谢。你确定第三方登录中定义了login
方法,而不是其中包含的模块吗?目前我不确定。我试图覆盖的代码是这样的:ThirdParty
真的是Rmega
?是的,我没有指定有问题的库名,因为我认为它不相关:)谢谢。我在alias\u method\u chain
行中得到了类“Module”的未定义方法“login”。谢谢。你确定第三方登录中定义了login
方法,而不是其中包含的模块吗?目前我不确定。我试图覆盖的代码如下:ThirdParty
实际上是Rmega
?是的,我没有指定有问题的库名,因为我认为它不相关:)谢谢。