Class: VersionManager::ReleaseVersion

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/version-manager/release_version.rb

Defined Under Namespace

Classes: IncorrentFormat

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*version_input) ⇒ ReleaseVersion



12
13
14
15
16
17
18
19
20
21
# File 'lib/version-manager/release_version.rb', line 12

def initialize(*version_input)
  version_components = Array(version_input.dup.flatten)
  if version_components.size == 1
    version_components = version_components.first.scan(/(\d+)\.{1}(\d+)\.?(\d*)(?:--(\w+))?/).flatten
    raise ArgumentError, 'Incorrect version format' if version_components.all?(&:nil?) || version_components.empty?
  end
  @major, @minor, @patch = version_components[0..2].map(&:to_i)
  @special = version_components[3]
  recalculate_parts
end

Instance Attribute Details

#majorObject (readonly)

Returns the value of attribute major.



65
66
67
# File 'lib/version-manager/release_version.rb', line 65

def major
  @major
end

#minorObject (readonly)

Returns the value of attribute minor.



65
66
67
# File 'lib/version-manager/release_version.rb', line 65

def minor
  @minor
end

#partsObject (readonly)

Returns the value of attribute parts.



65
66
67
# File 'lib/version-manager/release_version.rb', line 65

def parts
  @parts
end

#patchObject (readonly)

Returns the value of attribute patch.



65
66
67
# File 'lib/version-manager/release_version.rb', line 65

def patch
  @patch
end

#specialObject (readonly)

Returns the value of attribute special.



65
66
67
# File 'lib/version-manager/release_version.rb', line 65

def special
  @special
end

Class Method Details

.valid?(version) ⇒ Boolean



59
60
61
62
63
# File 'lib/version-manager/release_version.rb', line 59

def self.valid?(version)
  new(version) && true
rescue ArgumentError
  false
end

Instance Method Details

#-(other) ⇒ Object



38
39
40
# File 'lib/version-manager/release_version.rb', line 38

def -(other)
  self.class.new(parts.zip(other.parts).map { |x, y| x - y })
end

#<=>(other) ⇒ Object



32
33
34
35
36
# File 'lib/version-manager/release_version.rb', line 32

def <=>(other)
  parts.zip(other.parts)
       .map { |this, other_part| this <=> other_part }
       .find { |res| res != 0 } || 0
end

#bump(release_type) ⇒ Object

Raises:

  • (ArgumentError)


42
43
44
45
# File 'lib/version-manager/release_version.rb', line 42

def bump(release_type)
  raise ArgumentError, 'Unknown release type' unless i(major minor patch).include?(release_type.to_sym)
  public_send("bump_#{release_type}")
end

#bump_majorObject



47
48
49
# File 'lib/version-manager/release_version.rb', line 47

def bump_major
  self.class.new(@major + 1, 0, 0)
end

#bump_minorObject



51
52
53
# File 'lib/version-manager/release_version.rb', line 51

def bump_minor
  self.class.new(@major, @minor + 1, 0)
end

#bump_patchObject



55
56
57
# File 'lib/version-manager/release_version.rb', line 55

def bump_patch
  self.class.new(@major, @minor, @patch + 1)
end

#short_versionObject



28
29
30
# File 'lib/version-manager/release_version.rb', line 28

def short_version
  [major, minor].map(&:to_i).join('.')
end

#to_sObject



23
24
25
26
# File 'lib/version-manager/release_version.rb', line 23

def to_s
  res = parts.map(&:to_i).join('.')
  [res, special].compact.join('--')
end