Class: Bundler::PubGrub::VersionRange
- Inherits:
-
Object
- Object
- Bundler::PubGrub::VersionRange
- Defined in:
- lib/bundler/vendor/pub_grub/lib/pub_grub/version_range.rb
Direct Known Subclasses
Defined Under Namespace
Classes: Empty
Constant Summary collapse
Instance Attribute Summary collapse
-
#include_max ⇒ Object
(also: #include_max?)
readonly
Returns the value of attribute include_max.
-
#include_min ⇒ Object
(also: #include_min?)
readonly
Returns the value of attribute include_min.
-
#max ⇒ Object
readonly
Returns the value of attribute max.
-
#min ⇒ Object
readonly
Returns the value of attribute min.
Class Method Summary collapse
Instance Method Summary collapse
- #==(other) ⇒ Object
- #allows_all?(other) ⇒ Boolean
- #any? ⇒ Boolean
- #compare_version(version) ⇒ Object
- #contiguous_above?(other) ⇒ Boolean
- #contiguous_below?(other) ⇒ Boolean
- #contiguous_to?(other) ⇒ Boolean
- #empty? ⇒ Boolean
- #eql?(other) ⇒ Boolean
- #hash ⇒ Object
- #include?(version) ⇒ Boolean
-
#initialize(min: nil, max: nil, include_min: false, include_max: false, name: nil) ⇒ VersionRange
constructor
A new instance of VersionRange.
- #inspect ⇒ Object
- #intersect(other) ⇒ Object
- #intersects?(other) ⇒ Boolean (also: #allows_any?)
- #invert ⇒ Object
-
#partition_versions(versions) ⇒ Object
Partitions passed versions into [lower, within, higher].
- #ranges ⇒ Object
-
#select_versions(versions) ⇒ Object
Returns versions which are included by this range.
-
#span(other) ⇒ Object
The span covered by two ranges.
- #strictly_higher?(other) ⇒ Boolean
- #strictly_lower?(other) ⇒ Boolean
- #to_s ⇒ Object
- #union(other) ⇒ Object
- #upper_invert ⇒ Object
Constructor Details
#initialize(min: nil, max: nil, include_min: false, include_max: false, name: nil) ⇒ VersionRange
Returns a new instance of VersionRange.
78 79 80 81 82 83 84 85 86 87 |
# File 'lib/bundler/vendor/pub_grub/lib/pub_grub/version_range.rb', line 78 def initialize(min: nil, max: nil, include_min: false, include_max: false, name: nil) raise ArgumentError, "Ranges without a lower bound cannot have include_min == true" if !min && include_min == true raise ArgumentError, "Ranges without an upper bound cannot have include_max == true" if !max && include_max == true @min = min @max = max @include_min = include_min @include_max = include_max @name = name end |
Instance Attribute Details
#include_max ⇒ Object (readonly) Also known as: include_max?
Returns the value of attribute include_max.
5 6 7 |
# File 'lib/bundler/vendor/pub_grub/lib/pub_grub/version_range.rb', line 5 def include_max @include_max end |
#include_min ⇒ Object (readonly) Also known as: include_min?
Returns the value of attribute include_min.
5 6 7 |
# File 'lib/bundler/vendor/pub_grub/lib/pub_grub/version_range.rb', line 5 def include_min @include_min end |
#max ⇒ Object (readonly)
Returns the value of attribute max.
5 6 7 |
# File 'lib/bundler/vendor/pub_grub/lib/pub_grub/version_range.rb', line 5 def max @max end |
#min ⇒ Object (readonly)
Returns the value of attribute min.
5 6 7 |
# File 'lib/bundler/vendor/pub_grub/lib/pub_grub/version_range.rb', line 5 def min @min end |
Class Method Details
.any ⇒ Object
74 75 76 |
# File 'lib/bundler/vendor/pub_grub/lib/pub_grub/version_range.rb', line 74 def self.any new end |
.empty ⇒ Object
70 71 72 |
# File 'lib/bundler/vendor/pub_grub/lib/pub_grub/version_range.rb', line 70 def self.empty EMPTY end |
Instance Method Details
#==(other) ⇒ Object
402 403 404 405 406 407 408 |
# File 'lib/bundler/vendor/pub_grub/lib/pub_grub/version_range.rb', line 402 def ==(other) self.class == other.class && min == other.min && max == other.max && include_min == other.include_min && include_max == other.include_max end |
#allows_all?(other) ⇒ Boolean
332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 |
# File 'lib/bundler/vendor/pub_grub/lib/pub_grub/version_range.rb', line 332 def allows_all?(other) return true if other.empty? if other.is_a?(VersionUnion) return VersionUnion.new([self]).allows_all?(other) end return false if max && !other.max return false if min && !other.min if min case min <=> other.min when -1 when 0 return false if !include_min && other.include_min when 1 return false end end if max case max <=> other.max when -1 return false when 0 return false if !include_max && other.include_max when 1 end end true end |
#any? ⇒ Boolean
365 366 367 |
# File 'lib/bundler/vendor/pub_grub/lib/pub_grub/version_range.rb', line 365 def any? !min && !max end |
#compare_version(version) ⇒ Object
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 |
# File 'lib/bundler/vendor/pub_grub/lib/pub_grub/version_range.rb', line 154 def compare_version(version) if min case version <=> min when -1 return -1 when 0 return -1 if !include_min when 1 end end if max case version <=> max when -1 when 0 return 1 if !include_max when 1 return 1 end end 0 end |
#contiguous_above?(other) ⇒ Boolean
328 329 330 |
# File 'lib/bundler/vendor/pub_grub/lib/pub_grub/version_range.rb', line 328 def contiguous_above?(other) other.contiguous_below?(self) end |
#contiguous_below?(other) ⇒ Boolean
322 323 324 325 326 |
# File 'lib/bundler/vendor/pub_grub/lib/pub_grub/version_range.rb', line 322 def contiguous_below?(other) return false if !max || !other.min max == other.min && (include_max || other.include_min) end |
#contiguous_to?(other) ⇒ Boolean
315 316 317 318 319 320 |
# File 'lib/bundler/vendor/pub_grub/lib/pub_grub/version_range.rb', line 315 def contiguous_to?(other) return false if other.empty? return true if any? intersects?(other) || contiguous_below?(other) || contiguous_above?(other) end |
#empty? ⇒ Boolean
369 370 371 |
# File 'lib/bundler/vendor/pub_grub/lib/pub_grub/version_range.rb', line 369 def empty? false end |
#eql?(other) ⇒ Boolean
93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/bundler/vendor/pub_grub/lib/pub_grub/version_range.rb', line 93 def eql?(other) if other.is_a?(VersionRange) !other.empty? && min.eql?(other.min) && max.eql?(other.max) && include_min.eql?(other.include_min) && include_max.eql?(other.include_max) else ranges.eql?(other.ranges) end end |
#hash ⇒ Object
89 90 91 |
# File 'lib/bundler/vendor/pub_grub/lib/pub_grub/version_range.rb', line 89 def hash @hash ||= min.hash ^ max.hash ^ include_min.hash ^ include_max.hash end |
#include?(version) ⇒ Boolean
109 110 111 |
# File 'lib/bundler/vendor/pub_grub/lib/pub_grub/version_range.rb', line 109 def include?(version) compare_version(version) == 0 end |
#inspect ⇒ Object
377 378 379 |
# File 'lib/bundler/vendor/pub_grub/lib/pub_grub/version_range.rb', line 377 def inspect "#<#{self.class} #{to_s}>" end |
#intersect(other) ⇒ Object
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 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 |
# File 'lib/bundler/vendor/pub_grub/lib/pub_grub/version_range.rb', line 202 def intersect(other) return other if other.empty? return other.intersect(self) if other.is_a?(VersionUnion) min_range = if !min other elsif !other.min self else case min <=> other.min when 0 include_min ? other : self when -1 other when 1 self end end max_range = if !max other elsif !other.max self else case max <=> other.max when 0 include_max ? other : self when -1 self when 1 other end end if !min_range.equal?(max_range) && min_range.min && max_range.max case min_range.min <=> max_range.max when -1 when 0 if !min_range.include_min || !max_range.include_max return EMPTY end when 1 return EMPTY end end VersionRange.new( min: min_range.min, include_min: min_range.include_min, max: max_range.max, include_max: max_range.include_max ) end |
#intersects?(other) ⇒ Boolean Also known as: allows_any?
195 196 197 198 199 |
# File 'lib/bundler/vendor/pub_grub/lib/pub_grub/version_range.rb', line 195 def intersects?(other) return false if other.empty? return other.intersects?(self) if other.is_a?(VersionUnion) !strictly_lower?(other) && !strictly_higher?(other) end |
#invert ⇒ Object
387 388 389 390 391 392 393 394 395 396 397 398 399 400 |
# File 'lib/bundler/vendor/pub_grub/lib/pub_grub/version_range.rb', line 387 def invert return self.class.empty if any? low = -> { VersionRange.new(max: min, include_max: !include_min) } high = -> { VersionRange.new(min: max, include_min: !include_max) } if !min high.call elsif !max low.call else low.call.union(high.call) end end |
#partition_versions(versions) ⇒ Object
Partitions passed versions into [lower, within, higher]
versions must be sorted
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/bundler/vendor/pub_grub/lib/pub_grub/version_range.rb', line 116 def partition_versions(versions) min_index = if !min || versions.empty? 0 elsif include_min? (0..versions.size).bsearch { |i| versions[i].nil? || versions[i] >= min } else (0..versions.size).bsearch { |i| versions[i].nil? || versions[i] > min } end lower = versions.slice(0, min_index) versions = versions.slice(min_index, versions.size) max_index = if !max || versions.empty? versions.size elsif include_max? (0..versions.size).bsearch { |i| versions[i].nil? || versions[i] > max } else (0..versions.size).bsearch { |i| versions[i].nil? || versions[i] >= max } end [ lower, versions.slice(0, max_index), versions.slice(max_index, versions.size) ] end |
#ranges ⇒ Object
105 106 107 |
# File 'lib/bundler/vendor/pub_grub/lib/pub_grub/version_range.rb', line 105 def ranges [self] end |
#select_versions(versions) ⇒ Object
Returns versions which are included by this range.
versions must be sorted
148 149 150 151 152 |
# File 'lib/bundler/vendor/pub_grub/lib/pub_grub/version_range.rb', line 148 def select_versions(versions) return versions if any? partition_versions(versions)[1] end |
#span(other) ⇒ Object
The span covered by two ranges
If self and other are contiguous, this builds a union of the two ranges. (if they aren’t you are probably calling the wrong method)
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 |
# File 'lib/bundler/vendor/pub_grub/lib/pub_grub/version_range.rb', line 262 def span(other) return self if other.empty? min_range = if !min self elsif !other.min other else case min <=> other.min when 0 include_min ? self : other when -1 self when 1 other end end max_range = if !max self elsif !other.max other else case max <=> other.max when 0 include_max ? self : other when -1 other when 1 self end end VersionRange.new( min: min_range.min, include_min: min_range.include_min, max: max_range.max, include_max: max_range.include_max ) end |
#strictly_higher?(other) ⇒ Boolean
191 192 193 |
# File 'lib/bundler/vendor/pub_grub/lib/pub_grub/version_range.rb', line 191 def strictly_higher?(other) other.strictly_lower?(self) end |
#strictly_lower?(other) ⇒ Boolean
178 179 180 181 182 183 184 185 186 187 188 189 |
# File 'lib/bundler/vendor/pub_grub/lib/pub_grub/version_range.rb', line 178 def strictly_lower?(other) return false if !max || !other.min case max <=> other.min when 0 !include_max || !other.include_min when -1 true when 1 false end end |
#to_s ⇒ Object
373 374 375 |
# File 'lib/bundler/vendor/pub_grub/lib/pub_grub/version_range.rb', line 373 def to_s @name ||= constraints.join(", ") end |
#union(other) ⇒ Object
305 306 307 308 309 310 311 312 313 |
# File 'lib/bundler/vendor/pub_grub/lib/pub_grub/version_range.rb', line 305 def union(other) return other.union(self) if other.is_a?(VersionUnion) if contiguous_to?(other) span(other) else VersionUnion.union([self, other]) end end |
#upper_invert ⇒ Object
381 382 383 384 385 |
# File 'lib/bundler/vendor/pub_grub/lib/pub_grub/version_range.rb', line 381 def upper_invert return self.class.empty unless max VersionRange.new(min: max, include_min: !include_max) end |