Rails gem类覆盖实现加载顺序

时间:2015-12-11 01:55:26

标签: ruby-on-rails ruby rubygems faye private-pub

我想替换我正在使用的gem中的特定ruby文件。

我使用的gem是private_pub:https://github.com/ryanb/private_pub,我要替换的文件是:https://github.com/ryanb/private_pub/blob/master/lib/private_pub/faye_extension.rb

所以我决定做的是,在我的rails项目中,在config / initializers中添加一个名为faye_extension.rb的新文件,其中包含以下代码:

PrivatePub::FayeExtension
puts "Overide loaded"
module PrivatePub
  # This class is an extension for the Faye::RackAdapter.
  # It is used inside of PrivatePub.faye_app.
  class FayeExtension
    # Callback to handle incoming Faye messages. This authenticates both
    # subscribe and publish calls.
    def incoming(message, callback)
    puts "MY MEGA CHANGE"
    logger.debug "inside incoming override"
      if message["channel"] == "/meta/subscribe"
           puts "subscription request received"
        authenticate_subscribe(message)
      elsif message["channel"] !~ %r{^/meta/}
        puts "publish request received"
        authenticate_publish(message)
      end
      callback.call(message)
    end

  private

    # Ensure the subscription signature is correct and that it has not expired.
    def authenticate_subscribe(message)
      subscription = PrivatePub.subscription(:channel => message["subscription"], :timestamp => message["ext"]["private_pub_timestamp"])
      if message["ext"]["private_pub_signature"] != subscription[:signature]
        message["error"] = "Incorrect signature."
      elsif PrivatePub.signature_expired? message["ext"]["private_pub_timestamp"].to_i
        message["error"] = "Signature has expired."
      end
    end

    # Ensures the secret token is correct before publishing.
    def authenticate_publish(message)
      if PrivatePub.config[:secret_token].nil?
        raise Error, "No secret_token config set, ensure private_pub.yml is loaded properly."
      elsif message["ext"]["private_pub_token"] != PrivatePub.config[:secret_token]
        message["error"] = "Incorrect token."
      else
        message["ext"]["private_pub_token"] = nil
      end
    end
  end
end

我在顶部包含'PrivatePub :: FayeExtension'的原因是强制自动加载,因此FayeExtension类将覆盖gem的实现。但是,当我执行驱动此逻辑的代码路径时,我在日志中没有显示任何添加的puts语句。重要的是要提到我确实看到'Overide loaded'然而我相信这种情况会发生,然后在某种程度上,使用gem的实现。这让我相信正在使用gem的faye_extension.rb而不是我的版本。

如何强制使用我的版本而不是gem的版本?

0 个答案:

没有答案