Class: VersionBoss::Gem::Version

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

Overview

Parse and compare Ruby Gem version strings

This class will parse and validate a Ruby Gem version string

Two GemVersion objects can be compared using the spaceship operator (<=>) according to the rules of precedence defined in the Gem::Version class.

Examples:

Basic version parsing

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

Parsing a version with a pre-release identifier

gem_version = VersionBoss::GemVersion.new('1.2.3.alpha.1')
gem_version.pre_release # => 'alpha.1'
gem_version.pre_release_identifiers # => ['alpha', '1']

Separators are optional between pre-release identifiers

gem_version1 = VersionBoss::GemVersion.new('1.2.3.alpha.1')
gem_version2 = VersionBoss::GemVersion.new('1.2.3.alpha1')
gem_version3 = VersionBoss::GemVersion.new('1.2.3alpha1')

gem_version1 == gem_version2 # => true
gem_version1 == gem_version3 # => true
gem_version2 == gem_version3 # => true

gem_version1.pre_release1 # => '.alpha.1'
gem_version1.pre_release2 # => '.alpha1'
gem_version1.pre_release3 # => 'alpha1'

gem_version1.pre_release_identifiers # => ['alpha', '1']
gem_version2.pre_release_identifiers # => ['alpha', '1']
gem_version3.pre_release_identifiers # => ['alpha', '1']

Comparing versions

gem_version1 = VersionBoss::GemVersion.new('1.2.3')
gem_version2 = VersionBoss::GemVersion.new('1.2.4')
gem_version1 <=> gem_version2 # => -1
gem_version2 <=> gem_version3 # => 1
gem_version1 <=> gem_version1 # => 0

See Also:

Direct Known Subclasses

IncrementableVersion

Defined Under Namespace

Classes: PreReleaseIdentifier

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(version) ⇒ Version

Create a new GemVersion object

Examples:

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

Parameters:

  • version (String)

    The version string to parse

Raises:



66
67
68
69
70
71
# File 'lib/version_boss/gem/version.rb', line 66

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

Instance Attribute Details

#majorString (readonly)

The major part of the version

Examples:

gem_version = VersionBoss::GemVersion.new('1.2.3.alpha.1+build.001')
gem_version.major #=> '1'

Returns:

  • (String)


99
100
101
# File 'lib/version_boss/gem/version.rb', line 99

def major
  @major
end

#minorString (readonly)

The minor part of the version

Examples:

gem_version = VersionBoss::GemVersion.new('1.2.3.alpha.1+build.001')
gem_version.minor #=> '2'

Returns:

  • (String)


113
114
115
# File 'lib/version_boss/gem/version.rb', line 113

def minor
  @minor
end

#patchString (readonly)

The patch part of the version

Examples:

gem_version = VersionBoss::GemVersion.new('1.2.3.alpha.1+build.001')
gem_version.patch #=> '3'

Returns:

  • (String)


127
128
129
# File 'lib/version_boss/gem/version.rb', line 127

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:

gem_version = VersionBoss::GemVersion.new('1.2.3.alpha.1+build.001')
gem_version.pre_release #=> 'alpha.1'

When the version has no pre_release part

gem_version = VersionBoss::GemVersion.new('1.2.3')
gem_version.pre_release #=> ''

Returns:

  • (String)


147
148
149
# File 'lib/version_boss/gem/version.rb', line 147

def pre_release
  @pre_release
end

#pre_release_identifiersArray<String> (readonly)

The pre_release identifiers of the version

Examples:

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

When the version has no pre_release part

gem_version = VersionBoss::GemVersion.new('1.2.3')
gem_version.pre_release_identifiers #=> []

Returns:

  • (Array<String>)


165
166
167
# File 'lib/version_boss/gem/version.rb', line 165

def pre_release_identifiers
  @pre_release_identifiers
end

#versionString (readonly)

The complete version string

Examples:

gem_version = VersionBoss::GemVersion.new('1.2.3.alpha.1+build.001')
gem_version.version #=> '1.2.3.alpha.1+build.001'

Returns:

  • (String)


85
86
87
# File 'lib/version_boss/gem/version.rb', line 85

def version
  @version
end

Instance Method Details

#<=>(other) ⇒ Integer

Compare two GemVersion objects

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

Examples:

gem_version1 = VersionBoss::GemVersion.new('1.2.3')
gem_version2 = VersionBoss::GemVersion.new('1.2.4')
gem_version1 <=> gem_version2 # => -1
gem_version2 <=> gem_version1 # => 1

A GemVersion is equal to itself

gem_version1 = VersionBoss::GemVersion.new('1.2.3')
gem_version1 <=> gem_version1 # => 0

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

gem_version1 = VersionBoss::GemVersion.new('1.2.3.alpha.1')
gem_version2 = VersionBoss::GemVersion.new('1.2.3')
gem_version1 <=> gem_version2 # => -1

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

gem_version1 = VersionBoss::GemVersion.new('1.2.3.alpha.1')
gem_version2 = VersionBoss::GemVersion.new('1.2.3.alpha.2')
gem_version1 <=> gem_version2 # => -1

Build metadata is ignored when comparing versions

gem_version1 = VersionBoss::GemVersion.new('1.2.3+build.100')
gem_version2 = VersionBoss::GemVersion.new('1.2.3+build.101')
gem_version1 <=> gem_version2 # => 0

Parameters:

  • other (GemVersion)

    the other GemVersion to compare to

Returns:

  • (Integer)

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

Raises:



203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/version_boss/gem/version.rb', line 203

def <=>(other)
  assert_other_is_a_gem_version(other)

  core_comparison = compare_core_parts(other)
  pre_release_comparison = compare_pre_release_part(other)

  return core_comparison unless core_comparison.zero? && !pre_release_comparison.zero?

  return 1 if pre_release.empty?
  return -1 if other.pre_release.empty?

  pre_release_comparison
end

#==(other) ⇒ Boolean

Two versions are equal if their version strings are equal

Examples:

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

Parameters:

  • other (GemVersion)

    the other GemVersion to compare to

Returns:

  • (Boolean)

    true if the version strings are equal



242
243
244
# File 'lib/version_boss/gem/version.rb', line 242

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

#to_sString

The string representation of a GemVersion is its version string

Examples:

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

Returns:

  • (String)

    the version string



253
254
255
# File 'lib/version_boss/gem/version.rb', line 253

def to_s
  version
end

#valid?Boolean

Determine if the version string is a valid gem_version

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

Examples:

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

Returns:

  • (Boolean)

    true if the version string is a valid gem_version



228
229
230
231
# File 'lib/version_boss/gem/version.rb', line 228

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