Module: Philiprehberger::VersionCompare
- Defined in:
- lib/philiprehberger/version_compare.rb,
lib/philiprehberger/version_compare/version.rb
Defined Under Namespace
Classes: Error, SemanticVersion
Constant Summary collapse
- VERSION =
'0.3.0'
Class Method Summary collapse
-
.filter(versions, constraint) ⇒ Array<String>
Filter an array of version strings by a constraint.
-
.highest_satisfying(versions, constraint) ⇒ String?
Return the highest version from an array that satisfies a constraint.
-
.latest(versions) ⇒ String
Return the latest version from an array of version strings.
-
.parse(str) ⇒ SemanticVersion
Parse a version string into a SemanticVersion.
-
.sort(versions) ⇒ Array<String>
Sort an array of version strings.
Class Method Details
.filter(versions, constraint) ⇒ Array<String>
Filter an array of version strings by a constraint
193 194 195 |
# File 'lib/philiprehberger/version_compare.rb', line 193 def self.filter(versions, constraint) versions.select { |v| parse(v).satisfies?(constraint) } end |
.highest_satisfying(versions, constraint) ⇒ String?
Return the highest version from an array that satisfies a constraint
Performs a single pass over versions, tracking the maximum parsed version whose parsed form satisfies the constraint. Pre-release and stable resolution follow the same precedence used by sort and latest (pre-release versions rank lower than their stable counterparts per SemVer).
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 |
# File 'lib/philiprehberger/version_compare.rb', line 209 def self.(versions, constraint) best_raw = nil best_parsed = nil versions.each do |v| parsed = parse(v) next unless parsed.satisfies?(constraint) if best_parsed.nil? || parsed > best_parsed best_parsed = parsed best_raw = v end end best_raw end |
.latest(versions) ⇒ String
Return the latest version from an array of version strings
182 183 184 185 186 |
# File 'lib/philiprehberger/version_compare.rb', line 182 def self.latest(versions) raise Error, 'no versions provided' if versions.empty? versions.max_by { |v| parse(v) } end |
.parse(str) ⇒ SemanticVersion
Parse a version string into a SemanticVersion
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/philiprehberger/version_compare.rb', line 153 def self.parse(str) str = str.to_s.strip str = str.sub(/\Av/, '') match = str.match(/\A(\d+)(?:\.(\d+))?(?:\.(\d+))?(?:-([a-zA-Z0-9.]+))?(?:\+([a-zA-Z0-9.]+))?\z/) raise Error, "invalid version: #{str}" unless match SemanticVersion.new( match[1].to_i, (match[2] || '0').to_i, (match[3] || '0').to_i, pre_release: match[4], build_metadata: match[5] ) end |
.sort(versions) ⇒ Array<String>
Sort an array of version strings
173 174 175 |
# File 'lib/philiprehberger/version_compare.rb', line 173 def self.sort(versions) versions.sort_by { |v| parse(v) } end |