如何重命名Ruby库模块的名称空间? [英] How can I rename a Ruby library module's namespace?
问题描述
我想在Rails应用程序中使用 ruby Stripe库.它使用模块Stripe
作为其命名空间.
I'd like to use the ruby Stripe library in a Rails app. It uses the module Stripe
as its namespace.
我想使用Stripe
作为我的ActiveRecord模型的命名空间,并将库模块重命名为StripeApi之类的名称,例如StripeApi::Charge
指的是Stripe库,但是Stripe::Charge
指的是我的Stripe
命名的ActiveRecord模型(因此,例如Stripe::Charge.create(...)
创建数据库记录,而不仅仅是进行API调用).
I want to use Stripe
as the namespace for my ActiveRecord models, and rename the library module to something like StripeApi, so that e.g. StripeApi::Charge
refers to the Stripe library, but Stripe::Charge
refers to my Stripe
-namespaced ActiveRecord model (so that e.g. Stripe::Charge.create(...)
creates a database record, rather than just making API calls).
有什么好方法吗?
(当然,我可以重命名我的名称空间,或尝试使用其他命名的模型,但是我觉得那有点丑陋.)
(Sure, I could rename my namespace, or try to use differently named models, but I find that kinda ugly.)
推荐答案
我真的建议重命名自己的名称空间,因为您可以完全控制代码. 否则,如果您想更新Stripe gem的版本或搜索与您的命名空间以及原始Stripe命名空间有关的Bug,那么可能真的很麻烦.
I really recommend to rename your own namespace since you have full control over the code. Otherwise it could really become a pain in the ass if you want to update the version of the Stripe gem or search for a Bug relating to your namespace as well to the original Stripe namespace.
更改自己的名称空间而不是更改现有的gem(最终再次针对每个版本)要容易得多.
It's a lot easier to change the own namespace instead of changing an existing gem (eventually for every version again).
这篇关于如何重命名Ruby库模块的名称空间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!