Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add SemanticVersion to API docs #7003

Merged
merged 5 commits into from
Nov 24, 2018
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/docs_main.cr
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ require "./option_parser"
require "./partial_comparable"
require "./random/**"
require "./readline"
require "./semantic_version"
require "./signal"
require "./string_pool"
require "./string_scanner"
Expand Down
53 changes: 49 additions & 4 deletions src/semantic_version.cr
Original file line number Diff line number Diff line change
@@ -1,14 +1,31 @@
# Conforms to Semantic Versioning 2.0.0
RX14 marked this conversation as resolved.
Show resolved Hide resolved
# See http://semver.org/ for more information.
# See [https://semver.org/](https://semver.org/) for more information.
class SemanticVersion
include Comparable(self)

# The major version of this semantic version
getter major : Int32

# The minor version of this semantic version
getter minor : Int32

# The patch version of this semantic version
getter patch : Int32

# The build number of this semantic version
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not always just a number.

Suggested change
# The build number of this semantic version
# The build metadata of this semantic version

getter build : String?

# The pre-release metadata of this semantic version
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

semver.org calls this the pre-release version

getter prerelease : Prerelease

# Parses a `SemanticVersion` from the given semantic version string
#
# ```
# require "semantic_version"
#
# semver = SemanticVersion.parse("2.61.4")
# semver # => #<SemanticVersion:0x55b3667c9e70 @major=2, @minor=61, @patch=4, ... >
# ```
RX14 marked this conversation as resolved.
Show resolved Hide resolved
def self.parse(str : String) : self
m = str.match /^(\d+)\.(\d+)\.(\d+)(-([\w\.]+))?(\+(\w+))??$/
if m
Expand All @@ -23,6 +40,7 @@ class SemanticVersion
end
end

# Create a new `SemanticVersion` instance with the given major, minor, and patch versions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# Create a new `SemanticVersion` instance with the given major, minor, and patch versions
# Creates a new `SemanticVersion` instance with the given major, minor, and patch versions
# and optionally build and pre-release metadata
#
# Raises `ArgumentError` if *prerelease* is invalid pre-release metadata.

def initialize(@major : Int, @minor : Int, @patch : Int, prerelease : String | Prerelease | Nil = nil, @build : String? = nil)
@prerelease = case prerelease
when Prerelease
Expand All @@ -36,6 +54,12 @@ class SemanticVersion
end
end

# Returns the string representation of this semantic version
#
# ```
RX14 marked this conversation as resolved.
Show resolved Hide resolved
# semver = SemanticVersion.parse("0.27.1")
# semver.to_s # => "0.27.1"
# ```
def to_s(io : IO)
io << major << '.' << minor << '.' << patch
unless prerelease.identifiers.empty?
Expand All @@ -47,21 +71,31 @@ class SemanticVersion
end
end

# :nodoc:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why :nodoc:? The <=> comparison operator should be documented so users know it can be used here.

Suggested change
# :nodoc:
# The comparison operator.
#
# ```
# require "semantic_version"
#
# semver1 = SemanticVersion.new(1, 0, 0)
# semver2 = SemanticVersion.new(2, 0, 0)
# semver1 <=> semver2 # => -1
# ```

Copy link
Member Author

@Blacksmoke16 Blacksmoke16 Oct 29, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was thinking since its required by the Comparable that the user would look there.

But probably wouldn't hurt to add here as well.

def <=>(other : self) : Int32
r = major <=> other.major
return r if r != 0
return r unless r.zero?
r = minor <=> other.minor
return r if r != 0
return r unless r.zero?
r = patch <=> other.patch
return r if r != 0
return r unless r.zero?

pre1 = prerelease
pre2 = other.prerelease

prerelease <=> other.prerelease
end

# Contains additional pre-release metadata related to this semantic version
struct Prerelease
# Parses a `Prerelease` from the given pre-release metadata string
#
# ```
# require "semantic_version"
#
# prerelease = SemanticVersion::Prerelease.parse("rc.1.3")
RX14 marked this conversation as resolved.
Show resolved Hide resolved
# prerelease # => SemanticVersion::Prerelease(@identifiers=["rc", 1, 3])
# ```
def self.parse(str : String) : self
identifiers = [] of String | Int32
str.split('.').each do |val|
Expand All @@ -74,15 +108,26 @@ class SemanticVersion
Prerelease.new identifiers
end

# Array of identifiers that make up the pre-release metadata
getter identifiers : Array(String | Int32)

# Create a new `Prerelease` instance with supplied array of identifiers
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use the third person: Creates.

def initialize(@identifiers : Array(String | Int32) = [] of String | Int32)
end

# Returns the string representation of this semantic version's pre-release metadata
#
# ```
# require "semantic_version"
#
# semver = SemanticVersion.parse("0.27.1-rc.1")
# semver.prerelease.to_s # => "rc.1"
# ```
def to_s(io : IO)
identifiers.join(".", io)
end

# :nodoc:
Copy link
Contributor

@wooster0 wooster0 Oct 29, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

def <=>(other : self) : Int32
if identifiers.empty?
if other.identifiers.empty?
Expand Down