Class: Raketeer::SemVer

Inherits:
Object
  • Object
show all
Defined in:
lib/raketeer/sem_ver.rb

Overview

Semantic Version

This is a non-strict version of Semantic Versioning v2.0.0.

See Also:

Author:

  • Jonathan Bradley Whited

Since:

  • 0.2.4

Direct Known Subclasses

BumpVer

Constant Summary collapse

REGEX =

Since:

  • 0.2.4

/\d+(\.\d+)?(\.\d+)?(\-[0-9A-Za-z\-\.]+)?(\+[0-9A-Za-z\-\.]+)?/.freeze
STRICT_REGEX =

Since:

  • 0.2.4

/\d+\.\d+\.\d+(\-[0-9A-Za-z\-\.]+)?(\+[0-9A-Za-z\-\.]+)?/.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(major: nil, minor: nil, patch: nil, prerelease: nil, build_meta: nil) ⇒ SemVer

Returns a new instance of SemVer.

Since:

  • 0.2.4



33
34
35
36
37
38
39
40
# File 'lib/raketeer/sem_ver.rb', line 33

def initialize(major: nil,minor: nil,patch: nil,prerelease: nil,build_meta: nil)
  @build_meta = build_meta
  @major = major
  @minor = minor
  @patch = patch
  @prerelease = prerelease
  @version = nil
end

Instance Attribute Details

#build_metaObject

Since:

  • 0.2.4



26
27
28
# File 'lib/raketeer/sem_ver.rb', line 26

def build_meta
  @build_meta
end

#majorObject

Since:

  • 0.2.4



27
28
29
# File 'lib/raketeer/sem_ver.rb', line 27

def major
  @major
end

#minorObject

Since:

  • 0.2.4



28
29
30
# File 'lib/raketeer/sem_ver.rb', line 28

def minor
  @minor
end

#patchObject

Since:

  • 0.2.4



29
30
31
# File 'lib/raketeer/sem_ver.rb', line 29

def patch
  @patch
end

#prereleaseObject

Since:

  • 0.2.4



30
31
32
# File 'lib/raketeer/sem_ver.rb', line 30

def prerelease
  @prerelease
end

#versionObject

If not nil, #to_s will only return this.

Since:

  • 0.2.4



31
32
33
# File 'lib/raketeer/sem_ver.rb', line 31

def version
  @version
end

Class Method Details

.parse(str) ⇒ Object

Since:

  • 0.2.4



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/raketeer/sem_ver.rb', line 55

def self.parse(str)
  # Parsing backwards makes the logic easier
  build_meta = str.split('+',2)
  prerelease = build_meta[0].split('-',2)
  numbers = prerelease[0].split('.',3)

  sem_ver = SemVer.new
  sem_ver.major = numbers[0].to_i # There must always be a major version

  if numbers.length >= 2
    minor = numbers[1].strip
    sem_ver.minor = minor.to_i unless minor.empty?

    if numbers.length == 3
      patch = numbers[2].strip
      sem_ver.patch = patch.to_i unless patch.empty?
    end
  end

  if prerelease.length == 2
    prerelease = prerelease[1].strip
    sem_ver.prerelease = prerelease unless prerelease.empty?
  end

  if build_meta.length == 2
    build_meta = build_meta[1].strip
    sem_ver.build_meta = build_meta unless build_meta.empty?
  end

  return sem_ver
end

.parse_line(line, strict: false) ⇒ Object

Since:

  • 0.2.4



87
88
89
90
91
92
# File 'lib/raketeer/sem_ver.rb', line 87

def self.parse_line(line,strict: false)
  str = line[regex(strict)]

  return nil if str.nil? || (str = str.strip).empty?
  return parse(str)
end

.regex(strict = false) ⇒ Object

Since:

  • 0.2.4



103
104
105
# File 'lib/raketeer/sem_ver.rb', line 103

def self.regex(strict=false)
  return strict ? STRICT_REGEX : REGEX
end

Instance Method Details

#empty?Boolean

Returns:

  • (Boolean)

Since:

  • 0.2.4



94
95
96
97
98
99
100
101
# File 'lib/raketeer/sem_ver.rb', line 94

def empty?
  return @build_meta.nil? &&
         @major.nil? &&
         @minor.nil? &&
         @patch.nil? &&
         @prerelease.nil? &&
         @version.nil?
end

#initialize_copy(orig) ⇒ Object

Since:

  • 0.2.4



42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/raketeer/sem_ver.rb', line 42

def initialize_copy(orig)
  super(orig)

  copy = caller[0].to_s.include?('clone') ? :clone : :dup

  @build_meta = @build_meta.__send__(copy)
  @major = @major.__send__(copy)
  @minor = @minor.__send__(copy)
  @patch = @patch.__send__(copy)
  @prerelease = @prerelease.__send__(copy)
  @version = @version.__send__(copy)
end

#to_sObject

Since:

  • 0.2.4



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/raketeer/sem_ver.rb', line 107

def to_s
  s = ''.dup

  if !@version.nil?
    s << @version.to_s
  elsif !@major.nil?
    s << @major.to_s
    s << ".#{@minor}" unless @minor.nil?
    s << ".#{@patch}" unless @patch.nil?
    s << "-#{@prerelease}" unless @prerelease.nil?
    s << "+#{@build_meta}" unless @build_meta.nil?
  end

  return s
end