class Dependabot::Hex::Requirement

Constants

AND_SEPARATOR
OPS

Add the double-equality matcher to the list of allowed operations

OR_SEPARATOR
PATTERN
PATTERN_RAW

Public Class Methods

new(*requirements) click to toggle source

Patches Gem::Requirement to make it accept requirement strings like “~> 4.2.5, >= 4.2.5.1” without first needing to split them.

Calls superclass method
# File lib/dependabot/hex/requirement.rb, line 31
def initialize(*requirements)
  requirements = requirements.flatten.flat_map do |req_string|
    req_string.split(",").map(&:strip)
  end

  super(requirements)
end
parse(obj) click to toggle source

Override the parser to create Hex::Versions

# File lib/dependabot/hex/requirement.rb, line 40
def self.parse(obj)
  return ["=", Hex::Version.new(obj.to_s)] if obj.is_a?(Gem::Version)

  unless (matches = PATTERN.match(obj.to_s))
    msg = "Illformed requirement [#{obj.inspect}]"
    raise BadRequirementError, msg
  end

  return DefaultRequirement if matches[1] == ">=" && matches[2] == "0"

  [matches[1] || "=", Hex::Version.new(matches[2])]
end
requirements_array(requirement_string) click to toggle source

Returns an array of requirements. At least one requirement from the returned array must be satisfied for a version to be valid.

# File lib/dependabot/hex/requirement.rb, line 22
def self.requirements_array(requirement_string)
  requirement_string.strip.split(OR_SEPARATOR).map do |req_string|
    requirements = req_string.strip.split(AND_SEPARATOR)
    new(requirements)
  end
end

Public Instance Methods

satisfied_by?(version) click to toggle source
# File lib/dependabot/hex/requirement.rb, line 53
def satisfied_by?(version)
  version = Hex::Version.new(version.to_s)

  requirements.all? { |op, rv| (OPS[op] || OPS["="]).call(version, rv) }
end