如何覆盖gem内部模块内的类方法?

时间:2013-01-21 16:36:39

标签: ruby-on-rails module omniauth saml

我正在尝试将救援声明添加到其他失败的宝石中。麻烦的是我没有正确地覆盖原始gem的方法。我怎么能做到这一点?

原创宝石

module OmniAuth
  class Configuration
    include Singleton
      def add_camelization(name, camelized)
        self.camelizations[name.to_s] = camelized.to_s
      end

初始化/ omniauth.rb

module OmniAuth
  class Configuration
    def add_camelization(name, camelized)
      begin
        self.camelizations[name.to_s] = camelized.to_s
      rescue
        puts "No camelization for #{camelized}"
      end
      # ^ This rescue statement is not being called to replace the original gem's method.
    end
  end
end

1 个答案:

答案 0 :(得分:7)

尝试

OmniAuth::Configuration.class_eval do
    def add_camelization(name, camelized)
        ...
    end
end