class Semantic::Version
- Semantic::Version
- Reference
- Object
Overview
A small Crystal utility class for storing, parsing, and comparing SemVer-style version strings.
Included Modules
- Comparable(Semantic::Version)
Defined in:
semantic/version.crConstant 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
-
.new(version_string : String)
Initialize a new
Semantic::Version
from a version string. -
.new(version : Hash(String, Int32 | String | Nil))
Initialize a new
Semantic::Version
from a hash. -
.new(version : Tuple(Int32, Int32, Int32))
Initialize a new
Semantic::Version
from a tupleT(Int32, Int32, Int32)
. -
.new(version : Tuple(Int32, Int32, Int32, String | Nil, String | Nil))
Initialize a new
Semantic::Version
from a tupleT(Int32, Int32, Int32, String?, String?)
. -
.new(version : NamedTuple(major: Int32, minor: Int32, patch: Int32, pre: String | Nil, build: String | Nil))
Initialize a new
Semantic::Version
from a named tuple.
Instance Method Summary
- #build : String | Nil
- #compare_elem(one : Int32, other : Int32)
-
#eql?(other : Version)
Convenience method for testing whether a version is equal to supplied version.
-
#eql?(other : String)
Convenience method for testing whether a version is equal to supplied version string.
-
#gt?(other : Version)
Convenience method for testing whether a version is greater than supplied version.
-
#gt?(other : String)
Convenience method for testing whether a version is greater than supplied version string.
-
#gte?(other : Version)
Convenience method for testing whether a version is greater or equal than supplied version.
-
#gte?(other : String)
Convenience method for testing whether a version is greater or equal than supplied version string.
-
#lt?(other : Version)
Convenience method for testing whether a version is less than supplied version.
-
#lt?(other : String)
Convenience method for testing whether a version is less than supplied version string.
-
#lte?(other : Version)
Convenience method for testing whether a version is less or equal than supplied version.
-
#lte?(other : String)
Convenience method for testing whether a version is less or equal than supplied version string.
- #major : Int32
-
#major! : Version
Bumps up major version number and returns a new
Semantic::Version
instance. - #major=(major : Int32)
- #minor : Int32
-
#minor! : Version
Bumps up minor version number and returns a new
Semantic::Version
instance. - #minor=(minor : Int32)
- #patch : Int32
- #patch=(patch : Int32)
- #pre : String | Nil
- #pre=(pre : String | Nil)
-
#satisfies?(version : String)
Queries whether a version string with a constraint operator is satisfied with the instance.
-
#to_a
Returns an array of the elements of the version.
-
#to_h
Returns version information as a hash.
-
#to_s
Returns a version string.
-
#to_t
Returns version information as a tuple.
Constructor Detail
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"
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)
Initialize a new Semantic::Version
from a tuple T(Int32, Int32, Int32)
.
Initialize a new Semantic::Version
from a tuple T(Int32, Int32, Int32, String?, String?)
.
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)
Instance Method Detail
Convenience method for testing whether a version is equal to supplied version.
Convenience method for testing whether a version is equal to supplied version string.
Convenience method for testing whether a version is greater than supplied version.
Convenience method for testing whether a version is greater than supplied version string.
Convenience method for testing whether a version is greater or equal than supplied version.
Convenience method for testing whether a version is greater or equal than supplied version string.
Convenience method for testing whether a version is less than supplied version.
Convenience method for testing whether a version is less than supplied version string.
Convenience method for testing whether a version is less or equal than supplied version.
Convenience method for testing whether a version is less or equal than supplied version string.
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.
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.
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
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"]
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"}
Returns a version string.
Semantic::Version.new("2.4.0-pre1+build.1123").to_s # => "2.4.0-pre1+build.1123"
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"}