Class: Dependabot::Utils::Go::Requirement

Inherits:
Gem::Requirement
  • Object
show all
Defined in:
lib/dependabot/utils/go/requirement.rb

Constant Summary collapse

WILDCARD_REGEX =
/(?:\.|^)[xX*]/.freeze
OR_SEPARATOR =
/(?<=[a-zA-Z0-9*])\s*\|{2}/.freeze
PATTERN_RAW =
"\\s*(#{quoted})?\\s*(#{version_pattern})\\s*"
PATTERN =
/\A#{PATTERN_RAW}\z/.freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*requirements) ⇒ Requirement

Returns a new instance of Requirement.



52
53
54
55
56
57
58
59
60
# File 'lib/dependabot/utils/go/requirement.rb', line 52

def initialize(*requirements)
  requirements = requirements.flatten.flat_map do |req_string|
    req_string.split(",").map do |r|
      convert_go_constraint_to_ruby_constraint(r.strip)
    end
  end

  super(requirements)
end

Class Method Details

.parse(obj) ⇒ Object

Use Utils::Go::Version rather than Gem::Version to ensure that pre-release versions aren’t transformed.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/dependabot/utils/go/requirement.rb', line 27

def self.parse(obj)
  if obj.is_a?(Gem::Version)
    return ["=", Utils::Go::Version.new(obj.to_s)]
  end

  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] || "=", Utils::Go::Version.new(matches[2])]
end

.requirements_array(requirement_string) ⇒ Object

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



44
45
46
47
48
49
50
# File 'lib/dependabot/utils/go/requirement.rb', line 44

def self.requirements_array(requirement_string)
  return [new(nil)] if requirement_string.nil?

  requirement_string.strip.split(OR_SEPARATOR).map do |req_string|
    new(req_string)
  end
end