class

Semantic::Version

Inherits Comparable < Reference < Object

A small Crystal utility class for storing, parsing, and comparing SemVer-style version strings.

Constants

SEMVER_REGEXP = /\A(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][a-zA-Z0-9-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][a-zA-Z0-9-]*))*))?(?:\+([0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*))?\Z/

Constructors

new(version_string : String)

Initialize a new Semantic::Version from a version string.

The string must be a valid SemVer string, containing at least major, minor, and patch elements. Pre and build elements are optional.

Examples of valid strings:

"2.4.6"
"25.58.100"
"1.0.0-pre1"
"1.0.0+build.112358"
"2.0.5-alpha+prerelease"
Source
new(version : Hash(String, Int32 | String | Nil))

Initialize a new Semantic::Version from a hash.

The hash must contain "major", "minor", and "patch" keys with values. "Pre" and "build" elements are optional. The hash may contain other keys, too. Those are discarded on initialization.

Example:

hash = {
  "major" => 2,
  "minor" => 4,
  "patch" => 0, 
}
version = Semantic::Version.new(hash)
Source
new(version : Tuple(Int32, Int32, Int32))

Initialize a new Semantic::Version from a tuple T(Int32, Int32, Int32).

Source
new(version : Tuple(Int32, Int32, Int32, String | Nil, String | Nil))

Initialize a new Semantic::Version from a tuple T(Int32, Int32, Int32, String?, String?).

Source
new(version : NamedTuple(major: Int32, minor: Int32, patch: Int32, pre: String | Nil, build: String | Nil))

Initialize a new Semantic::Version from a named tuple.

Example:

tuple = {
  major: 2,
  minor: 4,
  patch: 0,
  pre: nil,
  build: "prerelease" 
}
Semantic::Version.new(tuple)
Source

Instance methods

build
Source
compare_elem(one : Int32, other : Int32)
Source
eql?(other : Version)

Convenience method for testing whether a version is equal to supplied version.

Source
eql?(other : String)

Convenience method for testing whether a version is equal to supplied version string.

Source
gt?(other : Version)

Convenience method for testing whether a version is greater than supplied version.

Source
gt?(other : String)

Convenience method for testing whether a version is greater than supplied version string.

Source
gte?(other : Version)

Convenience method for testing whether a version is greater or equal than supplied version.

Source
gte?(other : String)

Convenience method for testing whether a version is greater or equal than supplied version string.

Source
lt?(other : Version)

Convenience method for testing whether a version is less than supplied version.

Source
lt?(other : String)

Convenience method for testing whether a version is less than supplied version string.

Source
lte?(other : Version)

Convenience method for testing whether a version is less or equal than supplied version.

Source
lte?(other : String)

Convenience method for testing whether a version is less or equal than supplied version string.

Source
major
Source
major!

Bumps up major version number and returns a new Semantic::Version instance.

Note: this will clear pre and build version information for the new instance.

Source
major=(major : Int32)
Source
minor
Source
minor!

Bumps up minor version number and returns a new Semantic::Version instance.

Note: this will clear pre and build version information for the new instance.

Source
minor=(minor : Int32)
Source
patch
Source
patch=(patch : Int32)
Source
pre=(pre : String | Nil)
Source
satisfies?(version : String)

Queries whether a version string with a constraint operator is satisfied with the instance.

Supported constraints are wildcard (*) and tilde (~) version ranges. Examples:

version = Semantic::Version.new("2.4.8")
version.satisfies?("2.*")       # => true, >= 2.0.0 and < 3.0.0
version.satisfies?("2.4.*")     # => true, >= 2.4.0 and < 2.5.0
version.satisfies?("~2.3")      # => true, >= 2.3.0 and < 3.0.0
version.satisfies?("~2.3.6")    # => false, >= 2.3.6 and < 2.4.0
Source
to_a

Returns an array of the elements of the version.

Semantic::Version.new("2.4.0-pre1+build.1123").to_a   # => [2, 4, 0, "pre1", "build.1123"]
Source
to_h

Returns version information as a hash.

Semantic::Version.new("2.4.0-pre1+build.1123").to_h
# => {"major" => 2, "minor" => 4, "patch" => 0, "pre" => "pre1", "build" => "build.1123"}
Source
to_s

Returns a version string.

Semantic::Version.new("2.4.0-pre1+build.1123").to_s   # => "2.4.0-pre1+build.1123"
Source
to_t

Returns version information as a tuple.

Semantic::Version.new("2.4.0-pre1+build.1123").to_t
# => {major: 2, minor: 4, patch: 0, pre: "pre1", build: "build.1123"}
Source