Class: Udb::RequirementSpec

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/udb/version_spec.rb

Overview

Represents a version requirement

A requirement is either a logical comparison (>, >=, <, <=, =, !=) or a compatible operator (~>).

Examples:

Logical requirement

# When the requirement is a logical comparison, the extension parameter is not needed
RequirementSpec.new(">= 0.5").satisfied_by?(VersionSpec.new("1.0"), nil) #=> true
RequirementSpec.new(">= 0.5").satisfied_by?(VersionSpec.new("0.4"), nil) #=> false

Compatible requirement

s_ext = Extension.new(...) # S extension, which is breaking between 1.11 -> 1.12
RequirementSpec.new("~> 1.11").satisfied_by?(VersionSpec.new("1.10"), s_ext) #=> true
RequirementSpec.new("~> 1.11").satisfied_by?(VersionSpec.new("1.11"), s_ext) #=> true
RequirementSpec.new("~> 1.11").satisfied_by?(VersionSpec.new("1.12"), s_ext) #=> false

Constant Summary collapse

REQUIREMENT_OP_REGEX =
/((?:>=)|(?:>)|(?:~>)|(?:<)|(?:<=)|(?:!=)|(?:=))/
REQUIREMENT_REGEX =
/#{REQUIREMENT_OP_REGEX}\s*(#{VersionSpec::VERSION_REGEX})/

Instance Method Summary collapse

Constructor Details

#initialize(requirement)

Parameters:

  • requirement (String)

    A requirement string



159
160
161
162
163
164
165
166
167
168
# File 'lib/udb/version_spec.rb', line 159

def initialize(requirement)
  if requirement =~ /^\s*#{REQUIREMENT_REGEX}\s*$/
    m = T.must(::Regexp.last_match)
    @op = T.must(m[1])
    @version_str = T.must(m[2])
    @version_spec = VersionSpec.new(@version_str)
  else
    raise ArgumentError, "Bad requirement string '#{requirement}' #{REQUIREMENT_REGEX}"
  end
end

Instance Method Details

#invert!

This method returns an undefined value.

invert the requirement



177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/udb/version_spec.rb', line 177

def invert!
  case @op
  when ">="
    @op = "<"
  when ">"
    @op = "<="
  when "<="
    @op = ">"
  when "<"
    @op = ">="
  when "="
    @op = "!="
  when "!="
    @op = "="
  when "~>"
    @op = "!~>"
  end
  self
end

#satisfied_by?(version, ext) ⇒ Boolean

Returns if the version satisfies the requirement.

Parameters:

  • version (VersionSpec)

    A version spec

  • ext (Hash)

    Raw extension spec (from YAML)

  • version (String, VersionSpec)

    A version string

  • ext (Extension, Hash{String => T.untyped})

    An extension, needed to evaluate the compatible (~>) operator

Returns:

  • (Boolean)

    if the version satisfies the requirement



203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
# File 'lib/udb/version_spec.rb', line 203

def satisfied_by?(version, ext)
  v_spec =
    case version
    when String
      VersionSpec.new(version)
    when VersionSpec
      version
    else
      T.absurd(version)
    end

  case @op
  when ">="
    v_spec >= @version_spec
  when ">"
    v_spec > @version_spec
  when "<="
    v_spec <= @version_spec
  when "<"
    v_spec < @version_spec
  when "="
    v_spec == @version_spec
  when "!="
    v_spec != @version_spec
  when "~>"
    if ext.is_a?(Extension)
      matching_ver = ext.versions.find { |v| v.version_spec == v_spec }
      raise "Can't find version?" if matching_ver.nil?

      matching_ver.compatible?(ExtensionVersion.new(ext.name, v_spec.to_s, ext.arch))
    else
      versions = ext.fetch("versions")
      compatible_versions = []
      versions.each do |vinfo|
        vspec = VersionSpec.new(vinfo.fetch("version"))
        compatible_versions << vspec if vspec >= v_spec
        break if compatible_versions.size.positive? && vinfo.key?("breaking")
      end

      compatible_versions.include?(v_spec)
    end
  when "!~>" # not a legal spec, but used for inversion
    if ext.is_a?(Extension)
      matching_ver = ext.versions.find { |v| v.version_spec == v_spec }
      raise "Can't find version?" if matching_ver.nil?

      !matching_ver.compatible?(ExtensionVersion.new(ext.name, v_spec.to_s, ext.arch))
    else
      versions = ext.fetch("versions")
      compatible_versions = []
      versions.each do |vinfo|
        vspec = VersionSpec.new(vinfo.fetch("version"))
        compatible_versions << vspec if vspec >= v_spec
        break if compatible_versions.size.positive? && vinfo.key?("breaking")
      end

      !compatible_versions.include?(v_spec)
    end
  end
end

#to_sString

Returns:

  • (String)


171
172
173
# File 'lib/udb/version_spec.rb', line 171

def to_s
  "#{@op} #{@version_str}"
end