How to rename a Ruby library library namespace?

I would like to use the ruby ​​Stripe library in a Rails application. It uses the Stripe module as its namespace.

I want to use Stripe as a namespace for my ActiveRecord models and rename the library module to something like StripeApi so that, for example, StripeApi::Charge refers to the Stripe library, but Stripe::Charge refers to my ActiveRecord model. Stripe -namespaced (so for example, Stripe::Charge.create(...) creates a database record, rather than just calling API calls).

Is there a good way to do this?

(Of course, I could rename my namespace or try using models with different names, but I find this kind of ugly.)

+2
source share
2 answers

I really recommend renaming your own namespace, as you have full control over the code. Otherwise, it can cause pain in the ass if you want to upgrade your Stripe gem version or find an error related to your namespace, as well as the original Stripe namespace.

It is much easier to change your own namespace instead of changing the existing gem (in the end for each version again).

+2
source

Ruby has no concept of a namespace. This is just a variable (well, constant):

 StripeApi = Stripe 

Boom. Everything is ready.

Be sure to install Stripe in a new module so that you do not accidentally reopen the module if you think you are creating a new one:

 Stripe = Module.new 

Now you can do

 class Stripe::Charge; end 
+2
source

Source: https://habr.com/ru/post/1275646/


All Articles