Low commit activity in last 3 years
No release in over a year
Given a code base that adheres to certain conventions, ConstantResolver resolves any, even partially qualified, constant to the path of the file that defines it.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 5.0
~> 10.0
 Project Readme

ConstantResolver Build Status

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.

Installation

Add this line to your application's Gemfile:

gem 'constant_resolver'

And then execute:

$ bundle

Or install it yourself as:

$ gem install constant_resolver

Usage

Initialize the resolver

Initialize a ConstantResolver with a root path and load paths:

resolver = ConstantResolver.new(
  root_path: "/app",
  load_paths: [
    "/app/models",
    "/app/services",
  ]
)

Default namespaces

If any load paths have a default namespace other than Object, these can be specified in a hash:

resolver = ConstantResolver.new(
  root_path: "/app",
  load_paths: {
    "/app/models" => "::Object",
    "/app/services" => "::Object",
    "/app/internal" => "::Business",
  }
)

Resolve a constant

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"

Development

After checking out the repo, run bundle to install dependencies. Then, run rake test to run the tests.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/Shopify/constant_resolver.

License

The gem is available as open source under the terms of the MIT License.