Class: Vers::VersionRange

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(intervals = []) ⇒ VersionRange

Returns a new instance of VersionRange.



10
11
12
13
# File 'lib/vers/version_range.rb', line 10

def initialize(intervals = [])
  @intervals = intervals.compact.reject(&:empty?).sort_by { |i| [i.min || '', i.max || ''] }
  merge_overlapping_intervals!
end

Instance Attribute Details

#intervalsObject (readonly)

Returns the value of attribute intervals.



8
9
10
# File 'lib/vers/version_range.rb', line 8

def intervals
  @intervals
end

Class Method Details

.emptyObject



15
16
17
# File 'lib/vers/version_range.rb', line 15

def self.empty
  new([])
end

.exact(version) ⇒ Object



23
24
25
# File 'lib/vers/version_range.rb', line 23

def self.exact(version)
  new([Interval.exact(version)])
end

.greater_than(version, inclusive: false) ⇒ Object



27
28
29
# File 'lib/vers/version_range.rb', line 27

def self.greater_than(version, inclusive: false)
  new([Interval.greater_than(version, inclusive: inclusive)])
end

.less_than(version, inclusive: false) ⇒ Object



31
32
33
# File 'lib/vers/version_range.rb', line 31

def self.less_than(version, inclusive: false)
  new([Interval.less_than(version, inclusive: inclusive)])
end

.unboundedObject



19
20
21
# File 'lib/vers/version_range.rb', line 19

def self.unbounded
  new([Interval.unbounded])
end

Instance Method Details

#complementObject



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
# File 'lib/vers/version_range.rb', line 64

def complement
  return self.class.unbounded if empty?
  return self.class.empty if unbounded?

  result_intervals = []
  
  sorted_intervals = intervals.sort_by { |i| i.min || '' }
  
  first_interval = sorted_intervals.first
  if first_interval.min
    result_intervals << Interval.new(
      max: first_interval.min,
      max_inclusive: !first_interval.min_inclusive
    )
  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
        )
      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
    )
  end
  
  self.class.new(result_intervals)
end

#contains?(version) ⇒ Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/vers/version_range.rb', line 43

def contains?(version)
  intervals.any? { |interval| interval.contains?(version) }
end

#empty?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/vers/version_range.rb', line 35

def empty?
  intervals.empty?
end

#exclude(version) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/vers/version_range.rb', line 105

def exclude(version)
  return self if !contains?(version)
  
  result_intervals = []
  
  intervals.each do |interval|
    if interval.contains?(version)
      if interval.min && version_compare(interval.min, version) < 0
        result_intervals << Interval.new(
          min: interval.min,
          max: version,
          min_inclusive: interval.min_inclusive,
          max_inclusive: false
        )
      end
      
      if interval.max && version_compare(version, interval.max) < 0
        result_intervals << Interval.new(
          min: version,
          max: interval.max,
          min_inclusive: false,
          max_inclusive: interval.max_inclusive
        )
      end
    else
      result_intervals << interval
    end
  end
  
  self.class.new(result_intervals)
end

#intersect(other) ⇒ Object



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

def intersect(other)
  result_intervals = []
  
  intervals.each do |interval1|
    other.intervals.each do |interval2|
      intersection = interval1.intersect(interval2)
      result_intervals << intersection unless intersection.empty?
    end
  end
  
  self.class.new(result_intervals)
end

#to_sObject



137
138
139
140
# File 'lib/vers/version_range.rb', line 137

def to_s
  return "" if empty?
  return intervals.map(&:to_s).join("")
end

#unbounded?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/vers/version_range.rb', line 39

def unbounded?
  intervals.length == 1 && intervals.first.unbounded?
end

#union(other) ⇒ Object



60
61
62
# File 'lib/vers/version_range.rb', line 60

def union(other)
  self.class.new(intervals + other.intervals)
end