Class: Vers::Interval
- Inherits:
-
Object
- Object
- Vers::Interval
- Defined in:
- lib/vers/interval.rb
Instance Attribute Summary collapse
-
#max ⇒ Object
readonly
Returns the value of attribute max.
-
#max_inclusive ⇒ Object
readonly
Returns the value of attribute max_inclusive.
-
#min ⇒ Object
readonly
Returns the value of attribute min.
-
#min_inclusive ⇒ Object
readonly
Returns the value of attribute min_inclusive.
-
#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
- #adjacent?(other) ⇒ Boolean
- #contains?(version) ⇒ Boolean
- #empty? ⇒ Boolean
-
#initialize(min: nil, max: nil, min_inclusive: true, max_inclusive: true, scheme: nil) ⇒ Interval
constructor
A new instance of Interval.
- #intersect(other) ⇒ Object
- #overlaps?(other) ⇒ Boolean
- #to_s ⇒ Object
- #unbounded? ⇒ Boolean
- #union(other) ⇒ Object
Constructor Details
#initialize(min: nil, max: nil, min_inclusive: true, max_inclusive: true, scheme: nil) ⇒ Interval
Returns a new instance of Interval.
9 10 11 12 13 14 15 16 |
# File 'lib/vers/interval.rb', line 9 def initialize(min: nil, max: nil, min_inclusive: true, max_inclusive: true, scheme: nil) @min = min @max = max @min_inclusive = min_inclusive @max_inclusive = max_inclusive @scheme = scheme @empty = compute_empty end |
Instance Attribute Details
#max ⇒ Object (readonly)
Returns the value of attribute max.
7 8 9 |
# File 'lib/vers/interval.rb', line 7 def max @max end |
#max_inclusive ⇒ Object (readonly)
Returns the value of attribute max_inclusive.
7 8 9 |
# File 'lib/vers/interval.rb', line 7 def max_inclusive @max_inclusive end |
#min ⇒ Object (readonly)
Returns the value of attribute min.
7 8 9 |
# File 'lib/vers/interval.rb', line 7 def min @min end |
#min_inclusive ⇒ Object (readonly)
Returns the value of attribute min_inclusive.
7 8 9 |
# File 'lib/vers/interval.rb', line 7 def min_inclusive @min_inclusive end |
#scheme ⇒ Object (readonly)
Returns the value of attribute scheme.
7 8 9 |
# File 'lib/vers/interval.rb', line 7 def scheme @scheme end |
Class Method Details
.empty(scheme: nil) ⇒ Object
18 19 20 |
# File 'lib/vers/interval.rb', line 18 def self.empty(scheme: nil) new(min: "1", max: "0", min_inclusive: true, max_inclusive: true, scheme: scheme) end |
.exact(version, scheme: nil) ⇒ Object
26 27 28 |
# File 'lib/vers/interval.rb', line 26 def self.exact(version, scheme: nil) new(min: version, max: version, min_inclusive: true, max_inclusive: true, scheme: scheme) end |
.greater_than(version, inclusive: false, scheme: nil) ⇒ Object
30 31 32 |
# File 'lib/vers/interval.rb', line 30 def self.greater_than(version, inclusive: false, scheme: nil) new(min: version, min_inclusive: inclusive, scheme: scheme) end |
.less_than(version, inclusive: false, scheme: nil) ⇒ Object
34 35 36 |
# File 'lib/vers/interval.rb', line 34 def self.less_than(version, inclusive: false, scheme: nil) new(max: version, max_inclusive: inclusive, scheme: scheme) end |
.unbounded(scheme: nil) ⇒ Object
22 23 24 |
# File 'lib/vers/interval.rb', line 22 def self.unbounded(scheme: nil) new(scheme: scheme) end |
Instance Method Details
#adjacent?(other) ⇒ Boolean
198 199 200 201 202 203 204 205 206 207 208 209 210 |
# File 'lib/vers/interval.rb', line 198 def adjacent?(other) return false if empty? || other.empty? if max && other.min && version_compare(max, other.min) == 0 return (max_inclusive && !other.min_inclusive) || (!max_inclusive && other.min_inclusive) end if min && other.max && version_compare(min, other.max) == 0 return (min_inclusive && !other.max_inclusive) || (!min_inclusive && other.max_inclusive) end false end |
#contains?(version) ⇒ Boolean
46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/vers/interval.rb', line 46 def contains?(version) return false if empty? return true if unbounded? within_min = min.nil? || (min_inclusive ? version_compare(version, min) >= 0 : version_compare(version, min) > 0) within_max = max.nil? || (max_inclusive ? version_compare(version, max) <= 0 : version_compare(version, max) < 0) within_min && within_max end |
#empty? ⇒ Boolean
38 39 40 |
# File 'lib/vers/interval.rb', line 38 def empty? @empty end |
#intersect(other) ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 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 |
# File 'lib/vers/interval.rb', line 59 def intersect(other) merged_scheme = @scheme || other.scheme return self.class.empty(scheme: merged_scheme) if empty? || other.empty? new_min = nil new_min_inclusive = true new_max = nil new_max_inclusive = true if min && other.min comparison = version_compare(min, other.min) if comparison > 0 new_min = min new_min_inclusive = min_inclusive elsif comparison < 0 new_min = other.min new_min_inclusive = other.min_inclusive else new_min = min new_min_inclusive = min_inclusive && other.min_inclusive end elsif min new_min = min new_min_inclusive = min_inclusive elsif other.min new_min = other.min new_min_inclusive = other.min_inclusive end if max && other.max comparison = version_compare(max, other.max) if comparison < 0 new_max = max new_max_inclusive = max_inclusive elsif comparison > 0 new_max = other.max new_max_inclusive = other.max_inclusive else new_max = max new_max_inclusive = max_inclusive && other.max_inclusive end elsif max new_max = max new_max_inclusive = max_inclusive elsif other.max new_max = other.max new_max_inclusive = other.max_inclusive end self.class.new( min: new_min, max: new_max, min_inclusive: new_min_inclusive, max_inclusive: new_max_inclusive, scheme: merged_scheme ) end |
#overlaps?(other) ⇒ Boolean
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 |
# File 'lib/vers/interval.rb', line 178 def overlaps?(other) return false if empty? || other.empty? return true if unbounded? || other.unbounded? # Check if the intervals can't overlap by comparing bounds directly if max && other.min cmp = version_compare(max, other.min) return false if cmp < 0 return false if cmp == 0 && (!max_inclusive || !other.min_inclusive) end if min && other.max cmp = version_compare(min, other.max) return false if cmp > 0 return false if cmp == 0 && (!min_inclusive || !other.max_inclusive) end true end |
#to_s ⇒ Object
212 213 214 215 216 217 218 219 220 221 222 |
# File 'lib/vers/interval.rb', line 212 def to_s return "∅" if empty? return "(-∞,+∞)" if unbounded? min_bracket = min_inclusive ? "[" : "(" max_bracket = max_inclusive ? "]" : ")" min_str = min || "-∞" max_str = max || "+∞" "#{min_bracket}#{min_str},#{max_str}#{max_bracket}" end |
#unbounded? ⇒ Boolean
42 43 44 |
# File 'lib/vers/interval.rb', line 42 def unbounded? min.nil? && max.nil? end |
#union(other) ⇒ Object
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 |
# File 'lib/vers/interval.rb', line 117 def union(other) return other if empty? return self if other.empty? return nil unless overlaps?(other) || adjacent?(other) merged_scheme = @scheme || other.scheme new_min = nil new_min_inclusive = true new_max = nil new_max_inclusive = true if min && other.min comparison = version_compare(min, other.min) if comparison < 0 new_min = min new_min_inclusive = min_inclusive elsif comparison > 0 new_min = other.min new_min_inclusive = other.min_inclusive else new_min = min new_min_inclusive = min_inclusive || other.min_inclusive end elsif min.nil? new_min = other.min new_min_inclusive = other.min_inclusive elsif other.min.nil? new_min = min new_min_inclusive = min_inclusive end if max && other.max comparison = version_compare(max, other.max) if comparison > 0 new_max = max new_max_inclusive = max_inclusive elsif comparison < 0 new_max = other.max new_max_inclusive = other.max_inclusive else new_max = max new_max_inclusive = max_inclusive || other.max_inclusive end elsif max.nil? new_max = other.max new_max_inclusive = other.max_inclusive elsif other.max.nil? new_max = max new_max_inclusive = max_inclusive end self.class.new( min: new_min, max: new_max, min_inclusive: new_min_inclusive, max_inclusive: new_max_inclusive, scheme: merged_scheme ) end |