RBS::Trace automatically collects argument and return types and saves RBS type declarations as RBS files or comments.
Install the gem and add to the application's Gemfile by executing:
$ bundle add rbs-trace
If bundler is not being used to manage dependencies, install the gem by executing:
$ gem install rbs-trace
For example, suppose you have the following class:
class User
def initialize(first_name, last_name)
@first_name = first_name
@last_name = last_name
end
def full_name
"#{@first_name} #{@last_name}"
end
def say_hello
puts "hi, #{full_name}."
end
end
Call target methods within the enable
method block, and call the save_comments
method.
trace = RBS::Trace.new
# Collects the types of methods called in the block.
trace.enable do
user = User.new("Nanoha", "Takamachi")
user.say_hello
end
# Save RBS type declarations as embedded comments
trace.save_comments
Automatically insert comments into the file.
class User
# @rbs (String, String) -> void
def initialize(first_name, last_name)
@first_name = first_name
@last_name = last_name
end
# @rbs () -> String
def full_name
"#{@first_name} #{@last_name}"
end
# @rbs () -> void
def say_hello
puts "hi, #{full_name}."
end
end
Add the following code to spec/support/rbs_trace.rb
.
RSpec.configure do |config|
trace = RBS::Trace.new
config.before(:suite) { trace.enable }
config.after(:suite) do
trace.disable
trace.save_comments
end
end
Add the following code to test_helper.rb
.
trace = RBS::Trace.new
trace.enable
Minitest.after_run do
trace.disable
trace.save_comments
end
trace.files.each do |path, file|
file.rewrite if path.include?("app/models/")
end
trace.save_files(out_dir: "sig/trace/")
If you are using a parallel testing gem such as parallel_tests or flatware, first save the type definitions in RBS files.
trace.save_files(out_dir: "tmp/sig-#{ENV["TEST_ENV_NUMBER"]}")
Then use rbs-trace merge
to merge multiple RBS files into one.
$ rbs-trace merge --sig-dir='tmp/sig-*' > sig/merged.rbs
Finally, insert comments using the merged RBS files.
$ rbs-trace inline --rb-dir=app --rb-dir=lib
If you want to enable debug logging, specify the environment variable RBS_TRACE_DEBUG
.
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 the created tag, and push the .gem
file to rubygems.org.
Bug reports and pull requests are welcome on GitHub at https://github.com/sinsoku/rbs-trace. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the code of conduct.
The gem is available as open source under the terms of the MIT License.
Everyone interacting in the RBS::Trace project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.