Class: NuGetVersions::SemanticVersion
- Inherits:
-
Object
- Object
- NuGetVersions::SemanticVersion
- Defined in:
- lib/nuget_versions/semantic_version.rb
Overview
A strict SemVer implementation Based on: NuGet.Client/NuGet.Core/NuGet.Versioning/SemanticVersion.cs from the NuGet source code.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#major ⇒ Object
Returns the value of attribute major.
-
#metadata ⇒ Object
Returns the value of attribute metadata.
-
#minor ⇒ Object
Returns the value of attribute minor.
-
#patch ⇒ Object
Returns the value of attribute patch.
-
#release_labels ⇒ Object
Returns the value of attribute release_labels.
Class Method Summary collapse
-
.copy_of(version) ⇒ Object
Creates a SemanticVersion from an existing SemanticVersion Parameters: - version: Version to clone.
- .parse(value) ⇒ Object
- .try_parse(value) ⇒ Object
Instance Method Summary collapse
- #!=(other) ⇒ Object
- #<(other) ⇒ Object
- #<=(other) ⇒ Object
- #<=>(other) ⇒ Object
- #==(other) ⇒ Object
- #>(other) ⇒ Object
- #>=(other) ⇒ Object
- #has_metadata? ⇒ Boolean
-
#initialize(major, minor, patch, release_labels = nil, metadata = nil) ⇒ SemanticVersion
constructor
Creates a SemanticVersion X.Y.Z, X.Y.Z-alpha, X.Y.Z-alpha+metadata.
- #is_prerelease? ⇒ Boolean
- #release ⇒ Object
- #release=(new_value) ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(major, minor, patch, release_labels = nil, metadata = nil) ⇒ SemanticVersion
Creates a SemanticVersion X.Y.Z, X.Y.Z-alpha, X.Y.Z-alpha+metadata. Parameters:
-
major: X.y.z
-
minor: x.Y.z
-
patch: x.y.Z
-
release_labels: Prerelease label
-
metadata: Build metadata
19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/nuget_versions/semantic_version.rb', line 19 def initialize(major, minor, patch, release_labels = nil, = nil) @major = Integer(major, major.is_a?(String) ? 10 : 0) @minor = Integer(minor, minor.is_a?(String) ? 10 : 0) @patch = Integer(patch, patch.is_a?(String) ? 10 : 0) if !release_labels.nil? release_labels = release_labels.to_s.split('.') if !release_labels.kind_of? Array SemanticVersion.validate_identifiers release_labels @release_labels = release_labels end SemanticVersion.validate_identifier if !.nil? @metadata = end |
Instance Attribute Details
#major ⇒ Object
Returns the value of attribute major.
34 35 36 |
# File 'lib/nuget_versions/semantic_version.rb', line 34 def major @major end |
#metadata ⇒ Object
Returns the value of attribute metadata.
38 39 40 |
# File 'lib/nuget_versions/semantic_version.rb', line 38 def @metadata end |
#minor ⇒ Object
Returns the value of attribute minor.
35 36 37 |
# File 'lib/nuget_versions/semantic_version.rb', line 35 def minor @minor end |
#patch ⇒ Object
Returns the value of attribute patch.
36 37 38 |
# File 'lib/nuget_versions/semantic_version.rb', line 36 def patch @patch end |
#release_labels ⇒ Object
Returns the value of attribute release_labels.
37 38 39 |
# File 'lib/nuget_versions/semantic_version.rb', line 37 def release_labels @release_labels end |
Class Method Details
.copy_of(version) ⇒ Object
Creates a SemanticVersion from an existing SemanticVersion Parameters:
-
version: Version to clone.
8 9 10 |
# File 'lib/nuget_versions/semantic_version.rb', line 8 def self.copy_of(version) return SemanticVersion.new(version.major, version.minor, version.patch, version.release_labels, version.) end |
.parse(value) ⇒ Object
121 122 123 124 125 126 |
# File 'lib/nuget_versions/semantic_version.rb', line 121 def self.parse(value) ver = self.try_parse(value) raise "Invalid semantic version value" if ver.nil? ver end |
.try_parse(value) ⇒ Object
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/nuget_versions/semantic_version.rb', line 128 def self.try_parse(value) return nil if value.nil? value = value.to_s if !value.is_a? String return nil if value.empty? value = value.split("+", 2) = (value.length == 2) ? value.last : nil value = value.first.split("-", 2) return nil if value.empty? release_labels = (value.length == 2) ? value.last.split('.') : nil parts = value.first.split(".") return nil if parts.length != 3 return nil if release_labels && !SemanticVersion.try_validate_identifiers(release_labels) return nil if && !SemanticVersion.try_validate_identifier() begin major = Integer(parts[0], 10) minor = (parts.length >= 2) ? Integer(parts[1], 10) : 0 patch = (parts.length >= 3) ? Integer(parts[2], 10) : 0 rescue return nil end return SemanticVersion.new(major, minor, patch, release_labels, ) end |
Instance Method Details
#!=(other) ⇒ Object
97 98 99 |
# File 'lib/nuget_versions/semantic_version.rb', line 97 def !=(other) !(self == other) end |
#<(other) ⇒ Object
101 102 103 |
# File 'lib/nuget_versions/semantic_version.rb', line 101 def <(other) VersionComparer.compare(self, other) == -1 end |
#<=(other) ⇒ Object
109 110 111 |
# File 'lib/nuget_versions/semantic_version.rb', line 109 def <=(other) !(other < self) end |
#<=>(other) ⇒ Object
117 118 119 |
# File 'lib/nuget_versions/semantic_version.rb', line 117 def <=>(other) VersionComparer.compare(self, other) end |
#==(other) ⇒ Object
93 94 95 |
# File 'lib/nuget_versions/semantic_version.rb', line 93 def ==(other) VersionComparer.compare(self, other) == 0 end |
#>(other) ⇒ Object
105 106 107 |
# File 'lib/nuget_versions/semantic_version.rb', line 105 def >(other) other < self end |
#>=(other) ⇒ Object
113 114 115 |
# File 'lib/nuget_versions/semantic_version.rb', line 113 def >=(other) !(self < other) end |
#has_metadata? ⇒ Boolean
81 82 83 |
# File 'lib/nuget_versions/semantic_version.rb', line 81 def !@metadata.nil? && !@metadata.empty? end |
#is_prerelease? ⇒ Boolean
77 78 79 |
# File 'lib/nuget_versions/semantic_version.rb', line 77 def is_prerelease? !@release_labels.nil? && !@release_labels.all?(&:empty?) end |
#release ⇒ Object
65 66 67 68 |
# File 'lib/nuget_versions/semantic_version.rb', line 65 def release return nil if @release_labels.nil? @release_labels.join('.') end |
#release=(new_value) ⇒ Object
70 71 72 73 74 75 |
# File 'lib/nuget_versions/semantic_version.rb', line 70 def release=(new_value) return @release_labels = nil if new_value.nil? new_value = new_value.split('.') SemanticVersion.validate_identifiers new_value @release_labels = new_value end |
#to_s ⇒ Object
85 86 87 88 89 90 91 |
# File 'lib/nuget_versions/semantic_version.rb', line 85 def to_s version = "#{@major}.#{@minor}.#{@patch}" version += "-#{release}" if is_prerelease? version += "+#{}" if version end |