简体   繁体   English

Monkey(也许?)在我的 Rails 应用程序中修补 Gem

[英]Monkey (maybe?) Patching a Gem In My Rails Apps

I'm 100% sure of the terminology, still relatively new to the rails world, so forgive that if I'm too far off with the monkey patch, that might not apply in this case.我 100% 确定术语,对于 Rails 世界来说仍然相对较新,所以请原谅,如果我对猴子补丁太远了,这可能不适用于这种情况。

I am using a gem, LongURL, that lengthens shortened urls.我正在使用一个 gem,LongURL,它可以延长缩短的 url。 By default the gem uses longurl.org, but we push a few hundred thousand urls through it a day and figured it'd be nicer for everyone to bring that service internally.默认情况下,gem 使用 longurl.org,但我们每天通过它推送几十万个 url,并且认为每个人都在内部提供该服务会更好。 I just need to change 2 constants to point to my own url.我只需要更改 2 个常量以指向我自己的 url。

module LongURL
  ShortURLMatchRegexp = /http:\/\/[\/\-_.a-z0-9]+/im

  # Urls for longurl
  EndPoint        = URI.parse("http://api.longurl.org/v1/expand")
  ServiceEndPoint = URI.parse("http://api.longurl.org/v1/services")
end

It doesn't seem like such a minor change is worthy of a fork, what are some good, rails idiomatic?, approaches to making minor changes like this?似乎这样一个微小的改变不值得分叉,有什么好的,rails 惯用的?,进行这样的微小改变的方法是什么?

Thanks谢谢

When you're redefining constants you'll need to remove the old ones first, then re-apply the new ones.当您重新定义常量时,您需要先删除旧常量,然后重新应用新常量。 Your patch might look like this:您的补丁可能如下所示:

module LongURL
  remove_const(:ShortURLMatchRegexp)
  ShortURLMatchRegexp = /http:\/\/[\/\-_.a-z0-9]+/im

  # ... (etc) ...
end

This should help avoid warnings about redefining an existing const.这应该有助于避免有关重新定义现有 const 的警告。

As far as making it Railsy, put that into config/initializers and make sure it's clearly labelled, perhaps longurl_monkeypatch.rb so there's no confusion as to what kind of hackery is going on.至于让它成为 Railsy,将它放入config/initializers并确保它被清楚地标记,也许是longurl_monkeypatch.rb ,这样就不会混淆正在发生什么样的黑客行为。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM