Class: VersionBoss::Semver::IncrementableVersion

Inherits:
Version
  • Object
show all
Defined in:
lib/version_boss/semver/incrementable_version.rb

Overview

A Semver Version with additional constraints on the pre-release part of the version

An IncrementableVersion is valid if one of the two following conditions is met:

  1. The pre-release part is empty
  2. The pre-release part is composed of two dot-separated identifiers:
    • the first being a String representing the pre-release type (e.g. 'alpha', 'beta', etc.). The default pre-release type is 'pre'
    • the second being an Integer representing the pre-release sequence number (starting at 1)

Valid versions with pre-release parts: 1.2.3-alpha.1, 1.2.3-beta.2, 1.2.3-pre.3

Constant Summary collapse

DEFAULT_PRE_TYPE =

The default pre-release identifier

'pre'

Instance Attribute Summary

Attributes inherited from Version

#build_metadata, #major, #minor, #patch, #pre_release, #pre_release_identifiers, #version

Instance Method Summary collapse

Methods inherited from Version

#<=>, #==, #initialize, #to_s

Constructor Details

This class inherits a constructor from VersionBoss::Semver::Version

Instance Method Details

#next_major(pre: false, pre_type: DEFAULT_PRE_TYPE, build_metadata: nil) ⇒ IncrementableVersion

Increment the major version

Examples:

IncrementableVersionify::Semver.new('1.2.3').next_major # => IncrementableVersionify::Semver.new('2.0.0')

Returns:



48
49
50
51
52
53
54
# File 'lib/version_boss/semver/incrementable_version.rb', line 48

def next_major(pre: false, pre_type: DEFAULT_PRE_TYPE, build_metadata: nil)
  version_string = "#{major.to_i + 1}.0.0"
  version_string += "-#{pre_type}.1" if pre
   = self. if .nil?
  version_string += "+#{}" unless .empty?
  IncrementableVersion.new(version_string)
end

#next_minor(pre: false, pre_type: DEFAULT_PRE_TYPE, build_metadata: nil) ⇒ IncrementableVersion

Increment the minor version

Examples:

IncrementableVersionify::Semver.new('1.2.3').next_minor # => IncrementableVersionify::Semver.new('1.3.0')

Returns:



63
64
65
66
67
68
69
# File 'lib/version_boss/semver/incrementable_version.rb', line 63

def next_minor(pre: false, pre_type: DEFAULT_PRE_TYPE, build_metadata: nil)
  version_string = "#{major}.#{minor.to_i + 1}.0"
  version_string += "-#{pre_type}.1" if pre
   = self. if .nil?
  version_string += "+#{}" unless .empty?
  IncrementableVersion.new(version_string)
end

#next_patch(pre: false, pre_type: DEFAULT_PRE_TYPE, build_metadata: nil) ⇒ IncrementableVersion

Increment the patch version

Examples:

IncrementableVersionify::Semver.new('1.2.3').next_patch # => IncrementableVersionify::Semver.new('1.2.1')

Returns:



78
79
80
81
82
83
84
# File 'lib/version_boss/semver/incrementable_version.rb', line 78

def next_patch(pre: false, pre_type: DEFAULT_PRE_TYPE, build_metadata: nil)
  version_string = "#{major}.#{minor}.#{patch.to_i + 1}"
  version_string += "-#{pre_type}.1" if pre
   = self. if .nil?
  version_string += "+#{}" unless .empty?
  IncrementableVersion.new(version_string)
end

#next_pre(pre_type: nil, build_metadata: nil) ⇒ IncrementableVersion

Increment the pre_release part of the version

Examples:

IncrementableVersionify::Semver.new('1.2.3-pre.1').next_patch.to_s # => '1.2.3-pre.2'

Returns:

  • (IncrementableVersion)

    a new IncrementableVersion object with the pre_release part incremented



93
94
95
96
97
98
99
100
# File 'lib/version_boss/semver/incrementable_version.rb', line 93

def next_pre(pre_type: nil, build_metadata: nil)
  assert_is_a_pre_release_version
  version_string = "#{major}.#{minor}.#{patch}"
  version_string += next_pre_part(pre_type)
   ||= self.
  version_string += "+#{}" unless .empty?
  IncrementableVersion.new(version_string)
end

#next_release(build_metadata: nil) ⇒ IncrementableVersion

Drop the pre-release part of the version

Examples:

IncrementableVersionify::Semver.new('1.2.3-pre.1').next_release.to_s # => '1.2.3'

Returns:

Raises:



110
111
112
113
114
115
116
# File 'lib/version_boss/semver/incrementable_version.rb', line 110

def next_release(build_metadata: nil)
  assert_is_a_pre_release_version
  version_string = "#{major}.#{minor}.#{patch}"
   ||= self.
  version_string += "+#{}" unless .empty?
  IncrementableVersion.new(version_string)
end

#pre_numberInteger

The pre-release sequence number

The pre-release sequence number starts at 1 for each pre-release type.

Examples:

IncrementableVersionify::Semver.new('1.2.3-pre.1').pre_number # => 1

Returns:

  • (Integer)


138
139
140
# File 'lib/version_boss/semver/incrementable_version.rb', line 138

def pre_number
  pre_release_identifiers[1]
end

#pre_typeString

The pre-release type (for example, 'alpha', 'beta', 'pre', etc.)

Examples:

IncrementableVersionify::Semver.new('1.2.3-pre.1').pre_type # => 'pre'

Returns:

  • (String)


125
126
127
# File 'lib/version_boss/semver/incrementable_version.rb', line 125

def pre_type
  pre_release_identifiers[0]
end

#valid?Boolean

Create a new IncrementableVersion object

Examples:

IncrementableVersion.new('1.2.3').valid? # => true
IncrementableVersion.new('1.2.3-alpha.1+build.001').valid? # => true
IncrementableVersion.new('1.2.3-alpha').valid? # => raise VersionBoss::Error
IncrementableVersion.new('1.2.3-alpha.1.2').valid? # => raise VersionBoss::Error
IncrementableVersion.new('1.2.3-alpha.one').valid? # => raise VersionBoss::Error
IncrementableVersion.new('').valid? # => raise VersionBoss::Error

Returns:

  • (Boolean)

    true if the version string is a valid semver and meets the conditions above



34
35
36
# File 'lib/version_boss/semver/incrementable_version.rb', line 34

def valid?
  super && (pre_release.empty? || (pre_release_identifiers.size == 2 && pre_number.is_a?(Integer)))
end