class MetadataJsonLint::VersionRequirement

Parses a string module version requirement with semantic_puppet and provides methods to analyse it for lint warnings

Attributes

range[R]
requirement[R]

Public Class Methods

new(requirement) click to toggle source
# File lib/metadata-json-lint/version_requirement.rb, line 5
def initialize(requirement)
  @requirement = requirement

  if defined?(SemanticPuppet::VersionRange)
    @range = SemanticPuppet::VersionRange.parse(requirement)
    if @range == SemanticPuppet::VersionRange::EMPTY_RANGE
      raise ArgumentError,
            "Range matches no versions: \"#{requirement}\""
    end
  elsif requirement.match(/\A[a-z0-9*.\-^~><=|\t ]*\Z/i).nil?
    raise ArgumentError, "Unparsable version range: \"#{requirement}\""
  end
end

Public Instance Methods

max() click to toggle source
# File lib/metadata-json-lint/version_requirement.rb, line 65
def max
  range.end
end
min() click to toggle source
# File lib/metadata-json-lint/version_requirement.rb, line 61
def min
  range.begin
end
mixed_syntax?() click to toggle source

Whether the range uses a comparison operator (e.g. >=) with a wildcard syntax, such as “>= 1.x” or “< 2.0.x”

# File lib/metadata-json-lint/version_requirement.rb, line 21
def mixed_syntax?
  !/
    [><=^~]{1,2} # comparison operators
    \s*
    \d\. # MAJOR
    (?:
      (?:x|\*) # MINOR is wildcard
      |
      \d\.(?:x|\*)  # MINOR is digit and PATCH is wildcard
    )
  /x.match(requirement).nil?
end
open_ended?() click to toggle source
# File lib/metadata-json-lint/version_requirement.rb, line 34
def open_ended?
  if range
    range.end == SemanticPuppet::Version::MAX
  else
    # Empty requirement strings are open-ended.
    return true if requirement.strip.empty?

    # Strip superfluous whitespace.
    range_set = requirement.gsub(/([><=~^])(?:\s+|\s*v)/, '\1')

    # Split on logical OR
    ranges = range_set.split(/\s*\|\|\s*/)

    # Returns true if any range includes a '>' but not a corresponding '<'
    # which should be the only way to declare an open-ended range.
    ranges.select { |r| r.include?('>') }.any? { |r| !r.include?('<') }
  end
end
puppet_eol?() click to toggle source
# File lib/metadata-json-lint/version_requirement.rb, line 53
def puppet_eol?
  true if range.begin < SemanticPuppet::Version.parse(MIN_PUPPET_VER)
end
ver_range() click to toggle source
# File lib/metadata-json-lint/version_requirement.rb, line 57
def ver_range
  range
end