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

发布于 2024-11-24 01:06:20 字数 517 浏览 2 评论 0原文

我 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

要走就滚别墨迹 2024-12-01 01:06:20

当您重新定义常量时,您需要先删除旧常量,然后重新应用新常量。您的补丁可能如下所示:

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

  # ... (etc) ...
end

这应该有助于避免有关重新定义现有常量的警告。

至于使其成为 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:

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

  # ... (etc) ...
end

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, perhaps longurl_monkeypatch.rb so there's no confusion as to what kind of hackery is going on.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文