Class: VersionBoss::Gem::IncrementableVersion

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

Overview

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

A 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

#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::Gem::Version

Instance Method Details

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

Increment the major version

Examples:

VersionBoss::IncrementableVersion.new('1.2.3').next_major # =>
  VersionBoss::IncrementableVersion.new('2.0.0')

Returns:



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

def next_major(pre: false, pre_type: DEFAULT_PRE_TYPE)
  version_string = "#{major.to_i + 1}.0.0"
  version_string += ".#{pre_type}1" if pre
  IncrementableVersion.new(version_string)
end

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

Increment the minor version

Examples:

VersionBoss::IncrementableVersion.new('1.2.3').next_minor # =>
  VersionBoss::IncrementableVersion.new('1.3.0')

Returns:



62
63
64
65
66
# File 'lib/version_boss/gem/incrementable_version.rb', line 62

def next_minor(pre: false, pre_type: DEFAULT_PRE_TYPE)
  version_string = "#{major}.#{minor.to_i + 1}.0"
  version_string += ".#{pre_type}1" if pre
  IncrementableVersion.new(version_string)
end

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

Increment the patch version

Examples:

VersionBoss::IncrementableVersion.new('1.2.3').next_patch # =>
  VersionBoss::IncrementableVersion.new('1.2.1')

Returns:



76
77
78
79
80
# File 'lib/version_boss/gem/incrementable_version.rb', line 76

def next_patch(pre: false, pre_type: DEFAULT_PRE_TYPE)
  version_string = "#{major}.#{minor}.#{patch.to_i + 1}"
  version_string += ".#{pre_type}1" if pre
  IncrementableVersion.new(version_string)
end

#next_pre(pre_type: nil) ⇒ IncrementableVersion

Increment the pre_release part of the version

Examples:

VersionBoss::IncrementableVersion.new('1.2.3.pre.1').next_patch.to_s # => '1.2.3.pre.2'

Returns:



89
90
91
92
93
94
# File 'lib/version_boss/gem/incrementable_version.rb', line 89

def next_pre(pre_type: nil)
  assert_is_a_pre_release_version
  version_string = "#{major}.#{minor}.#{patch}"
  version_string += next_pre_part(pre_type)
  IncrementableVersion.new(version_string)
end

#next_releaseIncrementableVersion

Drop the pre-release part of the version

Examples:

VersionBoss::IncrementableVersion.new('1.2.3.pre.1').next_release.to_s # => '1.2.3'

Returns:

Raises:



104
105
106
107
108
# File 'lib/version_boss/gem/incrementable_version.rb', line 104

def next_release
  assert_is_a_pre_release_version
  version_string = "#{major}.#{minor}.#{patch}"
  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:

IncrementableVersion.new('1.2.3.pre.1').pre_number # => 1

Returns:

  • (Integer)


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

def pre_number
  pre_release_identifiers[1].identifier
end

#pre_number_prefixString

The pre-release sequence number

The pre-release identifier can optionally prefix the pre-release sequence number with a period.

Examples:

IncrementableVersion.new('1.2.3.pre.1').pre_number_prefix # => '.'
IncrementableVersion.new('1.2.3.pre1').pre_number_prefix # => ''

Returns:

  • (String)


161
162
163
# File 'lib/version_boss/gem/incrementable_version.rb', line 161

def pre_number_prefix
  pre_release_identifiers[1].prefix
end

#pre_typeString

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

Examples:

VersionBoss::IncrementableVersion.new('1.2.3.pre1').pre_type # => 'pre'

Returns:

  • (String)


117
118
119
# File 'lib/version_boss/gem/incrementable_version.rb', line 117

def pre_type
  pre_release_identifiers[0].identifier
end

#pre_type_prefix'.', ''

Returns the prefix of the pre-release type

Ruby Gem versions can optionally prefix the pre-release type with a period.

Semver requires a hyphen prefix.

Examples:

VersionBoss::GemVersion.new('1.2.3.pre1').pre_type_prefix # => '-'
VersionBoss::IncrementableVersion.new('1.2.3.pre1').pre_type # => 'pre'

Returns:

  • ('.', '')


134
135
136
# File 'lib/version_boss/gem/incrementable_version.rb', line 134

def pre_type_prefix
  pre_release_identifiers[0].prefix
end

#valid?Boolean

Create a new IncrementableVersion object

Examples:

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

Returns:

  • (Boolean)

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



33
34
35
# File 'lib/version_boss/gem/incrementable_version.rb', line 33

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