class Semantic::Version

Overview

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

Included Modules

Defined in:

semantic/version.cr

Constant Summary

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

Instance Method Summary

Constructor Detail

def self.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"

[View source]
def self.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)

[View source]
def self.new(version : Tuple(Int32, Int32, Int32)) #

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


[View source]
def self.new(version : Tuple(Int32, Int32, Int32, String | Nil, String | Nil)) #

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


[View source]
def self.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)

[View source]

Instance Method Detail

def build : String | Nil #

[View source]
def compare_elem(one : Int32, other : Int32) #

[View source]
def eql?(other : Version) #

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


[View source]
def eql?(other : String) #

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


[View source]
def gt?(other : Version) #

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


[View source]
def gt?(other : String) #

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


[View source]
def gte?(other : Version) #

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


[View source]
def gte?(other : String) #

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


[View source]
def lt?(other : Version) #

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


[View source]
def lt?(other : String) #

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


[View source]
def lte?(other : Version) #

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


[View source]
def lte?(other : String) #

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


[View source]
def major : Int32 #

[View source]
def major! : Version #

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.


[View source]
def major=(major : Int32) #

[View source]
def minor : Int32 #

[View source]
def minor! : Version #

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.


[View source]
def minor=(minor : Int32) #

[View source]
def patch : Int32 #

[View source]
def patch=(patch : Int32) #

[View source]
def pre : String | Nil #

[View source]
def pre=(pre : String | Nil) #

[View source]
def 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

[View source]
def 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"]

[View source]
def 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"}

[View source]
def to_s #

Returns a version string.

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

[View source]
def 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"}

[View source]