RPlate is an opinionated tool to generate ruby classes and modules and related specs from predefined templates.
Its goal is to remove the need to manually create everytime those files and fill them with a given content, like class definition, namespace scoping, method definition and so on.
It is tested to work on ruby 2.4+ in ubuntu and macosx platforms.
To take full advantage of rplate features your naming conventions must follow the zeitwerk filestructure.
Good news to be mentioned is that from rails 6 zeitwerk gem is the default require engine and you are probably good to go :-)
The cli interface of rplate
expects a list of underscored entities
rplate generate my_module my_class
will:
- place the
Myclass
class inlib/my_module/my_class.rb
- place the related spec file in
spec/lib/my_module/my_class_spec.rb
- scope
MyClass
inside theMyNamespace
module
Once those files have been generated rplate
will run rubocop on them according to the styleguide in rplate .rubocop.yml
.
Currently it is not supported the use of a custom rubocop styleguide but this does not mean that you cannot run your local styleguide by your own ;-)
Rplate accepts some arguments. Check that by calling the help:
rplate help generate
That will result in:
Usage:
rplate generate [ENTITIES]
Options:
-t, [--type=TYPE] # `class` or `module`
# Default: class
-m, [--required-methods=one two three] # the class methods
-r, [--root=ROOT] # example: `app/controllers`
# Default: lib
-i, [--inflections=one two three] # example: -i rplate:RPlate api:API
generate a ruby entity with the given name
You can define what kind of type your new entity should be: class or module
rplate
allows you to pass the list of desired methods you would like the new entity be provided.
For example:
rplate generate my_class -m self.perform perform
will declare the 2 methods in the result entity and spec.
To be noted that when ?
method wants to be added, it needs to be passed within ''
.
For example.
rplate generate my_class -m 'valid?'
Sometime you want to place a new entity in a root dir which is not expected to be the root for an another entity.
This can be the case for example of app/controllers
for a rails application.
rplate
allows you to pass this root dir via -r
option.
Sometime you want to place an entity api http_client
and you want the result entities to inflect api
with API
and http
with HTTP
.
rplate
allows you to do that with the inflections:
rplate generate api http_client -i api:API http_client:HTTPClient
Enjoy !!!
After checking out the repo, run bin/setup
to install dependencies. Then, run rake spec
to run the tests. You can also run bin/console
for an interactive prompt that will allow you to experiment.
To install this gem onto your local machine, run bundle exec rake install
. To release a new version, update the version number in version.rb
, and then run bundle exec rake release
, which will create a git tag for the version, push git commits and tags, and push the .gem
file to rubygems.org.
Bug reports and pull requests are welcome on GitHub at https://github.com/nic-lan/rplate.
The gem is available as open source under the terms of the MIT License.