1

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.

2 Answers 2

3

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 
Sign up to request clarification or add additional context in comments.

7 Comments

I'm getting undefined method 'login' for class 'Module' on the alias_method_chain line. Thanks.
Are you sure then login method is defined within ThirdParty login and not within one of the modules included within it?
At this point I'm not sure. The code I'm trying to override is this: github.com/topac/rmega/blob/master/lib/rmega/session.rb#L7
So ThirdParty is really Rmega?
yes, I didn't specify the lib name in question because I thought it would not be relevant :) Thanks.
|
1

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 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.