Class: Philiprehberger::VersionCompare::SemanticVersion
- Inherits:
-
Object
- Object
- Philiprehberger::VersionCompare::SemanticVersion
- Includes:
- Comparable
- Defined in:
- lib/philiprehberger/version_compare.rb
Overview
A parsed semantic version with comparison support
Instance Attribute Summary collapse
-
#build_metadata ⇒ Object
readonly
Returns the value of attribute build_metadata.
-
#major ⇒ Object
readonly
Returns the value of attribute major.
-
#minor ⇒ Object
readonly
Returns the value of attribute minor.
-
#patch ⇒ Object
readonly
Returns the value of attribute patch.
-
#pre_release ⇒ Object
readonly
Returns the value of attribute pre_release.
Instance Method Summary collapse
-
#<=>(other) ⇒ Integer
Compare two versions following SemVer precedence.
-
#initialize(major, minor, patch, pre_release: nil, build_metadata: nil) ⇒ SemanticVersion
constructor
A new instance of SemanticVersion.
- #inspect ⇒ Object
-
#next_major ⇒ SemanticVersion
Return a new version with major incremented, minor and patch reset to 0.
-
#next_minor ⇒ SemanticVersion
Return a new version with minor incremented, patch reset to 0.
-
#next_patch ⇒ SemanticVersion
Return a new version with patch incremented.
-
#satisfies?(constraint) ⇒ Boolean
Check if this version satisfies a constraint string.
-
#stable? ⇒ Boolean
Check if the version is stable (no pre-release tag).
-
#to_a ⇒ Array<Integer>
Return the version components as an array.
-
#to_s ⇒ String
Version string representation.
Constructor Details
#initialize(major, minor, patch, pre_release: nil, build_metadata: nil) ⇒ SemanticVersion
Returns a new instance of SemanticVersion.
20 21 22 23 24 25 26 |
# File 'lib/philiprehberger/version_compare.rb', line 20 def initialize(major, minor, patch, pre_release: nil, build_metadata: nil) @major = major @minor = minor @patch = patch @pre_release = pre_release @build_metadata = end |
Instance Attribute Details
#build_metadata ⇒ Object (readonly)
Returns the value of attribute build_metadata.
13 14 15 |
# File 'lib/philiprehberger/version_compare.rb', line 13 def @build_metadata end |
#major ⇒ Object (readonly)
Returns the value of attribute major.
13 14 15 |
# File 'lib/philiprehberger/version_compare.rb', line 13 def major @major end |
#minor ⇒ Object (readonly)
Returns the value of attribute minor.
13 14 15 |
# File 'lib/philiprehberger/version_compare.rb', line 13 def minor @minor end |
#patch ⇒ Object (readonly)
Returns the value of attribute patch.
13 14 15 |
# File 'lib/philiprehberger/version_compare.rb', line 13 def patch @patch end |
#pre_release ⇒ Object (readonly)
Returns the value of attribute pre_release.
13 14 15 |
# File 'lib/philiprehberger/version_compare.rb', line 13 def pre_release @pre_release end |
Instance Method Details
#<=>(other) ⇒ Integer
Compare two versions following SemVer precedence
32 33 34 35 36 37 38 39 |
# File 'lib/philiprehberger/version_compare.rb', line 32 def <=>(other) return nil unless other.is_a?(SemanticVersion) result = [major, minor, patch] <=> [other.major, other.minor, other.patch] return result unless result.zero? compare_pre_release(other) end |
#inspect ⇒ Object
113 114 115 |
# File 'lib/philiprehberger/version_compare.rb', line 113 def inspect "#<Version #{self}>" end |
#next_major ⇒ SemanticVersion
Return a new version with major incremented, minor and patch reset to 0
73 74 75 |
# File 'lib/philiprehberger/version_compare.rb', line 73 def next_major SemanticVersion.new(major + 1, 0, 0) end |
#next_minor ⇒ SemanticVersion
Return a new version with minor incremented, patch reset to 0
80 81 82 |
# File 'lib/philiprehberger/version_compare.rb', line 80 def next_minor SemanticVersion.new(major, minor + 1, 0) end |
#next_patch ⇒ SemanticVersion
Return a new version with patch incremented
87 88 89 |
# File 'lib/philiprehberger/version_compare.rb', line 87 def next_patch SemanticVersion.new(major, minor, patch + 1) end |
#satisfies?(constraint) ⇒ Boolean
Check if this version satisfies a constraint string
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/philiprehberger/version_compare.rb', line 46 def satisfies?(constraint) operator, version_str = parse_constraint(constraint) other = VersionCompare.parse(version_str) case operator when '>=' self >= other when '<=' self <= other when '>' self > other when '<' self < other when '=' self == other when '!=' self != other when '~>' pessimistic_match?(other) else raise Error, "unknown operator: #{operator}" end end |
#stable? ⇒ Boolean
Check if the version is stable (no pre-release tag)
94 95 96 |
# File 'lib/philiprehberger/version_compare.rb', line 94 def stable? pre_release.nil? end |
#to_a ⇒ Array<Integer>
Return the version components as an array
101 102 103 |
# File 'lib/philiprehberger/version_compare.rb', line 101 def to_a [major, minor, patch] end |
#to_s ⇒ String
Returns version string representation.
106 107 108 109 110 111 |
# File 'lib/philiprehberger/version_compare.rb', line 106 def to_s base = "#{major}.#{minor}.#{patch}" base = "#{base}-#{pre_release}" if pre_release base = "#{base}+#{}" if base end |