Class: Dependabot::Elm::Requirement

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

Constant Summary collapse

PATTERN_RAW =

Override the version pattern to allow local versions

"(#{Elm::Version::VERSION_PATTERN}) (<=?) v (<=?) " \
"(#{Elm::Version::VERSION_PATTERN})"
PATTERN =
/\A#{PATTERN_RAW}\z/.freeze
EXACT_PATTERN =
/\A#{Elm::Version::VERSION_PATTERN}\z/.freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*requirements) ⇒ Requirement

Overwrite superclass method to use ‘flat_map`



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/dependabot/elm/requirement.rb', line 48

def initialize(*requirements)
  if requirements.any?(&:nil?)
    raise BadRequirementError, "Nil requirement not supported in Elm"
  end

  requirements = requirements.flatten
  requirements.compact!
  requirements.uniq!

  if requirements.empty?
    @requirements = [DefaultRequirement]
  else
    @requirements = requirements.flat_map { |r| self.class.parse(r) }
    sort_requirements!
  end
end

Class Method Details

.parse(obj) ⇒ Object

Override the parser to create Elm::Versions and return an array of parsed requirements



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/dependabot/elm/requirement.rb', line 24

def self.parse(obj)
  # If a version is given this is an equals requirement
  if EXACT_PATTERN.match?(obj.to_s)
    return [["=", Elm::Version.new(obj.to_s)]]
  end

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

  # If the two versions specified are identical this is an equals
  # requirement
  if matches[1] == matches[4] && matches[3] == "<="
    return [["=", Elm::Version.new(matches[4])]]
  end

  [
    [matches[2].tr("<", ">"), Elm::Version.new(matches[1])],
    [matches[3], Elm::Version.new(matches[4])]
  ]
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.



18
19
20
# File 'lib/dependabot/elm/requirement.rb', line 18

def self.requirements_array(requirement_string)
  [new(requirement_string)]
end

Instance Method Details

#concat(new) ⇒ Object

Overwrite superclass method to use ‘flat_map`



66
67
68
69
70
71
72
73
74
# File 'lib/dependabot/elm/requirement.rb', line 66

def concat(new)
  new = new.flatten
  new.compact!
  new.uniq!
  new = new.flat_map { |r| self.class.parse(r) }

  @requirements.concat new
  sort_requirements!
end

#satisfied_by?(version) ⇒ Boolean

Returns:

  • (Boolean)


85
86
87
88
# File 'lib/dependabot/elm/requirement.rb', line 85

def satisfied_by?(version)
  version = Elm::Version.new(version.to_s)
  super
end

#sort_requirements!Object



76
77
78
79
80
81
82
83
# File 'lib/dependabot/elm/requirement.rb', line 76

def sort_requirements!
  @requirements.sort! do |l, r|
    comp = l.last <=> r.last # first, sort by the requirement's version
    next comp unless comp.zero?

    l.first <=> r.first # then, sort by the operator (for stability)
  end
end