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
Public API that resolves constants for a given filename #270
Comments
Yes, this makes sense. To be coherent with the existing API, this method would return a string with a constant path, "MyGem::Fizz::Buzz", and could accept also paths that do not end in ".rb" (for namespaces). You'd just split by "::" if you need the segments. I guess the point is precisely that the file may not exist yet, so that should not be validated. There will be edge cases like, it is a descendant of an ignored directory, or it is under no root directory, ..., that kind of thing. But these are details. |
Maybe this needs the argument to exist, because if the path
does not exist, there's no way to know if that would be a file or a directory. If it was a file, there is no constant path expected there, because only (non-hidden) files with ".rb" extension are processed. That is, we'd return |
The only reason I can think of to return the namespaces for a directory would be passing in a value like |
Let me elaborate a bit more. The API I am drafting is
If we require the argument to exist, that is doable. But if we don't, then there is an edge case in virtual paths that do not have extensions, because
should return "Foo" if "foo" is a directory, and In the example in the description you |
Correct! In my example I'd |
It's in |
Just created I'm still refining the plugin. When you cut a release with the new Can't wait to use this for my Rails and Gem projects. |
*Now renamed to I checked this into a demo repo at https://github.com/rubymonolith/demo/blob/main/Guardfile. If anybody is interested in playing around with it, clone https://github.com/rubymonolith/demo and run |
It's out, version 2.6.9. The method got a final rename after sleeping on it and exchanging impressions with @matthewd, it's been published as |
I've started using Guard again for local development and found myself thinking it would be cool if I could run
touch ./lib/my-gem/fizz/buzz.rb
, open up the file, and see this:To accomplish that, I'd need some way to pass Zeitwerk a path,
./lib/my-gem/fizz/buzz.rb
in my example, and get back a list of constants such as[:MyGem, :Fizz, :Buzz]
that my Guard plugin could interpret to generate the output above.During my initial analysis of the Zeitwerk gem, I got to https://github.com//fxn/zeitwerk/blob/3a84e57bed93e6193851dd89042755264e516ed2/lib/zeitwerk/loader/callbacks.rb#L11-L14, and discovered these APIs are private and not as straight forward as passing it a path.
This ticket opens the discussion for such an API, per @fxn's request at https://twitter.com/fxn/status/1682078644955389952, to uncover other use cases and determine if its worth implementing & supporting.
The text was updated successfully, but these errors were encountered: