Class: Philiprehberger::VersionCompare::SemanticVersion

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/philiprehberger/version_compare.rb

Overview

A parsed semantic version with comparison support

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(major, minor, patch, pre_release: nil, build_metadata: nil) ⇒ SemanticVersion

Returns a new instance of SemanticVersion.

Parameters:

  • major (Integer)

    major version number

  • minor (Integer)

    minor version number

  • patch (Integer)

    patch version number

  • pre_release (String, nil) (defaults to: nil)

    pre-release identifier

  • build_metadata (String, nil) (defaults to: nil)

    build metadata (ignored in comparison)



20
21
22
23
24
25
26
# File 'lib/philiprehberger/version_compare.rb', line 20

def initialize(major, minor, patch, pre_release: nil, build_metadata: nil)
  @major = major
  @minor = minor
  @patch = patch
  @pre_release = pre_release
  @build_metadata = 
end

Instance Attribute Details

#build_metadataObject (readonly)

Returns the value of attribute build_metadata.



13
14
15
# File 'lib/philiprehberger/version_compare.rb', line 13

def 
  @build_metadata
end

#majorObject (readonly)

Returns the value of attribute major.



13
14
15
# File 'lib/philiprehberger/version_compare.rb', line 13

def major
  @major
end

#minorObject (readonly)

Returns the value of attribute minor.



13
14
15
# File 'lib/philiprehberger/version_compare.rb', line 13

def minor
  @minor
end

#patchObject (readonly)

Returns the value of attribute patch.



13
14
15
# File 'lib/philiprehberger/version_compare.rb', line 13

def patch
  @patch
end

#pre_releaseObject (readonly)

Returns the value of attribute pre_release.



13
14
15
# File 'lib/philiprehberger/version_compare.rb', line 13

def pre_release
  @pre_release
end

Instance Method Details

#<=>(other) ⇒ Integer

Compare two versions following SemVer precedence

Parameters:

Returns:

  • (Integer)

    -1, 0, or 1



32
33
34
35
36
37
38
39
# File 'lib/philiprehberger/version_compare.rb', line 32

def <=>(other)
  return nil unless other.is_a?(SemanticVersion)

  result = [major, minor, patch] <=> [other.major, other.minor, other.patch]
  return result unless result.zero?

  compare_pre_release(other)
end

#inspectObject



113
114
115
# File 'lib/philiprehberger/version_compare.rb', line 113

def inspect
  "#<Version #{self}>"
end

#next_majorSemanticVersion

Return a new version with major incremented, minor and patch reset to 0

Returns:



73
74
75
# File 'lib/philiprehberger/version_compare.rb', line 73

def next_major
  SemanticVersion.new(major + 1, 0, 0)
end

#next_minorSemanticVersion

Return a new version with minor incremented, patch reset to 0

Returns:



80
81
82
# File 'lib/philiprehberger/version_compare.rb', line 80

def next_minor
  SemanticVersion.new(major, minor + 1, 0)
end

#next_patchSemanticVersion

Return a new version with patch incremented

Returns:



87
88
89
# File 'lib/philiprehberger/version_compare.rb', line 87

def next_patch
  SemanticVersion.new(major, minor, patch + 1)
end

#satisfies?(constraint) ⇒ Boolean

Check if this version satisfies a constraint string

Parameters:

  • constraint (String)

    constraint like “>= 1.0.0”, “~> 2.1”, “!= 1.2.3”, “< 3.0.0”

Returns:

  • (Boolean)

    true if the version satisfies the constraint

Raises:

  • (Error)

    if the constraint format is invalid



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/philiprehberger/version_compare.rb', line 46

def satisfies?(constraint)
  operator, version_str = parse_constraint(constraint)
  other = VersionCompare.parse(version_str)

  case operator
  when '>='
    self >= other
  when '<='
    self <= other
  when '>'
    self > other
  when '<'
    self < other
  when '='
    self == other
  when '!='
    self != other
  when '~>'
    pessimistic_match?(other)
  else
    raise Error, "unknown operator: #{operator}"
  end
end

#stable?Boolean

Check if the version is stable (no pre-release tag)

Returns:

  • (Boolean)

    true if no pre-release identifier



94
95
96
# File 'lib/philiprehberger/version_compare.rb', line 94

def stable?
  pre_release.nil?
end

#to_aArray<Integer>

Return the version components as an array

Returns:

  • (Array<Integer>)
    major, minor, patch


101
102
103
# File 'lib/philiprehberger/version_compare.rb', line 101

def to_a
  [major, minor, patch]
end

#to_sString

Returns version string representation.

Returns:

  • (String)

    version string representation



106
107
108
109
110
111
# File 'lib/philiprehberger/version_compare.rb', line 106

def to_s
  base = "#{major}.#{minor}.#{patch}"
  base = "#{base}-#{pre_release}" if pre_release
  base = "#{base}+#{}" if 
  base
end