Class: VersionInterval

Inherits:
Object
  • Object
show all
Defined in:
lib/semver_dialects/semantic_version/version_interval.rb

Direct Known Subclasses

EmptyInterval

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type, start_cut, end_cut) ⇒ VersionInterval

Returns a new instance of VersionInterval.



12
13
14
15
16
# File 'lib/semver_dialects/semantic_version/version_interval.rb', line 12

def initialize(type, start_cut, end_cut)
  @type = type
  @start_cut = start_cut
  @end_cut = end_cut
end

Instance Attribute Details

#end_cutObject

Returns the value of attribute end_cut.



10
11
12
# File 'lib/semver_dialects/semantic_version/version_interval.rb', line 10

def end_cut
  @end_cut
end

#start_cutObject

Returns the value of attribute start_cut.



10
11
12
# File 'lib/semver_dialects/semantic_version/version_interval.rb', line 10

def start_cut
  @start_cut
end

#typeObject

Returns the value of attribute type.



10
11
12
# File 'lib/semver_dialects/semantic_version/version_interval.rb', line 10

def type
  @type
end

Instance Method Details

#==(other_interval) ⇒ Object



180
181
182
# File 'lib/semver_dialects/semantic_version/version_interval.rb', line 180

def ==(other_interval)
  @start_cut == other_interval.start_cut && @end_cut == other_interval.end_cut && @type == other_interval.type
end

#bit_set?(interval_type) ⇒ Boolean

Returns:

  • (Boolean)


199
200
201
# File 'lib/semver_dialects/semantic_version/version_interval.rb', line 199

def bit_set?(interval_type)
  @type & interval_type != 0
end

#collapse(other_interval) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/semver_dialects/semantic_version/version_interval.rb', line 45

def collapse(other_interval)
  return EmptyInterval.new if self.intersect(other_interval).instance_of?(EmptyInterval)

  frame = [@start_cut, other_interval.start_cut, @end_cut, other_interval.end_cut].reject { |cut| special(cut) }
  min_cut = frame.reduce { |smallest, current| smallest < current ? smallest : current }
  max_cut = frame.reduce { |biggest, current| biggest > current ? biggest : current }

  # compute the boundaries for the union
  type = compute_union_boundary(self, other_interval, min_cut, max_cut)
  VersionInterval.new(type, min_cut, max_cut)
end

#cross_totalObject



172
173
174
175
176
177
178
# File 'lib/semver_dialects/semantic_version/version_interval.rb', line 172

def cross_total
  if distinct?
    @start_cut.cross_total
  else
    @start_cut.cross_total + @end_cut.cross_total
  end
end

#diff(other_interval, abs = true) ⇒ Object



150
151
152
153
154
155
156
# File 'lib/semver_dialects/semantic_version/version_interval.rb', line 150

def diff(other_interval, abs = true)
  if self.distinct? && other_interval.distinct?
    self.start_cut.diff(other_interval.start_cut, abs)
  else
    EmptyInterval.new()
  end
end

#distinct?Boolean

Returns:

  • (Boolean)


102
103
104
# File 'lib/semver_dialects/semantic_version/version_interval.rb', line 102

def distinct?
  bit_set?(IntervalType::LEFT_CLOSED) && bit_set?(IntervalType::RIGHT_CLOSED) && @start_cut == @end_cut
end

#empty?Boolean

Returns:

  • (Boolean)


142
143
144
# File 'lib/semver_dialects/semantic_version/version_interval.rb', line 142

def empty?
  self.instance_of?(EmptyInterval)
end

#intersect(other_interval) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/semver_dialects/semantic_version/version_interval.rb', line 18

def intersect(other_interval)
  # this look odd -- we have to use it here though, because it may be that placeholders are present inside
  # the version for which > and < would yield true
  return EmptyInterval.new if !(@start_cut <= other_interval.end_cut) || !(other_interval.start_cut <= @end_cut)

  start_cut_new = max(@start_cut, other_interval.start_cut)
  end_cut_new = min(@end_cut, other_interval.end_cut)

  # compute the boundaries for the intersection
  type = compute_intersection_boundary(self, other_interval, start_cut_new, end_cut_new)
  interval = VersionInterval.new(type, start_cut_new, end_cut_new)
  half_open = !(interval.bit_set?(IntervalType::RIGHT_CLOSED) && interval.bit_set?(IntervalType::LEFT_CLOSED))

  interval.singleton? && half_open ? EmptyInterval.new : interval
end

#invertObject

inverts the given version interval – note that this function amy return two version intervals e.g., (2,10] -> (-inf, 2], (10, +inf)

left       right


187
188
189
190
191
192
193
194
195
196
197
# File 'lib/semver_dialects/semantic_version/version_interval.rb', line 187

def invert
  intervals = []
  left_type = bit_set?(IntervalType::LEFT_OPEN) ? IntervalType::RIGHT_CLOSED : IntervalType::RIGHT_OPEN
  left_type = left_type | IntervalType::LEFT_OPEN
  right_type = bit_set?(IntervalType::RIGHT_OPEN) ? IntervalType::LEFT_CLOSED : IntervalType::LEFT_OPEN
  right_type = right_type | IntervalType::RIGHT_OPEN

  intervals << VersionInterval.new(left_type, BelowAll.new, @start_cut) unless @start_cut.instance_of?(BelowAll)
  intervals << VersionInterval.new(right_type, @end_cut, AboveAll.new) unless @end_cut.instance_of?(AboveAll)
  intervals
end

#join(other_interval) ⇒ Object



162
163
164
165
166
167
168
169
170
# File 'lib/semver_dialects/semantic_version/version_interval.rb', line 162

def join(other_interval)
  if self.joinable?(other_interval)
    _join(self, other_interval)
  elsif other_interval.joinable?(self)
    _join(other_interval, self)
  else
    EmptyInterval.new()
  end
end

#joinable?(other_interval) ⇒ Boolean

Returns:

  • (Boolean)


158
159
160
# File 'lib/semver_dialects/semantic_version/version_interval.rb', line 158

def joinable?(other_interval)
  other_interval.start_cut.is_successor_of?(self.end_cut) || universal?
end

#singleton?Boolean

Returns:

  • (Boolean)


146
147
148
# File 'lib/semver_dialects/semantic_version/version_interval.rb', line 146

def singleton?
  @start_cut == @end_cut && @start_cut.value == @end_cut.value
end

#special(cut) ⇒ Object



57
58
59
# File 'lib/semver_dialects/semantic_version/version_interval.rb', line 57

def special(cut)
  cut.instance_of?(AboveAll) || cut.instance_of?(BelowAll)
end

#subsumes?(other) ⇒ Boolean

Returns:

  • (Boolean)


106
107
108
# File 'lib/semver_dialects/semantic_version/version_interval.rb', line 106

def subsumes?(other)
  @start_cut <= other.start_cut && @end_cut >= other.end_cut
end

#to_conan_sObject



126
127
128
# File 'lib/semver_dialects/semantic_version/version_interval.rb', line 126

def to_conan_s
  get_canoncial_s
end

#to_description_sObject

this function returns a human-readable descriptions of the version strings



72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/semver_dialects/semantic_version/version_interval.rb', line 72

def to_description_s
  s = ""
  if self.distinct?
    s = "version #{@start_cut.to_s}"
  elsif self.universal?
    s = "all versions "
  else
    s = "all versions "
    s += start_cut.instance_of?(BelowAll) ? "" : bit_set?(IntervalType::LEFT_OPEN) ? "after #{@start_cut.to_s} " : bit_set?(IntervalType::LEFT_CLOSED) ? "starting from #{@start_cut.to_s} " : ""
    s += end_cut.instance_of?(AboveAll) ? "" : bit_set?(IntervalType::RIGHT_OPEN) ? "before #{@end_cut.to_s}" : bit_set?(IntervalType::RIGHT_CLOSED) ? "up to #{@end_cut.to_s}" : ""
  end
  s.strip
end

#to_gem_sObject



114
115
116
# File 'lib/semver_dialects/semantic_version/version_interval.rb', line 114

def to_gem_s
  get_canoncial_s
end

#to_go_sObject



130
131
132
# File 'lib/semver_dialects/semantic_version/version_interval.rb', line 130

def to_go_s
  get_canoncial_s
end

#to_maven_sObject



90
91
92
93
94
95
96
97
98
99
100
# File 'lib/semver_dialects/semantic_version/version_interval.rb', line 90

def to_maven_s
  s = ""
  # special case -- distinct version
  if self.distinct?
    s += "[#{@start_cut.to_s}]"
  else
    s += start_cut.instance_of?(BelowAll) ? "(," : bit_set?(IntervalType::LEFT_OPEN) ? "[#{@start_cut.to_s}," : bit_set?(IntervalType::LEFT_CLOSED) ? "[#{@start_cut.to_s}," : ""
    s += end_cut.instance_of?(AboveAll) ? ")" : bit_set?(IntervalType::RIGHT_OPEN) ? "#{@end_cut.to_s})" : bit_set?(IntervalType::RIGHT_CLOSED) ? "#{@end_cut.to_s}]" : ""
  end
  s
end

#to_npm_sObject



122
123
124
# File 'lib/semver_dialects/semantic_version/version_interval.rb', line 122

def to_npm_s
  get_canoncial_s
end

#to_nuget_sObject



86
87
88
# File 'lib/semver_dialects/semantic_version/version_interval.rb', line 86

def to_nuget_s
  to_maven_s
end

#to_packagist_sObject



138
139
140
# File 'lib/semver_dialects/semantic_version/version_interval.rb', line 138

def to_packagist_s
  get_canoncial_s(',')
end

#to_pypi_sObject



134
135
136
# File 'lib/semver_dialects/semantic_version/version_interval.rb', line 134

def to_pypi_s
  get_canoncial_s(',', '==')
end

#to_ruby_sObject



118
119
120
# File 'lib/semver_dialects/semantic_version/version_interval.rb', line 118

def to_ruby_s
  get_canoncial_s
end

#to_sObject



61
62
63
64
65
66
67
68
69
# File 'lib/semver_dialects/semantic_version/version_interval.rb', line 61

def to_s
  s = ""
  s += bit_set?(IntervalType::LEFT_CLOSED) ? "[" : ""
  s += bit_set?(IntervalType::LEFT_OPEN) ? "(" : ""
  s += [@start_cut, @end_cut].join(",")
  s += bit_set?(IntervalType::RIGHT_CLOSED) ? "]" : ""
  s += bit_set?(IntervalType::RIGHT_OPEN) ? ")" : ""
  s
end

#union(other_interval) ⇒ Object



34
35
36
37
38
39
40
41
42
43
# File 'lib/semver_dialects/semantic_version/version_interval.rb', line 34

def union(other_interval)
  return EmptyInterval.new if self.intersect(other_interval).instance_of?(EmptyInterval)

  start_cut_new = min(@start_cut, other_interval.start_cut)
  end_cut_new = max(@end_cut, other_interval.end_cut)

  # compute the boundaries for the union
  type = compute_union_boundary(self, other_interval, start_cut_new, end_cut_new)
  VersionInterval.new(type, start_cut_new, end_cut_new)
end

#universal?Boolean

Returns:

  • (Boolean)


110
111
112
# File 'lib/semver_dialects/semantic_version/version_interval.rb', line 110

def universal?
  (bit_set?(IntervalType::LEFT_OPEN) && bit_set?(IntervalType::RIGHT_OPEN) && @start_cut.instance_of?(BelowAll) && @end_cut.instance_of?(AboveAll)) || @start_cut.is_initial_version? && @end_cut.instance_of?(AboveAll)
end