Class: TTY::Link::SemanticVersion Private

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/tty/link/semantic_version.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Responsible for comparing terminal release versions

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(major, minor, patch) ⇒ SemanticVersion

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Create a TTY::Link::SemanticVersion instance

Examples:

TTY::Link::SemanticVersion.new(1, 2, 3)

Parameters:

  • major (Integer)

    the major number

  • minor (Integer)

    the minor number

  • patch (Integer)

    the patch number



145
146
147
148
149
# File 'lib/tty/link/semantic_version.rb', line 145

def initialize(major, minor, patch)
  @major = major
  @minor = minor
  @patch = patch
end

Instance Attribute Details

#majorInteger (readonly)

The major number

Examples:

semantic_version.major

Returns:

  • (Integer)


110
111
112
# File 'lib/tty/link/semantic_version.rb', line 110

def major
  @major
end

#minorInteger (readonly)

The minor number

Examples:

semantic_version.minor

Returns:

  • (Integer)


120
121
122
# File 'lib/tty/link/semantic_version.rb', line 120

def minor
  @minor
end

#patchInteger (readonly)

The patch number

Examples:

semantic_version.patch

Returns:

  • (Integer)


130
131
132
# File 'lib/tty/link/semantic_version.rb', line 130

def patch
  @patch
end

Class Method Details

.from(*version, separator: VERSION_SEPARATOR) ⇒ TTY::Link::SemanticVersion

Create a TTY::Link::SemanticVersion instance from a version value

Examples:

TTY::Link::SemanticVersion.from(1, 2, 3)
TTY::Link::SemanticVersion[1, 2, 3]
TTY::Link::SemanticVersion.from("1234")
TTY::Link::SemanticVersion.from("1.2.3")
TTY::Link::SemanticVersion.from("1-2-3", separator: "-")

Parameters:

  • version (Array<Integer, String>)

    the version to convert to a semantic version

  • separator (String) (defaults to: VERSION_SEPARATOR)

    the version separator

Returns:



60
61
62
63
64
65
66
67
68
# File 'lib/tty/link/semantic_version.rb', line 60

def self.from(*version, separator: VERSION_SEPARATOR)
  major, minor, patch =
    if version.size == 1 && version[0].respond_to?(:split)
      convert_to_array(version[0], separator: separator)
    else
      version
    end
  new(major.to_i, minor.to_i, patch.to_i)
end

Instance Method Details

#<=>(other) ⇒ Integer?

Compare this semantic version with another object

Examples:

semantic_version >= other

Parameters:

  • other (Object)

    the other object to compare with

Returns:

  • (Integer, nil)

    Return negative, zero, or positive number when this semantic version is less than, equal to, or greater than other semantic version. Return nil when the other object is not a semantic version.



167
168
169
170
171
172
173
174
175
176
177
# File 'lib/tty/link/semantic_version.rb', line 167

def <=>(other)
  return unless other.is_a?(self.class)

  major_comparison = @major <=> other.major
  return major_comparison unless major_comparison.zero?

  minor_comparison = @minor <=> other.minor
  return minor_comparison unless minor_comparison.zero?

  @patch <=> other.patch
end

#hashInteger

Generate hash value for this semantic version

Examples:

semantic_version.hash

Returns:

  • (Integer)


187
188
189
# File 'lib/tty/link/semantic_version.rb', line 187

def hash
  [self.class, @major, @minor, @patch].hash
end

#inspectString

Convert this semantic version to a string

Examples:

semantic_version.inspect

Returns:

  • (String)


199
200
201
# File 'lib/tty/link/semantic_version.rb', line 199

def inspect
  [@major, @minor, @patch].join(VERSION_SEPARATOR)
end