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.
Class Method Summary collapse
- .empty ⇒ Object
- .exact(version) ⇒ Object
- .greater_than(version, inclusive: false) ⇒ Object
- .less_than(version, inclusive: false) ⇒ Object
- .unbounded ⇒ Object
Instance Method Summary collapse
- #adjacent?(other) ⇒ Boolean
- #contains?(version) ⇒ Boolean
- #empty? ⇒ Boolean
-
#initialize(min: nil, max: nil, min_inclusive: true, max_inclusive: true) ⇒ 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) ⇒ 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) @min = min @max = max @min_inclusive = min_inclusive @max_inclusive = max_inclusive validate_bounds! 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 |
Class Method Details
.empty ⇒ Object
18 19 20 |
# File 'lib/vers/interval.rb', line 18 def self.empty new(min: "1", max: "0", min_inclusive: true, max_inclusive: true) end |
.exact(version) ⇒ Object
26 27 28 |
# File 'lib/vers/interval.rb', line 26 def self.exact(version) new(min: version, max: version, min_inclusive: true, max_inclusive: true) end |
.greater_than(version, inclusive: false) ⇒ Object
30 31 32 |
# File 'lib/vers/interval.rb', line 30 def self.greater_than(version, inclusive: false) new(min: version, min_inclusive: inclusive) end |
.less_than(version, inclusive: false) ⇒ Object
34 35 36 |
# File 'lib/vers/interval.rb', line 34 def self.less_than(version, inclusive: false) new(max: version, max_inclusive: inclusive) end |
.unbounded ⇒ Object
22 23 24 |
# File 'lib/vers/interval.rb', line 22 def self.unbounded new end |
Instance Method Details
#adjacent?(other) ⇒ Boolean
181 182 183 184 185 186 187 188 189 190 191 192 193 |
# File 'lib/vers/interval.rb', line 181 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
48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/vers/interval.rb', line 48 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 41 42 |
# File 'lib/vers/interval.rb', line 38 def empty? return true if min && max && version_compare(min, max) > 0 return true if min && max && version_compare(min, max) == 0 && (!min_inclusive || !max_inclusive) false end |
#intersect(other) ⇒ Object
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 61 def intersect(other) return self.class.empty 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 ) end |
#overlaps?(other) ⇒ Boolean
176 177 178 179 |
# File 'lib/vers/interval.rb', line 176 def overlaps?(other) return false if empty? || other.empty? !intersect(other).empty? end |
#to_s ⇒ Object
195 196 197 198 199 200 201 202 203 204 205 |
# File 'lib/vers/interval.rb', line 195 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
44 45 46 |
# File 'lib/vers/interval.rb', line 44 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 |
# 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) 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 ) end |