Class: SemanticRange::Range
- Inherits:
-
Object
- Object
- SemanticRange::Range
- Defined in:
- lib/semantic_range/range.rb
Instance Attribute Summary collapse
-
#loose ⇒ Object
readonly
Returns the value of attribute loose.
-
#range ⇒ Object
readonly
Returns the value of attribute range.
-
#raw ⇒ Object
readonly
Returns the value of attribute raw.
-
#set ⇒ Object
readonly
Returns the value of attribute set.
Instance Method Summary collapse
- #format ⇒ Object
- #hyphen_replace(match) ⇒ Object
-
#initialize(range, loose) ⇒ Range
constructor
A new instance of Range.
- #isX(id) ⇒ Object
- #parse_comparator(comp, loose) ⇒ Object
- #parse_range(range) ⇒ Object
- #replace_caret(comp, loose) ⇒ Object
- #replace_carets(comp, loose) ⇒ Object
- #replace_stars(comp, loose) ⇒ Object
- #replace_tilde(comp, loose) ⇒ Object
- #replace_tildes(comp, loose) ⇒ Object
- #replace_x_range(comp, loose) ⇒ Object
- #replace_x_ranges(comp, loose) ⇒ Object
- #test(version) ⇒ Object
- #test_set(set, version) ⇒ Object
Constructor Details
#initialize(range, loose) ⇒ Range
Returns a new instance of Range.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
# File 'lib/semantic_range/range.rb', line 5 def initialize(range, loose) range = range.raw if range.is_a?(Range) @raw = range @loose = loose split = range.split(/\s*\|\|\s*/) split = ['', ''] if range == '||' split = [''] if split == [] @set = split.map {|range| parse_range(range.strip) } raise 'Invalid SemVer Range: ' + range if @set.empty? || @set == [[]] format end |
Instance Attribute Details
#loose ⇒ Object (readonly)
Returns the value of attribute loose.
3 4 5 |
# File 'lib/semantic_range/range.rb', line 3 def loose @loose end |
#range ⇒ Object (readonly)
Returns the value of attribute range.
3 4 5 |
# File 'lib/semantic_range/range.rb', line 3 def range @range end |
#raw ⇒ Object (readonly)
Returns the value of attribute raw.
3 4 5 |
# File 'lib/semantic_range/range.rb', line 3 def raw @raw end |
#set ⇒ Object (readonly)
Returns the value of attribute set.
3 4 5 |
# File 'lib/semantic_range/range.rb', line 3 def set @set end |
Instance Method Details
#format ⇒ Object
20 21 22 23 24 25 |
# File 'lib/semantic_range/range.rb', line 20 def format @range = @set.map do |comps| comps.join(' ').strip end.join('||').strip @range end |
#hyphen_replace(match) ⇒ Object
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 |
# File 'lib/semantic_range/range.rb', line 234 def hyphen_replace(match) from = match[1] fM = match[2] fm = match[3] fp = match[4] fpr = match[5] fb = match[6] to = match[7] tM = match[8] tm = match[9] tp = match[10] tpr = match[11] tb = match[12] if isX(fM) from = '' elsif isX(fm) from = ">=#{fM}.0.0" elsif isX(fp) from = ">=#{fM}.#{fm}.0" else from = ">=#{from}" end if isX(tM) to = '' elsif isX(tm) to = "<#{(tM.to_i + 1)}.0.0" elsif isX(tp) to = "<#{tm}.#{(tm.to_i + 1)}.0" elsif tpr to = "<=#{tM}.#{tm}.#{tp}-#{tpr}" else to = "<=#{to}" end "#{from} #{to}".strip end |
#isX(id) ⇒ Object
75 76 77 |
# File 'lib/semantic_range/range.rb', line 75 def isX(id) !id || id.downcase == 'x' || id == '*' end |
#parse_comparator(comp, loose) ⇒ Object
79 80 81 82 83 84 |
# File 'lib/semantic_range/range.rb', line 79 def parse_comparator(comp, loose) comp = replace_carets(comp, loose) comp = replace_tildes(comp, loose) comp = replace_x_ranges(comp, loose) replace_stars(comp, loose) end |
#parse_range(range) ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/semantic_range/range.rb', line 49 def parse_range(range) # expand hyphens range = range.gsub(@loose ? HYPHENRANGELOOSE : HYPHENRANGE){ hyphen_replace(Regexp.last_match) } # comparator trim range = range.gsub(COMPARATORTRIM, '\1\2\3') # tilde trim range = range.gsub(TILDETRIM, '\1~') # caret trim range = range.gsub(CARETTRIM, '\1^') # normalise spaces range = range.split(/\s+/).join(' ') set = range.split(' ').map do |comp| parse_comparator(comp, @loose) end.join(' ').split(/\s+/) set = [''] if set == [] set = set.select{|comp| !!comp.match(COMPARATORLOOSE) } if @loose set.map{|comp| Comparator.new(comp, @loose) } end |
#replace_caret(comp, loose) ⇒ Object
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 |
# File 'lib/semantic_range/range.rb', line 92 def replace_caret(comp, loose) comp.gsub(loose ? CARETLOOSE : CARET) do match = Regexp.last_match mj = match[1] m = match[2] p = match[3] pr = match[4] if isX(mj) '' elsif isX(m) ">=#{mj}.0.0 <#{(mj.to_i + 1)}.0.0" elsif isX(p) if mj == '0' ">=#{mj}.#{m}.0 <#{mj}.#{(m.to_i + 1)}.0" else ">=#{mj}.#{m}.0 <#{(mj.to_i + 1)}.0.0" end elsif pr if pr[0] != '-' pr = "-#{pr}" end if mj == '0' if m == '0' ">=#{mj}.#{m}.#{p}#{pr} <#{mj}.#{m}.#{(p.to_i + 1)}" else ">=#{mj}.#{m}.#{p}#{pr} <#{mj}.#{(m.to_i + 1)}.0" end else ">=#{mj}.#{m}.#{p}#{pr} <#{(mj.to_i + 1)}.0.0" end else if mj == '0' if m == '0' ">=#{mj}.#{m}.#{p} <#{mj}.#{m}.#{(p.to_i + 1)}" else ">=#{mj}.#{m}.#{p} <#{mj}.#{(m.to_i + 1)}.0" end else ">=#{mj}.#{m}.#{p} <#{(mj.to_i + 1)}.0.0" end end end end |
#replace_carets(comp, loose) ⇒ Object
86 87 88 89 90 |
# File 'lib/semantic_range/range.rb', line 86 def replace_carets(comp, loose) comp.strip.split(/\s+/).map do |comp| replace_caret(comp, loose) end.join(' ') end |
#replace_stars(comp, loose) ⇒ Object
230 231 232 |
# File 'lib/semantic_range/range.rb', line 230 def replace_stars(comp, loose) comp.strip.gsub(STAR, '') end |
#replace_tilde(comp, loose) ⇒ Object
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/semantic_range/range.rb', line 143 def replace_tilde(comp, loose) comp.gsub(loose ? TILDELOOSE : TILDE) do match = Regexp.last_match mj = match[1] m = match[2] p = match[3] pr = match[4] if isX(mj) '' elsif isX(m) ">=#{mj}.0.0 <#{(mj.to_i + 1)}.0.0" elsif isX(p) ">=#{mj}.#{m}.0 <#{mj}.#{(m.to_i + 1)}.0" elsif pr pr = '-' + pr if (pr[0] != '-') ">=#{mj}.#{m}.#{p}#{pr} <#{mj}.#{(m.to_i + 1)}.0" else ">=#{mj}.#{m}.#{p} <#{mj}.#{(m.to_i + 1)}.0" end end end |
#replace_tildes(comp, loose) ⇒ Object
137 138 139 140 141 |
# File 'lib/semantic_range/range.rb', line 137 def replace_tildes(comp, loose) comp.strip.split(/\s+/).map do |comp| replace_tilde(comp, loose) end.join(' ') end |
#replace_x_range(comp, loose) ⇒ Object
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 |
# File 'lib/semantic_range/range.rb', line 172 def replace_x_range(comp, loose) comp = comp.strip comp.gsub(loose ? XRANGELOOSE : XRANGE) do match = Regexp.last_match ret = match[0] gtlt = match[1] mj = match[2] m = match[3] p = match[4] pr = match[5] xM = isX(mj) xm = xM || isX(m) xp = xm || isX(p) anyX = xp gtlt = '' if gtlt == '=' && anyX if xM if gtlt == '>' || gtlt == '<' '<0.0.0' else '*' end elsif !gtlt.nil? && gtlt != '' && anyX m = 0 if xm p = 0 if xp if gtlt == '>' gtlt = '>=' if xm mj = mj.to_i + 1 m = 0 p = 0 elsif xp m = m.to_i + 1 p = 0 end elsif gtlt == '<=' gtlt = '<' if xm mj = mj.to_i + 1 else m = m.to_i + 1 end end "#{gtlt}#{mj}.#{m}.#{p}" elsif xm ">=#{mj}.0.0 <#{(mj.to_i + 1)}.0.0" elsif xp ">=#{mj}.#{m}.0 <#{mj}.#{(m.to_i + 1)}.0" else ret end end end |
#replace_x_ranges(comp, loose) ⇒ Object
166 167 168 169 170 |
# File 'lib/semantic_range/range.rb', line 166 def replace_x_ranges(comp, loose) comp.strip.split(/\s+/).map do |comp| replace_x_range(comp, loose) end.join(' ') end |
#test(version) ⇒ Object
27 28 29 30 31 |
# File 'lib/semantic_range/range.rb', line 27 def test(version) return false if !version version = Version.new(version, @loose) if version.is_a?(String) @set.any?{|s| test_set(s, version) } end |
#test_set(set, version) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/semantic_range/range.rb', line 33 def test_set(set, version) return false if set.any? {|comp| !comp.test(version) } if version.prerelease.length > 0 set.each do |comp| next if comp.semver == ANY if comp.semver.prerelease.length > 0 allowed = comp.semver return true if allowed.major == version.major && allowed.minor == version.minor && allowed.patch == version.patch end end return false end return true end |