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.4.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.
-
.max(versions) ⇒ SemanticVersion?
Return the highest version from an array of versions.
-
.min(versions) ⇒ SemanticVersion?
Return the lowest version from an array of versions.
-
.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
224 225 226 |
# File 'lib/philiprehberger/version_compare.rb', line 224 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).
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 |
# File 'lib/philiprehberger/version_compare.rb', line 240 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
195 196 197 198 199 |
# File 'lib/philiprehberger/version_compare.rb', line 195 def self.latest(versions) raise Error, 'no versions provided' if versions.empty? versions.max_by { |v| parse(v) } end |
.max(versions) ⇒ SemanticVersion?
Return the highest version from an array of versions
214 215 216 217 |
# File 'lib/philiprehberger/version_compare.rb', line 214 def self.max(versions) parsed = versions.map { |v| v.is_a?(SemanticVersion) ? v : parse(v) } parsed.max end |
.min(versions) ⇒ SemanticVersion?
Return the lowest version from an array of versions
205 206 207 208 |
# File 'lib/philiprehberger/version_compare.rb', line 205 def self.min(versions) parsed = versions.map { |v| v.is_a?(SemanticVersion) ? v : parse(v) } parsed.min end |
.parse(str) ⇒ SemanticVersion
Parse a version string into a SemanticVersion
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
# File 'lib/philiprehberger/version_compare.rb', line 166 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
186 187 188 |
# File 'lib/philiprehberger/version_compare.rb', line 186 def self.sort(versions) versions.sort_by { |v| parse(v) } end |