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.
-
#platform ⇒ Object
readonly
Returns the value of attribute platform.
-
#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: false, platform: nil) ⇒ Range
constructor
A new instance of Range.
- #intersects(range, loose: false, platform: nil) ⇒ Object
- #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: false, platform: nil) ⇒ Range
Returns a new instance of Range.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/semantic_range/range.rb', line 5 def initialize(range, loose: false, platform: nil) range = range.raw if range.is_a?(Range) @platform = platform @raw = range @loose = loose split = range.split(/\s*\|\|\s*/) split = ['', ''] if range == '||' split = [''] if split == [] begin @set = split.map {|range| parse_range(range.strip) } rescue InvalidComparator @set = [] end raise InvalidRange.new(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 |
#platform ⇒ Object (readonly)
Returns the value of attribute platform.
3 4 5 |
# File 'lib/semantic_range/range.rb', line 3 def platform @platform 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
25 26 27 28 29 30 |
# File 'lib/semantic_range/range.rb', line 25 def format @range = @set.map do |comps| comps.join(' ').strip end.join('||').strip @range end |
#hyphen_replace(match) ⇒ Object
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 272 273 274 275 276 277 278 279 280 281 282 283 284 |
# File 'lib/semantic_range/range.rb', line 247 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 |
#intersects(range, loose: false, platform: nil) ⇒ Object
286 287 288 289 290 291 292 293 294 |
# File 'lib/semantic_range/range.rb', line 286 def intersects(range, loose: false, platform: nil) range = Range.new(range, loose: loose, platform: platform) @set.any? do |comparators| comparators.all? do |comparator| comparator.satisfies_range(range, loose: loose, platform: platform) end end end |
#isX(id) ⇒ Object
83 84 85 |
# File 'lib/semantic_range/range.rb', line 83 def isX(id) !id || id.downcase == 'x' || id == '*' end |
#parse_comparator(comp, loose) ⇒ Object
87 88 89 90 91 92 |
# File 'lib/semantic_range/range.rb', line 87 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
54 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 |
# File 'lib/semantic_range/range.rb', line 54 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^') # comma trim range = range.gsub(',', ' ') # 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
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 |
# File 'lib/semantic_range/range.rb', line 100 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
94 95 96 97 98 |
# File 'lib/semantic_range/range.rb', line 94 def replace_carets(comp, loose) comp.strip.split(/\s+/).map do |comp| replace_caret(comp, loose) end.join(' ') end |
#replace_stars(comp, loose) ⇒ Object
243 244 245 |
# File 'lib/semantic_range/range.rb', line 243 def replace_stars(comp, loose) comp.strip.gsub(STAR, '') end |
#replace_tilde(comp, loose) ⇒ Object
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/range.rb', line 151 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) if ['Rubygems', 'Packagist'].include?(platform) ">=#{mj}.#{m}.0 <#{mj.to_i+1}.0.0" else ">=#{mj}.#{m}.0 <#{mj}.#{(m.to_i + 1)}.0" end 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
145 146 147 148 149 |
# File 'lib/semantic_range/range.rb', line 145 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
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 229 230 231 232 233 234 235 236 237 238 239 240 241 |
# File 'lib/semantic_range/range.rb', line 185 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
179 180 181 182 183 |
# File 'lib/semantic_range/range.rb', line 179 def replace_x_ranges(comp, loose) comp.strip.split(/\s+/).map do |comp| replace_x_range(comp, loose) end.join(' ') end |
#test(version) ⇒ Object
32 33 34 35 36 |
# File 'lib/semantic_range/range.rb', line 32 def test(version) return false if !version version = Version.new(version, loose: @loose) if version.is_a?(String) @set.any?{|s| test_set(s, version) } end |
#test_set(set, version) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/semantic_range/range.rb', line 38 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 |