Backport helps you manage backported code with ease.
Add this line to your application's Gemfile:
gem 'backport'
And then execute:
$ bundle
Or install it yourself as:
$ gem install backport
Backport is used by registering checks and creating notices (which are only displayed if a certain check returns true). A check can be either static (when its result is defined when the check is defined) or dynamic (when its result is computed every time a notice is defined).
The following examples illustrates both types of checks:
# This is a dynamic check: it accepts an argument and determines
# whether the Rails' version is greater than or equal to the argument.
Backport.register_check(:rails_version_gte) do |version|
Rails.gem_version > Gem::Version.new(version)
end
# You can also define dynamic checks by passing a proc as the
# second argument to register_check.
Backport.register_check(
:rails_version_gte,
-> (version) { Rails.gem_version > Gem::Version.new(version) }
)
# This is a static check: its value is defined when the check is defined.
Backport.register_check(:rails5, Rails.gem_version >= Gem::Version.new('5.0.0'))
And this is how you use both types of checks:
def my_method
Backport.notify('This method is not needed in Rails 5', :rails5)
# ...
end
def my_other_method
Backport.notify('This method is not needed since Rails 5.1', :rails_version_gte, '5.1.0')
# ...
end
You can also use #notify!
instead of #notify
if you want to raise an exception rather than
using ActiveSupport's default deprecation behavior.
Backport also allows you to deprecate RSpec blocks, which is very useful when you backport both a piece of code and its tests.
To enable the RSpec integration, add the following to your spec_helper.rb
(after requiring RSpec):
require 'backport/rspec'
Now, you can do this:
RSpec.describe BackportedClass, backport: [
'This test is not needed as of 2.0',
:backported_library_gte,
'2.0'
] do
it 'some test here' do
# ...
end
end
If the backported_library_gte
check with 2.0
as its argument returns true
, an
ActiveSupport::DeprecationException
will be raised and the test will fail.
Note that the backport
tag accepts the same exact arguments as the #notify
method.
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/nebulab/backport. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.
The gem is available as open source under the terms of the MIT License.
Everyone interacting in the Backport project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.