Class: SemVer
- Inherits:
-
Object
- Object
- SemVer
- Defined in:
- lib/ratatui_ruby/devtools/tasks/bump/sem_ver.rb
Overview
Represents a semantic version number.
Version numbers have three segments: major, minor, patch. Incrementing one resets the following segments to zero. Parsing strings by hand is tedious.
This class parses version strings and computes the next version for any segment. It follows the Semantic Versioning 2.0.0 specification.
Use it to bump versions in rake tasks.
Example
SemVer.parse("1.2.3").next(:minor).to_s # => "1.3.0"
Constant Summary collapse
- SEGMENTS =
The valid segment names for version bumping.
[:major, :minor, :patch].freeze
Class Method Summary collapse
-
.parse(string) ⇒ Object
Parses a version string into a SemVer instance.
Instance Method Summary collapse
-
#initialize(segments) ⇒ SemVer
constructor
Creates a new SemVer from an array of segments.
-
#next(segment) ⇒ Object
Returns the next version after bumping the given segment.
-
#to_s ⇒ Object
Returns the version as a string.
Constructor Details
#initialize(segments) ⇒ SemVer
Creates a new SemVer from an array of segments.
- segments
-
An array of three integers:
[major, minor, patch].
39 40 41 |
# File 'lib/ratatui_ruby/devtools/tasks/bump/sem_ver.rb', line 39 def initialize(segments) @segments = segments end |
Class Method Details
.parse(string) ⇒ Object
Parses a version string into a SemVer instance.
- string
-
A version string like
"1.2.3".
30 31 32 33 34 |
# File 'lib/ratatui_ruby/devtools/tasks/bump/sem_ver.rb', line 30 def self.parse(string) require "rubygems" segments = Gem::Version.new(string).segments.fill(0, 3).first(3) new(segments) end |
Instance Method Details
#next(segment) ⇒ Object
Returns the next version after bumping the given segment.
Bumping a segment resets all following segments to zero.
- segment
-
One of
:major,:minor, or:patch.
48 49 50 51 52 53 54 55 56 57 |
# File 'lib/ratatui_ruby/devtools/tasks/bump/sem_ver.rb', line 48 def next(segment) index = SEGMENTS.index(segment) raise ArgumentError, "Invalid segment: #{segment}" unless index new_segments = @segments.dup new_segments[index] += 1 new_segments.fill(0, (index + 1)..2) SemVer.new(new_segments) end |
#to_s ⇒ Object
Returns the version as a string.
60 61 62 |
# File 'lib/ratatui_ruby/devtools/tasks/bump/sem_ver.rb', line 60 def to_s @segments.join(".") end |