Class: SemanticVersioning::Version

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

Constant Summary collapse

PATTERN =
/^
  (?<required>
    (?<major>[0-9]+)\.
    (?<minor>[0-9]+)\.
    (?<patch>[0-9]+)
  )
  (-(?<pre>[0-9A-Za-z\-\.]+))?
  (\+(?<build>[0-9A-Za-z\-\.]+))?
$/x

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input, segment = Segment) ⇒ Version

Returns a new instance of Version.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/semantic_versioning/version.rb', line 21

def initialize(input, segment = Segment)
  if (m = input.match(PATTERN))
    @input = input
    @major, @minor, @patch = m['major'].to_i, m['minor'].to_i, m['patch'].to_i
    @required, @pre, @build = m['required'], m['pre'], m['build']
    @segments = [
      segment.new(@required),
      segment.new(@pre),
      segment.new(@build)
    ]
  else
    raise ParsingError, 'String input not correctly formatted for Semantic Versioning'
  end
end

Instance Attribute Details

#buildObject

Returns the value of attribute build.



18
19
20
# File 'lib/semantic_versioning/version.rb', line 18

def build
  @build
end

#majorObject (readonly)

Returns the value of attribute major.



17
18
19
# File 'lib/semantic_versioning/version.rb', line 17

def major
  @major
end

#minorObject (readonly)

Returns the value of attribute minor.



17
18
19
# File 'lib/semantic_versioning/version.rb', line 17

def minor
  @minor
end

#patchObject (readonly)

Returns the value of attribute patch.



17
18
19
# File 'lib/semantic_versioning/version.rb', line 17

def patch
  @patch
end

#preObject Also known as: prerelease

Returns the value of attribute pre.



18
19
20
# File 'lib/semantic_versioning/version.rb', line 18

def pre
  @pre
end

#requiredObject (readonly)

Returns the value of attribute required.



17
18
19
# File 'lib/semantic_versioning/version.rb', line 17

def required
  @required
end

#segmentsObject (readonly)

Returns the value of attribute segments.



17
18
19
# File 'lib/semantic_versioning/version.rb', line 17

def segments
  @segments
end

Instance Method Details

#<=>(other) ⇒ Object



36
37
38
39
40
41
42
43
44
45
# File 'lib/semantic_versioning/version.rb', line 36

def <=>(other)
  return nil unless other.is_a?(Version)
  return 0 if to_s == other.to_s
  if required == other.required
    return -1 if pre && other.pre.nil?
    return 1 if pre.nil? && other.pre
  end
  scores = segments.map.with_index { |s, idx| s <=> other.segments[idx] }
  scores.compact.detect { |s| s.abs == 1 }
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/semantic_versioning/version.rb', line 47

def eql?(other)
  self == other
end

#hashObject



51
52
53
# File 'lib/semantic_versioning/version.rb', line 51

def hash
  to_s.hash
end

#increment(identifier) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/semantic_versioning/version.rb', line 62

def increment(identifier)
  case identifier
  when :major
    @major += 1
    reset(:minor, :patch)
  when :minor
    @minor += 1
    reset(:patch)
  when :patch
    @patch += 1
  else
    raise IncrementError, 'Only :major, :minor and :patch attributes may be incremented'
  end
  clear_optional_identifiers
end

#to_sObject



55
56
57
58
59
60
# File 'lib/semantic_versioning/version.rb', line 55

def to_s
  version = "#{@major}.#{@minor}.#{@patch}"
  version += "-#{@pre}" unless pre.nil?
  version += "+#{@build}" unless build.nil?
  version
end