Reputation: 1886
I'm trying to override a class method in a module that has been provided by a third party.
My goal is to check for the second argument, if it's a Hash then instantiate a new custom object, otherwise call the original implementation:
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
Original method signature:
module ThirdParty
def self.login(email, password)
# Library doing its own stuff
end
end
At the moment this is failing with
ThirdParty.login('email', { test: true })
NoMethodError: super: no superclass method `login' for ThirdParty:Module
I'm also using ActiveSupport, in case there's a solution to this problem in this framework.
Upvotes: 1
Views: 1395
Reputation: 21
Maybe this will work use alias_method to alias the original method in monkey_patched module
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
Upvotes: 1
Reputation: 44725
Try:
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
Upvotes: 3