VersionGem
Give your next library an introspectable Version module.
MyLib::Version.to_s # => "1.2.3.rc3"
MyLib::Version.major # => 1
MyLib::Version.minor # => 2
MyLib::Version.patch # => 3
MyLib::Version.pre # => "rc3"
MyLib::Version.to_a # => [1, 2, 3, "rc3"]
MyLib::Version.to_h # => { major: 1, minor: 2, patch: 3, pre: "rc3" }
This library was extracted from the gem oauth2.
This gem has no runtime dependencies.
| Project | bundle add version_gem | |
|---|---|---|
| 1️⃣ | name, license, docs, standards | |
| 2️⃣ | version & activity | |
| 3️⃣ | maintanence & linting | |
| 4️⃣ | testing | |
| 5️⃣ | coverage & security | |
| 6️⃣ | resources | |
| 7️⃣ | spread 💖 |
Installation
Install the gem and add to the application's Gemfile by executing:
$ bundle add version_gem
If bundler is not being used to manage dependencies, install the gem by executing:
$ gem install version_gem
Usage
In the standard bundle gem my_lib code you get the following in lib/my_lib/version.rb:
module MyLib
VERSION = "0.1.0"
end
Change it to a nested Version namespace (the one implied by the path => namespace convention):
module MyLib
module Version
VERSION = "0.1.0"
end
end
Now add the following near the top of the file the manages requiring external libraries.
Using the same example of bundle gem my_lib, this would be lib/my_lib.rb.
require "version_gem"
Then, add the following wherever you want in the same file (recommend the bottom).
MyLib::Version.class_eval do
extend VersionGem::Basic
end
And now you have some version introspection methods available:
MyLib::Version.to_s # => "0.1.0"
MyLib::Version.major # => 0
MyLib::Version.minor # => 1
MyLib::Version.patch # => 0
MyLib::Version.pre # => ""
MyLib::Version.to_a # => [0, 1, 0]
MyLib::Version.to_h # => { major: 0, minor: 1, patch: 0, pre: "" }
Side benefit
Your version.rb file now abides the Ruby convention of directory / path matching the namespace / class!
Caveat
This design keeps your version.rb file compatible with the way gemspec files use them.
This means that the introspection is not available within the gemspec.
The enhancement from this gem is only available at runtime.
RSpec Matchers
In spec_helper.rb:
require 'version_gem/rspec'
Then you can write a test like:
RSpec.describe MyLib::Version do
it_behaves_like 'a Version module', described_class
end
# Or, if you want to write your own, here is the a la carte menu:
RSpec.describe MyLib::Version do
it "is a Version module" do
expect(described_class).is_a?(Module)
expect(described_class).to have_version_constant
expect(described_class).to have_version_as_string
expect(described_class.to_s).to be_a(String)
expect(described_class).to have_major_as_integer
expect(described_class).to have_minor_as_integer
expect(described_class).to have_patch_as_integer
expect(described_class).to have_pre_as_nil_or_string
expect(described_class.to_h.keys).to match_array(%i[major minor patch pre])
expect(described_class.to_a).to be_a(Array)
end
end
Development
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.
Contributing
See CONTRIBUTING.md
Contributors
Made with contributors-img.
License
The gem is available as open source under the terms of
the MIT License .
See LICENSE for the official Copyright Notice.
- Copyright (c) 2022 Peter H. Boling of Rails Bling
Code of Conduct
Everyone interacting in the VersionGem project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.
Versioning
This library aims to adhere to Semantic Versioning 2.0.0. Violations of this scheme should be reported as bugs. Specifically, if a minor or patch version is released that breaks backward compatibility, a new version should be immediately released that restores compatibility. Breaking changes to the public API will only be introduced with new major versions.
As a result of this policy, you can (and should) specify a dependency on this gem using the Pessimistic Version Constraint with two digits of precision.
For example:
spec.add_dependency "version_gem", "~> 1.1"
Security
See SECURITY.md.