Monkey(也许?)在我的 Rails 应用程序中修补 Gem
我 100% 确定这些术语,对于 Rails 世界来说仍然相对较新,所以请原谅,如果我与猴子补丁相差太远,这可能不适用于本例。
我正在使用 gem,LongURL,它可以延长缩短的 url。默认情况下,gem 使用 longurl.org,但我们每天通过它推送几十万个 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
这么小的改变似乎不值得分叉,有哪些好的、Rails 惯用的方法?进行这样的小改变的方法是什么?
谢谢
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.
I am using a gem, LongURL, that lengthens shortened urls. 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. I just need to change 2 constants to point to my own 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?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您重新定义常量时,您需要先删除旧常量,然后重新应用新常量。您的补丁可能如下所示:
这应该有助于避免有关重新定义现有常量的警告。
至于使其成为 Railsy,请将其放入
config/initializers
中,并确保它有明确的标签,也许是longurl_monkeypatch.rb
,这样就不会混淆将会发生什么样的黑客攻击在。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:
This should help avoid warnings about redefining an existing const.
As far as making it Railsy, put that into
config/initializers
and make sure it's clearly labelled, perhapslongurl_monkeypatch.rb
so there's no confusion as to what kind of hackery is going on.