Class: SemanticRange::Version
- Inherits:
-
Object
- Object
- SemanticRange::Version
- Defined in:
- lib/semantic_range/version.rb
Instance Attribute Summary collapse
-
#major ⇒ Object
readonly
Returns the value of attribute major.
-
#minor ⇒ Object
readonly
Returns the value of attribute minor.
-
#patch ⇒ Object
readonly
Returns the value of attribute patch.
-
#prerelease ⇒ Object
readonly
Returns the value of attribute prerelease.
Class Method Summary collapse
Instance Method Summary collapse
- #compare(other) ⇒ Object
- #compare_main(other) ⇒ Object
- #compare_pre(other) ⇒ Object
- #format ⇒ Object
- #increment!(release, identifier) ⇒ Object
-
#initialize(version, loose = false) ⇒ Version
constructor
A new instance of Version.
- #raw ⇒ Object
- #to_s ⇒ Object
- #truthy(val) ⇒ Object
- #version ⇒ Object
Constructor Details
#initialize(version, loose = false) ⇒ Version
Returns a new instance of Version.
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/semantic_range/version.rb', line 7 def initialize(version, loose = false) @raw = version @loose = loose @raw = version.raw if version.is_a?(Version) match = @raw.strip.match(loose ? LOOSE : FULL) raise NoMatchFound.new(version) if match.nil? @major = match[1] ? match[1].to_i : 0 @minor = match[2] ? match[2].to_i : 0 @patch = match[3] ? match[3].to_i : 0 @prerelease = PreRelease.new match[4] @build = match[5] ? match[5].split('.') : [] end |
Instance Attribute Details
#major ⇒ Object (readonly)
Returns the value of attribute major.
5 6 7 |
# File 'lib/semantic_range/version.rb', line 5 def major @major end |
#minor ⇒ Object (readonly)
Returns the value of attribute minor.
5 6 7 |
# File 'lib/semantic_range/version.rb', line 5 def minor @minor end |
#patch ⇒ Object (readonly)
Returns the value of attribute patch.
5 6 7 |
# File 'lib/semantic_range/version.rb', line 5 def patch @patch end |
#prerelease ⇒ Object (readonly)
Returns the value of attribute prerelease.
5 6 7 |
# File 'lib/semantic_range/version.rb', line 5 def prerelease @prerelease end |
Class Method Details
.compare_identifiers(a, b) ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/semantic_range/version.rb', line 65 def self.compare_identifiers(a,b) anum = /^[0-9]+$/.match(a.to_s) bnum = /^[0-9]+$/.match(b.to_s) if anum && bnum a = a.to_i b = b.to_i end return (anum && !bnum) ? -1 : (bnum && !anum) ? 1 : a < b ? -1 : a > b ? 1 : 0; end |
Instance Method Details
#compare(other) ⇒ Object
43 44 45 46 47 |
# File 'lib/semantic_range/version.rb', line 43 def compare(other) other = Version.new(other, @loose) unless other.is_a?(Version) res = truthy(compare_main(other)) || truthy(compare_pre(other)) res.is_a?(FalseClass) ? 0 : res end |
#compare_main(other) ⇒ Object
49 50 51 52 53 54 |
# File 'lib/semantic_range/version.rb', line 49 def compare_main(other) other = Version.new(other, @loose) unless other.is_a?(Version) truthy(self.class.compare_identifiers(@major, other.major)) || truthy(self.class.compare_identifiers(@minor, other.minor)) || truthy(self.class.compare_identifiers(@patch, other.patch)) end |
#compare_pre(other) ⇒ Object
61 62 63 |
# File 'lib/semantic_range/version.rb', line 61 def compare_pre(other) prerelease <=> other.prerelease end |
#format ⇒ Object
26 27 28 29 |
# File 'lib/semantic_range/version.rb', line 26 def format v = "#{@major}.#{@minor}.#{@patch}" prerelease.length > 0 ? "#{v}-#{prerelease}" : v end |
#increment!(release, identifier) ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 |
# File 'lib/semantic_range/version.rb', line 81 def increment!(release, identifier) case release when 'premajor' @prerelease.clear! @patch = 0 @minor = 0 @major = @major + 1 increment! 'pre', identifier when 'preminor' @prerelease.clear! @patch = 0 @minor = @minor + 1 increment! 'pre', identifier when 'prepatch' # If this is already a prerelease, it will bump to the next version # drop any prereleases that might already exist, since they are not # relevant at this point. @prerelease.clear! increment! 'patch', identifier increment! 'pre', identifier # If the input is a non-prerelease version, this acts the same as # prepatch. when 'prerelease' if @prerelease.empty? increment! 'patch', identifier end increment! 'pre', identifier when 'major' # If this is a pre-major version, bump up to the same major version. # Otherwise increment major. # 1.0.0-5 bumps to 1.0.0 # 1.1.0 bumps to 2.0.0 if @minor != 0 || @patch != 0 || @prerelease.empty? @major = @major + 1 end @minor = 0 @patch = 0 @prerelease.clear! when 'minor' # If this is a pre-minor version, bump up to the same minor version. # Otherwise increment minor. # 1.2.0-5 bumps to 1.2.0 # 1.2.1 bumps to 1.3.0 if @patch != 0 || @prerelease.empty? @minor = @minor + 1 end @patch = 0 @prerelease.clear! when 'patch' # If this is not a pre-release version, it will increment the patch. # If it is a pre-release it will bump up to the same patch version. # 1.2.0-5 patches to 1.2.0 # 1.2.0 patches to 1.2.1 if @prerelease.empty? @patch = @patch + 1 end @prerelease.clear! # This probably shouldn't be used publicly. # 1.0.0 "pre" would become 1.0.0-0 which is the wrong direction. when 'pre' if @prerelease.empty? @prerelease.zero! else parts = @prerelease.parts i = parts.length while i >= 0 if parts[i].is_a?(Fixnum) parts[i] += 1 i = -2 else i = i-1 end end if i == -1 # didn't increment anything parts << 0 end @prerelease = PreRelease.new(parts.join(".")) end if identifier # 1.2.0-beta.1 bumps to 1.2.0-beta.2, # 1.2.0-beta.fooblz or 1.2.0-beta bumps to 1.2.0-beta.0 if @prerelease.parts[0] == identifier unless @prerelease.parts[1].kind_of?(Fixnum) @prerelease = PreRelease.new([identifier, 0].join('.')) end else @prerelease = PreRelease.new([identifier, 0].join('.')) end end else raise InvalidIncrement.new release end self end |
#raw ⇒ Object
39 40 41 |
# File 'lib/semantic_range/version.rb', line 39 def raw version end |
#to_s ⇒ Object
31 32 33 |
# File 'lib/semantic_range/version.rb', line 31 def to_s @version end |
#truthy(val) ⇒ Object
56 57 58 59 |
# File 'lib/semantic_range/version.rb', line 56 def truthy(val) return val unless val.is_a?(Integer) val.zero? ? false : val end |
#version ⇒ Object
35 36 37 |
# File 'lib/semantic_range/version.rb', line 35 def version format end |