MarkOates / blast

0 stars 0 forks source link

Add RailsInflector #2

Closed MarkOates closed 6 years ago

MarkOates commented 6 years ago

Accessing Rails' Inflectors

Some internal projects require access to Rails' ActiveSupport::Inflectors. Particularly, if you're wanting to navigate a Rails codebase and you want a program to infer the parallel model, controller, and test filenames. Each of these filenames have different naming schemes that can not be trivially inferred. Thus, the #singularize and #pluralize methods provided by ActiveSupport::Inflectors are crucial.

Parallel Filenames for "Category"

For example, the pluralization and/or singularization of the model name Category would need to generate the following parallel filenames:

Type Class Name File Name
Model Category category.rb
Model test CategoryTest category_test.rb
Controller CategoriesController categories_controller.rb
Controller test CategoriesControllerTest categories_controller_test.rb

In practice, if you are currently viewing categories_controller_test.rb file and would like to infer the model filename, a singularization would be needed (after stripping off the _controller_test.rb suffix).

Access the Inflector

In order for RailsInflector to work, the underlying system will need to have ruby installed and accessible from the command line, and rails installed and available in the ruby gems path. Specifically, the active_support gem of Rails is required.

The RailsInflector works by running the ruby code through a shell command and retrieving the output as a string (by using ShellCommandExecutor). An alternative approach might be to include ruby.h and use the interpreter within C++, but this requires header and linker dependencies could be more cumbersome.