Class: SemverDialects::Interval

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

Overview

Interval is an interval that starts with a lower boundary and ends with an upper boundary. The interval includes the boundaries or not depending on its type.

Direct Known Subclasses

EmptyInterval

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type, start_cut, end_cut) ⇒ Interval

Returns a new instance of Interval.



24
25
26
27
28
# File 'lib/semver_dialects/interval.rb', line 24

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.



22
23
24
# File 'lib/semver_dialects/interval.rb', line 22

def end_cut
  @end_cut
end

#start_cutObject

Returns the value of attribute start_cut.



22
23
24
# File 'lib/semver_dialects/interval.rb', line 22

def start_cut
  @start_cut
end

#typeObject

Returns the value of attribute type.



22
23
24
# File 'lib/semver_dialects/interval.rb', line 22

def type
  @type
end

Class Method Details

.from_version(version) ⇒ Object

Returns an interval that only includes the given version.



17
18
19
20
# File 'lib/semver_dialects/interval.rb', line 17

def self.from_version(version)
  boundary = Boundary.new(version)
  Interval.new(IntervalType::LEFT_CLOSED | IntervalType::RIGHT_CLOSED, boundary, boundary)
end

Instance Method Details

#==(other) ⇒ Object



179
180
181
# File 'lib/semver_dialects/interval.rb', line 179

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

#bit_set?(interval_type) ⇒ Boolean

Returns:

  • (Boolean)


183
184
185
# File 'lib/semver_dialects/interval.rb', line 183

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

#distinct?Boolean

Returns:

  • (Boolean)


117
118
119
# File 'lib/semver_dialects/interval.rb', line 117

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

#empty?Boolean

Returns:

  • (Boolean)


171
172
173
# File 'lib/semver_dialects/interval.rb', line 171

def empty?
  instance_of?(EmptyInterval)
end

#intersect(other_interval) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/semver_dialects/interval.rb', line 30

def intersect(other_interval)
  return EmptyInterval.new if empty?

  # 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 = Interval.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

#singleton?Boolean

Returns:

  • (Boolean)


175
176
177
# File 'lib/semver_dialects/interval.rb', line 175

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

#special(cut) ⇒ Object



48
49
50
# File 'lib/semver_dialects/interval.rb', line 48

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

#subsumes?(other) ⇒ Boolean

Returns:

  • (Boolean)


121
122
123
# File 'lib/semver_dialects/interval.rb', line 121

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

#to_cargo_sObject



159
160
161
# File 'lib/semver_dialects/interval.rb', line 159

def to_cargo_s
  get_canoncial_s
end

#to_conan_sObject



143
144
145
# File 'lib/semver_dialects/interval.rb', line 143

def to_conan_s
  get_canoncial_s
end

#to_description_sObject

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



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
# File 'lib/semver_dialects/interval.rb', line 63

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

#to_gem_sObject



131
132
133
# File 'lib/semver_dialects/interval.rb', line 131

def to_gem_s
  get_canoncial_s
end

#to_go_sObject



147
148
149
# File 'lib/semver_dialects/interval.rb', line 147

def to_go_s
  get_canoncial_s
end

#to_maven_sObject



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/semver_dialects/interval.rb', line 93

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

#to_npm_sObject



139
140
141
# File 'lib/semver_dialects/interval.rb', line 139

def to_npm_s
  get_canoncial_s
end

#to_nuget_sObject



89
90
91
# File 'lib/semver_dialects/interval.rb', line 89

def to_nuget_s
  to_maven_s
end

#to_packagist_sObject



155
156
157
# File 'lib/semver_dialects/interval.rb', line 155

def to_packagist_s
  get_canoncial_s(',')
end

#to_pub_sObject



167
168
169
# File 'lib/semver_dialects/interval.rb', line 167

def to_pub_s
  get_canoncial_s
end

#to_pypi_sObject



151
152
153
# File 'lib/semver_dialects/interval.rb', line 151

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

#to_ruby_sObject



135
136
137
# File 'lib/semver_dialects/interval.rb', line 135

def to_ruby_s
  get_canoncial_s
end

#to_sObject



52
53
54
55
56
57
58
59
60
# File 'lib/semver_dialects/interval.rb', line 52

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

#to_swift_sObject



163
164
165
# File 'lib/semver_dialects/interval.rb', line 163

def to_swift_s
  get_canoncial_s
end

#universal?Boolean

Returns:

  • (Boolean)


125
126
127
128
129
# File 'lib/semver_dialects/interval.rb', line 125

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