Class: Semverve::SemanticVersion
- Inherits:
-
Object
- Object
- Semverve::SemanticVersion
- Includes:
- Comparable
- Defined in:
- lib/semverve/semantic_version.rb
Overview
Value object for a MAJOR.MINOR.PATCH semantic version.
Constant Summary collapse
- PATTERN =
Regular expression for Semverve's supported version format.
/\A(?<major>\d+)\.(?<minor>\d+)\.(?<patch>\d+)\z/
Instance Attribute Summary collapse
-
#major ⇒ Integer
readonly
Major version number.
-
#minor ⇒ Integer
readonly
Minor version number.
-
#patch ⇒ Integer
readonly
Patch version number.
Class Method Summary collapse
-
.parse(value) ⇒ Semverve::SemanticVersion
Parses a value into a semantic version.
Instance Method Summary collapse
-
#<=>(other) ⇒ Integer?
Compares semantic versions by major, minor, then patch.
-
#increment(level) ⇒ Semverve::SemanticVersion
Returns a new semantic version with the requested level incremented.
-
#initialize(major:, minor:, patch:) ⇒ Semverve::SemanticVersion
constructor
Initializes a semantic version.
-
#to_a ⇒ Array<Integer>
Version as
[major, minor, patch]. -
#to_s ⇒ String
Version as
MAJOR.MINOR.PATCH.
Constructor Details
#initialize(major:, minor:, patch:) ⇒ Semverve::SemanticVersion
Initializes a semantic version.
63 64 65 66 67 |
# File 'lib/semverve/semantic_version.rb', line 63 def initialize(major:, minor:, patch:) @major = major @minor = minor @patch = patch end |
Instance Attribute Details
#major ⇒ Integer (readonly)
Major version number.
21 22 23 |
# File 'lib/semverve/semantic_version.rb', line 21 def major @major end |
#minor ⇒ Integer (readonly)
Minor version number.
27 28 29 |
# File 'lib/semverve/semantic_version.rb', line 27 def minor @minor end |
#patch ⇒ Integer (readonly)
Patch version number.
33 34 35 |
# File 'lib/semverve/semantic_version.rb', line 33 def patch @patch end |
Class Method Details
.parse(value) ⇒ Semverve::SemanticVersion
Parses a value into a semantic version.
41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/semverve/semantic_version.rb', line 41 def self.parse(value) match = value.to_s.match(PATTERN) unless match raise Error, "Expected a semantic version in MAJOR.MINOR.PATCH format, got #{value.inspect}." end new( major: match[:major].to_i, minor: match[:minor].to_i, patch: match[:patch].to_i ) end |
Instance Method Details
#<=>(other) ⇒ Integer?
Compares semantic versions by major, minor, then patch.
94 95 96 97 98 |
# File 'lib/semverve/semantic_version.rb', line 94 def <=>(other) return unless other.is_a?(self.class) to_a <=> other.to_a end |
#increment(level) ⇒ Semverve::SemanticVersion
Returns a new semantic version with the requested level incremented.
75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/semverve/semantic_version.rb', line 75 def increment(level) case level.to_sym when :major self.class.new(major: major + 1, minor: 0, patch: 0) when :minor self.class.new(major: major, minor: minor + 1, patch: 0) when :patch self.class.new(major: major, minor: minor, patch: patch + 1) else raise Error, "Unknown version increment level: #{level.inspect}." end end |
#to_a ⇒ Array<Integer>
Version as [major, minor, patch].
112 113 114 |
# File 'lib/semverve/semantic_version.rb', line 112 def to_a [major, minor, patch] end |
#to_s ⇒ String
Version as MAJOR.MINOR.PATCH.
104 105 106 |
# File 'lib/semverve/semantic_version.rb', line 104 def to_s to_a.join(".") end |