class Semver::String

A helper class for working with semantic versioning strings.

~~~ version = Semver::String.new('1.2.3-alpha+build-123')

version.major #=> 1 version.major #=> 2 version.patch #=> 3 version.pre_release #=> alpha version.build_metadata #=> build-123 version.to_s #=> “1.2.3-alpha+build-123” ~~~

Attributes

build_metadata[R]

@return [String, nil]

major[R]

@return [Integer]

minor[R]

@return [Integer]

patch[R]

@return [Integer]

pre_release[R]

@return [String, nil]

Public Class Methods

new( major:, minor:, patch:, pre_release: nil, build_metadata: nil ) click to toggle source

@param [Integer] major @param [Integer] minor @param [Integer] patch @param [String,nil] pre_release @param [String,nil] build_metadata

# File lib/semver/string.rb, line 36
def initialize(
  major:,
  minor:,
  patch:,
  pre_release: nil,
  build_metadata: nil
)
  @major = major
  @minor = minor
  @patch = patch
  @pre_release = pre_release
  @build_metadata = build_metadata
  @string = format_string
end
parse(string) click to toggle source

@param [String] string @return [Version]

# File lib/semver/string.rb, line 25
def self.parse(string)
  semver = allocate
  semver.send(:initialize, Parser.new.parse(string))
  semver
end

Public Instance Methods

<=>(other) click to toggle source

See section #11 of semver.org/spec/v2.0.0.html @return [Integer] Returns -1, 0, or 1.

# File lib/semver/string.rb, line 90
def <=>(other)
  Comparator.new.compare(left: self, right: other)
end
==(other) click to toggle source

@return [Boolean] Returns `true` if the string value of the

two objects are equal.
# File lib/semver/string.rb, line 84
def ==(other)
  to_s == other.to_s
end
eql?(other) click to toggle source

@return [Boolean] @api private

# File lib/semver/string.rb, line 74
def eql?(other)
  if other.is_a?(Semver::String)
    to_str == other.to_str
  else
    false
  end
end
to_s()
Alias for: to_str
to_str() click to toggle source

@return [String]

# File lib/semver/string.rb, line 67
def to_str
  @string
end
Also aliased as: to_s

Private Instance Methods

format_string() click to toggle source
# File lib/semver/string.rb, line 96
def format_string
  string = [major, minor, patch].join('.')
  string += "-#{pre_release}" if pre_release
  string += "+#{build_metadata}" if build_metadata
  string
end