ConstantResolver
resolves partially qualified constant reference to the fully qualified name and the path of the file defining it. It does not load the files to do that, its inference engine purely works on file paths and constant names.
ConstantResolver
uses the same assumptions as Rails' code loader, Zeitwerk
to infer constant locations. Please see Zeitwerk's documentation on file structure and inflection for more information.
Add this line to your application's Gemfile:
gem 'constant_resolver'
And then execute:
$ bundle
Or install it yourself as:
$ gem install constant_resolver
Initialize a ConstantResolver
with a root path and load paths:
resolver = ConstantResolver.new(
root_path: "/app",
load_paths: [
"/app/models",
"/app/services",
]
)
Resolve a constant from the contents of your load paths:
context = resolver.resolve("Some::Nested::Model")
context.name # => "::Some::Nested::Model"
context.location # => "models/some/nested/model.rb"
After checking out the repo, run bundle
to install dependencies. Then, run rake test
to run the tests.
Bug reports and pull requests are welcome on GitHub at https://github.com/Shopify/constant_resolver.
The gem is available as open source under the terms of the MIT License.