Class: VersionBoss::Semver::Version

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/version_boss/semver/version.rb

Overview

Parse and compare semver version strings

This class will parse a semver version string that complies to Semantic Versioning 2.0.0.

Two Semver objects can be compared using the spaceship operator (<=>) according to the rules of Semantic Versioning 2.0.0.

See the Semantic Versioning 2.0.0 specification for more details.

Examples:

Basic version parsing

semver = VersionBoss::Semver.new('1.2.3')
semver.major # => '1'
semver.minor # => '2'
semver.patch # => '3'

Parsing a version with a pre-release identifier

semver = VersionBoss::Semver.new('1.2.3-alpha.1')
semver.pre_release # => 'alpha.1'
semver.pre_release_identifiers # => ['alpha', '1']

A version with build metadata

semver = VersionBoss::Semver.new('1.2.3+build.1')
semver. # => 'build.1'

Comparing versions

semver1 = VersionBoss::Semver.new('1.2.3')
semver2 = VersionBoss::Semver.new('1.2.4')
semver1 <=> semver2 # => true

See Also:

Direct Known Subclasses

IncrementableVersion

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(version) ⇒ Version

Create a new Semver object

Examples:

version = VersionBoss::Semver.new('1.2.3-alpha.1')

Parameters:

  • version (String)

    The version string to parse

Raises:



53
54
55
56
57
58
# File 'lib/version_boss/semver/version.rb', line 53

def initialize(version)
  assert_version_must_be_a_string(version)
  @version = version
  parse
  assert_valid_version
end

Instance Attribute Details

#build_metadataString (readonly)

The build_metadata part of the version

Will be an empty string if the version has no build_metadata part.

Examples:

semver = VersionBoss::Semver.new('1.2.3-alpha.1+build.001')
semver. #=> 'build.001'

When the version has no build_metadata part

semver = VersionBoss::Semver.new('1.2.3')
semver. #=> ''

Returns:

  • (String)


172
173
174
# File 'lib/version_boss/semver/version.rb', line 172

def 
  @build_metadata
end

#majorString (readonly)

The major part of the version

Examples:

semver = VersionBoss::Semver.new('1.2.3-alpha.1+build.001')
semver.major #=> '1'

Returns:

  • (String)


86
87
88
# File 'lib/version_boss/semver/version.rb', line 86

def major
  @major
end

#minorString (readonly)

The minor part of the version

Examples:

semver = VersionBoss::Semver.new('1.2.3-alpha.1+build.001')
semver.minor #=> '2'

Returns:

  • (String)


100
101
102
# File 'lib/version_boss/semver/version.rb', line 100

def minor
  @minor
end

#patchString (readonly)

The patch part of the version

Examples:

semver = VersionBoss::Semver.new('1.2.3-alpha.1+build.001')
semver.patch #=> '3'

Returns:

  • (String)


114
115
116
# File 'lib/version_boss/semver/version.rb', line 114

def patch
  @patch
end

#pre_releaseString (readonly)

The pre_release part of the version

Will be an empty string if the version has no pre_release part.

Examples:

semver = VersionBoss::Semver.new('1.2.3-alpha.1+build.001')
semver.pre_release #=> 'alpha.1'

When the version has no pre_release part

semver = VersionBoss::Semver.new('1.2.3')
semver.pre_release #=> ''

Returns:

  • (String)


134
135
136
# File 'lib/version_boss/semver/version.rb', line 134

def pre_release
  @pre_release
end

#pre_release_identifiersArray<String> (readonly)

The pre_release identifiers of the version

Examples:

semver = VersionBoss::Semver.new('1.2.3-alpha.1+build.001')
semver.pre_release_identifiers #=> ['alpha', '1']

When the version has no pre_release part

semver = VersionBoss::Semver.new('1.2.3')
semver.pre_release_identifiers #=> []

Returns:

  • (Array<String>)


152
153
154
# File 'lib/version_boss/semver/version.rb', line 152

def pre_release_identifiers
  @pre_release_identifiers
end

#versionString (readonly)

The complete version string

Examples:

semver = VersionBoss::Semver.new('1.2.3-alpha.1+build.001')
semver.version #=> '1.2.3-alpha.1+build.001'

Returns:

  • (String)


72
73
74
# File 'lib/version_boss/semver/version.rb', line 72

def version
  @version
end

Instance Method Details

#<=>(other) ⇒ Integer

Compare two Semver objects

See the Precedence Rules in the Semantic Versioning 2.0.0 Specification for more details.

Examples:

semver1 = VersionBoss::Semver.new('1.2.3')
semver2 = VersionBoss::Semver.new('1.2.4')
semver1 <=> semver2 # => -1
semver2 <=> semver1 # => 1

A Semver is equal to itself

semver1 = VersionBoss::Semver.new('1.2.3')
semver1 <=> semver1 # => 0

A pre-release version is always older than a normal version

semver1 = VersionBoss::Semver.new('1.2.3-alpha.1')
semver2 = VersionBoss::Semver.new('1.2.3')
semver1 <=> semver2 # => -1

Pre-releases are compared by the parts of the pre-release version

semver1 = VersionBoss::Semver.new('1.2.3-alpha.1')
semver2 = VersionBoss::Semver.new('1.2.3-alpha.2')
semver1 <=> semver2 # => -1

Build metadata is ignored when comparing versions

semver1 = VersionBoss::Semver.new('1.2.3+build.100')
semver2 = VersionBoss::Semver.new('1.2.3+build.101')
semver1 <=> semver2 # => 0

Parameters:

  • other (Semver)

    the other Semver to compare to

Returns:

  • (Integer)

    -1 if self < other, 0 if self == other, or 1 if self > other

Raises:



210
211
212
213
214
215
216
217
218
219
220
# File 'lib/version_boss/semver/version.rb', line 210

def <=>(other)
  assert_other_is_a_semver(other)

  result = compare_core_parts(other)

  return result unless result.zero? && pre_release != other.pre_release
  return 1 if pre_release.empty?
  return -1 if other.pre_release.empty?

  compare_pre_release_part(other)
end

#==(other) ⇒ Boolean

Two versions are equal if their version strings are equal

Examples:

VersionBoss::Semver.new('1.2.3') == '1.2.3' # => true

Parameters:

  • other (Semver)

    the other Semver to compare to

Returns:

  • (Boolean)

    true if the version strings are equal



247
248
249
# File 'lib/version_boss/semver/version.rb', line 247

def ==(other)
  version == other.to_s
end

#to_sString

The string representation of a Semver is its version string

Examples:

VersionBoss::Semver.new('1.2.3').to_s # => '1.2.3'

Returns:

  • (String)

    the version string



258
259
260
# File 'lib/version_boss/semver/version.rb', line 258

def to_s
  version
end

#valid?Boolean

Determine if the version string is a valid semver

Override this method in a subclass to provide extra or custom validation.

Examples:

VersionBoss::Semver.new('1.2.3').valid? # => true
VersionBoss::Semver.new('1.2.3-alpha.1+build.001').valid? # => true
VersionBoss::Semver.new('bogus').valid? # => raises VersionBoss::Error

Returns:

  • (Boolean)

    true if the version string is a valid semver



233
234
235
236
# File 'lib/version_boss/semver/version.rb', line 233

def valid?
  # If major is set, then so is everything else
  !major.nil?
end