Class: Dependabot::GoModules::Requirement

Inherits:
Gem::Requirement
  • Object
show all
Defined in:
lib/dependabot/go_modules/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


50
51
52
53
54
55
56
57
58
# File 'lib/dependabot/go_modules/requirement.rb', line 50

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 GoModules::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
# File 'lib/dependabot/go_modules/requirement.rb', line 27

def self.parse(obj)
  return ["=", 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] || "=", 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.


42
43
44
45
46
47
48
# File 'lib/dependabot/go_modules/requirement.rb', line 42

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