Class: Dependabot::Hex::Requirement

Inherits:
Requirement
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/dependabot/hex/requirement.rb

Constant Summary collapse

AND_SEPARATOR =
/\s+and\s+/
OR_SEPARATOR =
/\s+or\s+/
OPS =

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

T.let(
  OPS.merge(
    "==" => lambda { |v, r|
              v == r
            }
  ),
  T::Hash[String, T.proc.params(arg0: Gem::Version, arg1: Gem::Version).returns(T::Boolean)]
)
PATTERN_RAW =
T.let("\\s*(#{quoted})?\\s*(#{Hex::Version::VERSION_PATTERN})\\s*".freeze, String)
PATTERN =
/\A#{PATTERN_RAW}\z/

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*requirements) ⇒ Requirement

Returns a new instance of Requirement.



46
47
48
49
50
51
52
# File 'lib/dependabot/hex/requirement.rb', line 46

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

  super(requirements)
end

Class Method Details

.parse(obj) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/dependabot/hex/requirement.rb', line 59

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(T.must(matches[2]))]
end

.requirements_array(requirement_string) ⇒ Object



36
37
38
39
40
41
# File 'lib/dependabot/hex/requirement.rb', line 36

def self.requirements_array(requirement_string)
  T.must(requirement_string).strip.split(OR_SEPARATOR).map do |req_string|
    requirements = req_string.strip.split(AND_SEPARATOR)
    new(requirements)
  end
end

Instance Method Details

#satisfied_by?(version) ⇒ Boolean

Returns:

  • (Boolean)


73
74
75
76
77
# File 'lib/dependabot/hex/requirement.rb', line 73

def satisfied_by?(version)
  version = Hex::Version.new(version.to_s)

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