Class: Vers::VersionRange
- Inherits:
-
Object
- Object
- Vers::VersionRange
- Defined in:
- lib/vers/version_range.rb
Instance Attribute Summary collapse
-
#intervals ⇒ Object
readonly
Returns the value of attribute intervals.
-
#raw_constraints ⇒ Object
readonly
Returns the value of attribute raw_constraints.
-
#scheme ⇒ Object
readonly
Returns the value of attribute scheme.
Class Method Summary collapse
- .empty(scheme: nil) ⇒ Object
- .exact(version, scheme: nil) ⇒ Object
- .greater_than(version, inclusive: false, scheme: nil) ⇒ Object
- .less_than(version, inclusive: false, scheme: nil) ⇒ Object
- .unbounded(scheme: nil) ⇒ Object
Instance Method Summary collapse
- #complement ⇒ Object
- #contains?(version) ⇒ Boolean
- #empty? ⇒ Boolean
- #exclude(version) ⇒ Object
-
#initialize(intervals = [], raw_constraints: nil, scheme: nil) ⇒ VersionRange
constructor
A new instance of VersionRange.
- #intersect(other) ⇒ Object
- #to_s ⇒ Object
- #unbounded? ⇒ Boolean
- #union(other) ⇒ Object
Constructor Details
#initialize(intervals = [], raw_constraints: nil, scheme: nil) ⇒ VersionRange
Returns a new instance of VersionRange.
10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/vers/version_range.rb', line 10 def initialize(intervals = [], raw_constraints: nil, scheme: nil) @scheme = scheme @intervals = intervals.select { |i| i && !i.empty? } if @scheme @intervals.sort! { |a, b| compare_interval_bounds(a, b) } else @intervals.sort_by! { |i| [i.min || '', i.max || ''] } end @raw_constraints = raw_constraints merge_overlapping_intervals! end |
Instance Attribute Details
#intervals ⇒ Object (readonly)
Returns the value of attribute intervals.
8 9 10 |
# File 'lib/vers/version_range.rb', line 8 def intervals @intervals end |
#raw_constraints ⇒ Object (readonly)
Returns the value of attribute raw_constraints.
8 9 10 |
# File 'lib/vers/version_range.rb', line 8 def raw_constraints @raw_constraints end |
#scheme ⇒ Object (readonly)
Returns the value of attribute scheme.
8 9 10 |
# File 'lib/vers/version_range.rb', line 8 def scheme @scheme end |
Class Method Details
.empty(scheme: nil) ⇒ Object
22 23 24 |
# File 'lib/vers/version_range.rb', line 22 def self.empty(scheme: nil) new([], scheme: scheme) end |
.exact(version, scheme: nil) ⇒ Object
30 31 32 |
# File 'lib/vers/version_range.rb', line 30 def self.exact(version, scheme: nil) new([Interval.exact(version, scheme: scheme)], scheme: scheme) end |
.greater_than(version, inclusive: false, scheme: nil) ⇒ Object
34 35 36 |
# File 'lib/vers/version_range.rb', line 34 def self.greater_than(version, inclusive: false, scheme: nil) new([Interval.greater_than(version, inclusive: inclusive, scheme: scheme)], scheme: scheme) end |
Instance Method Details
#complement ⇒ Object
75 76 77 78 79 80 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 |
# File 'lib/vers/version_range.rb', line 75 def complement return self.class.unbounded(scheme: @scheme) if empty? return self.class.empty(scheme: @scheme) if unbounded? result_intervals = [] sorted_intervals = if @scheme intervals.sort { |a, b| compare_interval_bounds(a, b) } else intervals.sort_by { |i| i.min || '' } end first_interval = sorted_intervals.first if first_interval.min result_intervals << Interval.new( max: first_interval.min, max_inclusive: !first_interval.min_inclusive, scheme: @scheme ) end sorted_intervals.each_cons(2) do |curr, next_interval| if curr.max && next_interval.min comparison = version_compare(curr.max, next_interval.min) if comparison < 0 || (comparison == 0 && (!curr.max_inclusive || !next_interval.min_inclusive)) result_intervals << Interval.new( min: curr.max, max: next_interval.min, min_inclusive: !curr.max_inclusive, max_inclusive: !next_interval.min_inclusive, scheme: @scheme ) end end end last_interval = sorted_intervals.last if last_interval.max result_intervals << Interval.new( min: last_interval.max, min_inclusive: !last_interval.max_inclusive, scheme: @scheme ) end self.class.new(result_intervals, scheme: @scheme) end |
#contains?(version) ⇒ Boolean
50 51 52 |
# File 'lib/vers/version_range.rb', line 50 def contains?(version) intervals.any? { |interval| interval.contains?(version) } end |
#empty? ⇒ Boolean
42 43 44 |
# File 'lib/vers/version_range.rb', line 42 def empty? intervals.empty? end |
#exclude(version) ⇒ Object
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 |
# File 'lib/vers/version_range.rb', line 123 def exclude(version) return self if !contains?(version) result_intervals = [] intervals.each do |interval| if interval.contains?(version) if interval.min.nil? || version_compare(interval.min, version) < 0 result_intervals << Interval.new( min: interval.min, max: version, min_inclusive: interval.min_inclusive, max_inclusive: false, scheme: @scheme ) end if interval.max.nil? || version_compare(version, interval.max) < 0 result_intervals << Interval.new( min: version, max: interval.max, min_inclusive: false, max_inclusive: interval.max_inclusive, scheme: @scheme ) end else result_intervals << interval end end self.class.new(result_intervals, raw_constraints: raw_constraints, scheme: @scheme) end |
#intersect(other) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/vers/version_range.rb', line 54 def intersect(other) merged_scheme = @scheme || other.scheme result_intervals = [] intervals.each do |interval1| other.intervals.each do |interval2| intersection = interval1.intersect(interval2) result_intervals << intersection unless intersection.empty? end end combined_raw = (raw_constraints || intervals) + (other.raw_constraints || other.intervals) self.class.new(result_intervals, raw_constraints: combined_raw, scheme: merged_scheme) end |
#to_s ⇒ Object
157 158 159 160 |
# File 'lib/vers/version_range.rb', line 157 def to_s return "∅" if empty? return intervals.map(&:to_s).join(" ∪ ") end |
#unbounded? ⇒ Boolean
46 47 48 |
# File 'lib/vers/version_range.rb', line 46 def unbounded? intervals.length == 1 && intervals.first.unbounded? end |
#union(other) ⇒ Object
69 70 71 72 73 |
# File 'lib/vers/version_range.rb', line 69 def union(other) merged_scheme = @scheme || other.scheme combined_raw = (raw_constraints || intervals) + (other.raw_constraints || other.intervals) self.class.new(intervals + other.intervals, raw_constraints: combined_raw, scheme: merged_scheme) end |