Class: Vers::Interval

Inherits:
Object
  • Object
show all
Defined in:
lib/vers/interval.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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.canonical(scheme)
  @empty = compute_empty
end

Instance Attribute Details

#maxObject (readonly)

Returns the value of attribute max.



7
8
9
# File 'lib/vers/interval.rb', line 7

def max
  @max
end

#max_inclusiveObject (readonly)

Returns the value of attribute max_inclusive.



7
8
9
# File 'lib/vers/interval.rb', line 7

def max_inclusive
  @max_inclusive
end

#minObject (readonly)

Returns the value of attribute min.



7
8
9
# File 'lib/vers/interval.rb', line 7

def min
  @min
end

#min_inclusiveObject (readonly)

Returns the value of attribute min_inclusive.



7
8
9
# File 'lib/vers/interval.rb', line 7

def min_inclusive
  @min_inclusive
end

#schemeObject (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

Returns:

  • (Boolean)


193
194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'lib/vers/interval.rb', line 193

def adjacent?(other)
  merged_scheme = compatible_scheme(other)
  return false if empty? || other.empty?
  
  if max && other.min && version_compare(max, other.min, merged_scheme) == 0
    return (max_inclusive && !other.min_inclusive) || (!max_inclusive && other.min_inclusive)
  end
  
  if min && other.max && version_compare(min, other.max, merged_scheme) == 0
    return (min_inclusive && !other.max_inclusive) || (!min_inclusive && other.max_inclusive)
  end
  
  false
end

#compatible_scheme(other) ⇒ Object



224
225
226
227
228
229
230
# File 'lib/vers/interval.rb', line 224

def compatible_scheme(other)
  if scheme && other.scheme && scheme != other.scheme
    raise ArgumentError, "Cannot combine #{scheme} and #{other.scheme} version ranges"
  end

  scheme || other.scheme
end

#contains?(version, comparison_scheme: scheme) ⇒ Boolean

Returns:

  • (Boolean)


46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/vers/interval.rb', line 46

def contains?(version, comparison_scheme: scheme)
  return false if empty?
  return true if unbounded?

  within_min = min.nil? || 
               (min_inclusive ? version_compare(version, min, comparison_scheme) >= 0 : version_compare(version, min, comparison_scheme) > 0)
  
  within_max = max.nil? || 
               (max_inclusive ? version_compare(version, max, comparison_scheme) <= 0 : version_compare(version, max, comparison_scheme) < 0)

  within_min && within_max
end

#empty?Boolean

Returns:

  • (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 = compatible_scheme(other)
  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, merged_scheme)
    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, merged_scheme)
    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

Returns:

  • (Boolean)


172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/vers/interval.rb', line 172

def overlaps?(other)
  merged_scheme = compatible_scheme(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, merged_scheme)
    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, merged_scheme)
    return false if cmp > 0
    return false if cmp == 0 && (!min_inclusive || !other.max_inclusive)
  end

  true
end

#to_sObject



232
233
234
235
236
237
238
239
240
241
242
# File 'lib/vers/interval.rb', line 232

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

Returns:

  • (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
# File 'lib/vers/interval.rb', line 117

def union(other)
  merged_scheme = compatible_scheme(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, merged_scheme)
    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? || other.min.nil?
    new_min = nil
    new_min_inclusive = true
  end

  if max && other.max
    comparison = version_compare(max, other.max, merged_scheme)
    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? || other.max.nil?
    new_max = nil
    new_max_inclusive = true
  end

  self.class.new(
    min: new_min,
    max: new_max,
    min_inclusive: new_min_inclusive,
    max_inclusive: new_max_inclusive,
    scheme: merged_scheme
  )
end

#with_scheme(value) ⇒ Object



208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/vers/interval.rb', line 208

def with_scheme(value)
  canonical = Scheme.canonical(value)
  if scheme && canonical && scheme != canonical
    raise ArgumentError, "Cannot combine #{scheme} and #{canonical} version ranges"
  end
  return self if scheme == canonical

  self.class.new(
    min: min,
    max: max,
    min_inclusive: min_inclusive,
    max_inclusive: max_inclusive,
    scheme: canonical
  )
end