Module: Vers
- Defined in:
- lib/vers.rb,
lib/vers/parser.rb,
lib/vers/version.rb,
lib/vers/interval.rb,
lib/vers/constraint.rb,
lib/vers/maven_version.rb,
lib/vers/nuget_version.rb,
lib/vers/version_range.rb,
sig/vers.rbs
Overview
Vers - A Ruby gem for parsing, comparing and sorting versions according to the VERS spec
This gem provides tools for working with version ranges across different package managers, using a mathematical interval model internally and supporting the vers specification from the Package URL (PURL) project.
Features
- Parse version ranges from multiple package ecosystems (npm, gem, pypi, maven, etc.)
- Convert between native version range syntax and universal vers URI format
- Mathematical interval-based operations (union, intersection, complement)
- Version comparison and containment checking
- Extensible architecture for adding new package manager support
Quick Start
require 'vers'
# Parse a vers URI
range = Vers.parse("vers:npm/>=1.2.3|<2.0.0")
range.contains?("1.5.0") # => true
range.contains?("2.1.0") # => false
# Parse native package manager syntax
npm_range = Vers.parse_native("^1.2.3", "npm")
gem_range = Vers.parse_native("~> 1.0", "gem")
# Check version containment
Vers.satisfies?("1.5.0", ">=1.0.0,<2.0.0") # => true
# Compare versions
Vers.compare("1.2.3", "1.2.4") # => -1
Mathematical Model
Internally, all version ranges are represented as mathematical intervals, similar to those used in mathematics (e.g., [1.0.0, 2.0.0) represents versions from 1.0.0 inclusive to 2.0.0 exclusive).
This allows for precise set operations like union, intersection, and complement, regardless of the original package manager syntax.
Defined Under Namespace
Modules: MavenVersion, NuGetVersion Classes: Constraint, Error, Interval, Parser, Version, VersionRange
Constant Summary collapse
- VERSION =
"1.3.1"- @@parser =
Default parser instance for convenience methods
Parser.new
Class Method Summary collapse
- .clean(version_string) ⇒ Object
-
.compare(a, b) ⇒ Integer
Compares two version strings.
-
.compare_with_scheme(a, b, scheme) ⇒ Integer
Compares two version strings using scheme-specific rules.
-
.empty ⇒ VersionRange
Creates an empty version range (matches no versions).
-
.exact(version) ⇒ VersionRange
Creates an exact version range.
-
.greater_than(version, inclusive: false) ⇒ VersionRange
Creates a greater-than version range.
-
.less_than(version, inclusive: false) ⇒ VersionRange
Creates a less-than version range.
-
.normalize(version_string) ⇒ String
Normalizes a version string to a consistent format.
-
.parse(vers_string) ⇒ VersionRange
Parses a vers URI string into a VersionRange.
-
.parse_native(range_string, scheme) ⇒ VersionRange
Parses a native package manager version range into a VersionRange.
-
.satisfies?(version, constraint, scheme = nil) ⇒ Boolean
Checks if a version satisfies a version range constraint.
-
.to_vers_string(version_range, scheme) ⇒ String
Converts a VersionRange to a vers URI string.
-
.unbounded ⇒ VersionRange
Creates an unbounded version range (matches all versions).
-
.valid?(version_string) ⇒ Boolean
Checks if a version string is valid.
Class Method Details
.clean(version_string) ⇒ Object
182 183 184 |
# File 'lib/vers.rb', line 182 def self.clean(version_string) Version.clean(version_string) end |
.compare(a, b) ⇒ Integer
146 147 148 |
# File 'lib/vers.rb', line 146 def self.compare(a, b) Version.compare(a, b) end |
.compare_with_scheme(a, b, scheme) ⇒ Integer
Compares two version strings using scheme-specific rules
158 159 160 |
# File 'lib/vers.rb', line 158 def self.compare_with_scheme(a, b, scheme) Version.compare_with_scheme(a, b, scheme) end |
.empty ⇒ VersionRange
Creates an empty version range (matches no versions)
232 233 234 |
# File 'lib/vers.rb', line 232 def self.empty VersionRange.empty end |
.exact(version) ⇒ VersionRange
Creates an exact version range
192 193 194 |
# File 'lib/vers.rb', line 192 def self.exact(version) VersionRange.exact(version) end |
.greater_than(version, inclusive: false) ⇒ VersionRange
Creates a greater-than version range
203 204 205 |
# File 'lib/vers.rb', line 203 def self.greater_than(version, inclusive: false) VersionRange.greater_than(version, inclusive: inclusive) end |
.less_than(version, inclusive: false) ⇒ VersionRange
Creates a less-than version range
214 215 216 |
# File 'lib/vers.rb', line 214 def self.less_than(version, inclusive: false) VersionRange.less_than(version, inclusive: inclusive) end |
.normalize(version_string) ⇒ String
Normalizes a version string to a consistent format
168 169 170 |
# File 'lib/vers.rb', line 168 def self.normalize(version_string) Version.normalize(version_string) end |
.parse(vers_string) ⇒ VersionRange
76 77 78 |
# File 'lib/vers.rb', line 76 def self.parse(vers_string) @@parser.parse(vers_string) end |
.parse_native(range_string, scheme) ⇒ VersionRange
93 94 95 |
# File 'lib/vers.rb', line 93 def self.parse_native(range_string, scheme) @@parser.parse_native(range_string, scheme) end |
.satisfies?(version, constraint, scheme = nil) ⇒ Boolean
121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/vers.rb', line 121 def self.satisfies?(version, constraint, scheme = nil) sub_ranges = constraint.split('||').map(&:strip).reject(&:empty?) sub_ranges.any? do |sub_range| range = if scheme parse_native(sub_range, scheme) else parse(sub_range) end range&.contains?(version) || false end end |
.to_vers_string(version_range, scheme) ⇒ String
Converts a VersionRange to a vers URI string
104 105 106 |
# File 'lib/vers.rb', line 104 def self.to_vers_string(version_range, scheme) @@parser.to_vers_string(version_range, scheme) end |
.unbounded ⇒ VersionRange
Creates an unbounded version range (matches all versions)
223 224 225 |
# File 'lib/vers.rb', line 223 def self.unbounded VersionRange.unbounded end |