Class: Raketeer::SemVer
- Inherits:
-
Object
- Object
- Raketeer::SemVer
- Defined in:
- lib/raketeer/sem_ver.rb
Overview
Semantic Version
This is a non-strict version of Semantic Versioning v2.0.0.
Direct Known Subclasses
Constant Summary collapse
- REGEX =
/\d+(\.\d+)?(\.\d+)?(\-[0-9A-Za-z\-\.]+)?(\+[0-9A-Za-z\-\.]+)?/.freeze
- STRICT_REGEX =
/\d+\.\d+\.\d+(\-[0-9A-Za-z\-\.]+)?(\+[0-9A-Za-z\-\.]+)?/.freeze
Instance Attribute Summary collapse
- #build_meta ⇒ Object
- #major ⇒ Object
- #minor ⇒ Object
- #patch ⇒ Object
- #prerelease ⇒ Object
-
#version ⇒ Object
If not
nil
, #to_s will only return this.
Class Method Summary collapse
Instance Method Summary collapse
- #empty? ⇒ Boolean
-
#initialize(major: nil, minor: nil, patch: nil, prerelease: nil, build_meta: nil) ⇒ SemVer
constructor
A new instance of SemVer.
- #initialize_copy(orig) ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(major: nil, minor: nil, patch: nil, prerelease: nil, build_meta: nil) ⇒ SemVer
Returns a new instance of SemVer.
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 = @major = major @minor = minor @patch = patch @prerelease = prerelease @version = nil end |
Instance Attribute Details
#build_meta ⇒ Object
26 27 28 |
# File 'lib/raketeer/sem_ver.rb', line 26 def @build_meta end |
#major ⇒ Object
27 28 29 |
# File 'lib/raketeer/sem_ver.rb', line 27 def major @major end |
#minor ⇒ Object
28 29 30 |
# File 'lib/raketeer/sem_ver.rb', line 28 def minor @minor end |
#patch ⇒ Object
29 30 31 |
# File 'lib/raketeer/sem_ver.rb', line 29 def patch @patch end |
#prerelease ⇒ Object
30 31 32 |
# File 'lib/raketeer/sem_ver.rb', line 30 def prerelease @prerelease end |
#version ⇒ Object
If not nil
, #to_s will only return this.
31 32 33 |
# File 'lib/raketeer/sem_ver.rb', line 31 def version @version end |
Class Method Details
.parse(str) ⇒ Object
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 = str.split('+',2) prerelease = [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 .length == 2 = [1].strip sem_ver. = unless .empty? end return sem_ver end |
.parse_line(line, strict: false) ⇒ Object
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
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
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
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_s ⇒ Object
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 |