Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dependency is not registered when namespace is non-standard named #20

Open
deepj opened this issue Apr 19, 2020 · 7 comments
Open

Dependency is not registered when namespace is non-standard named #20

deepj opened this issue Apr 19, 2020 · 7 comments
Labels
bug Something isn't working
Milestone

Comments

@deepj
Copy link
Contributor

deepj commented Apr 19, 2020

Describe the bug

I have a namespace which looks like (example) CloudNATS. This is a namespace of Rails application declared in config/application.rb as well.

When I tried to import a dependecy, I got

Dry::Container::Error (Nothing registered with the key :http)

To Reproduce

# app/operations/create_user
class CreateUser
  include CloudNATS::Import[:http]
end
# lib/cloudnats/http.rb
module CloudNATS
  class HTTP
  end
end

Expected behavior

The dependency is successfully imported without any exception. My namespace would

Your environment

  • Affects my production application: NO
  • Ruby version: 2.7.1
  • OS: macOS
@deepj deepj added the bug Something isn't working label Apr 19, 2020
@deepj deepj changed the title Dependency is not registreted when namespace is non-standart named Dependency is not registered when namespace is non-standart named Apr 19, 2020
@solnic
Copy link
Member

solnic commented Apr 20, 2020

This is expected since it's not a regular word that includes an acronym that you need to register in your inflector. I'm not sure if registering just the acronym or the entire word will work, you need to verify that yourself.

@deepj
Copy link
Contributor Author

deepj commented Apr 20, 2020

The acronym is declared in config/initializers/inflections.rb 🤧

@solnic
Copy link
Member

solnic commented Apr 20, 2020

@deepj yeah but the path to files does not match the constant name, lib/cloudnats/http.rb should be lib/cloud_nats/http.rb. Whatever you do, you need to make sure that the inflector correctly converts the dir names into constant names.

@deepj
Copy link
Contributor Author

deepj commented Apr 21, 2020

@solnic The problem is, at least in my understanding the situation, the Rails' Inflector returns cloudnats for CloudNATS

Loading development environment (Rails 6.0.2.2)
irb(main):001:0> 'CloudNATS'.underscore
=> "cloudnats"

I tried lib/cloud_nats/http.rb as well, but without any success :(

NOTE: The naming of the app was chosen a few years ago btw :)

@deepj
Copy link
Contributor Author

deepj commented Apr 21, 2020

I know why it's happening 🙀

Screenshot 2020-04-21 at 10 52 09

I guess I would update documentation because it's taken from dry-rails documentation 😹

Suggestion, dry-rails may would expose Zeitwerk inflection API. Because after solving the above fix, I'm getting

expected file ~/lib/cloudnats/http.rb to define constant CloudNATS::Http, but didn't (Zeitwerk::NameError). 

And this is dangerous to set in Rails Inflector because it can affect 3rd-party gems which can use HTTP acronym in their names. It's safer to use Zeitwerk inflection rather than Rails one.

@solnic
Copy link
Member

solnic commented Apr 21, 2020

Suggestion, dry-rails may would expose Zeitwerk inflection API

yeah I've thought about it. In general I would like to make dry-rails zeitwerk-only eventually, because it's a much better solution than whatever old Rails is doing.

@deepj deepj changed the title Dependency is not registered when namespace is non-standart named Dependency is not registered when namespace is non-standard named Apr 21, 2020
@solnic
Copy link
Member

solnic commented Apr 22, 2020

And this is dangerous to set in Rails Inflector because it can affect 3rd-party gems which can use HTTP acronym in their names. It's safer to use Zeitwerk inflection rather than Rails one.

I should add that you can (and in this case should) set up a custom inflector and configure it to your needs. It's a matter of doing config.inflector = your_inflector

@solnic solnic added this to the 0.2.0 milestone Jun 15, 2020
@solnic solnic modified the milestones: 0.2.0, 0.3.0 Jul 21, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants