Sha256: 631faf2d79f84eca7e76b5a41fd8546fe90daa62cf64bb22e3abadfe89253fd6

Contents?: true

Size: 1.96 KB

Versions: 17

Compression:

Stored size: 1.96 KB

Contents

class SemVer
  VERSION = /^v?(\d+)\.(\d+)\.(\d+)([A-Za-z][0-9A-Za-z-]*|)$/
  SIMPLE_RANGE = /^v?(\d+|[xX])(?:\.(\d+|[xX])(?:\.(\d+|[xX]))?)?$/

  include Comparable

  def self.valid?(ver)
    VERSION =~ ver
  end

  def self.find_matching(pattern, versions)
    versions.select { |v| v.matched_by?("#{pattern}") }.sort.last
  end

  attr_reader :major, :minor, :tiny, :special

  def initialize(ver)
    unless SemVer.valid?(ver)
      raise ArgumentError.new("Invalid version string '#{ver}'!")
    end

    @major, @minor, @tiny, @special = VERSION.match(ver).captures.map do |x|
      # Because Kernel#Integer tries to interpret hex and octal strings, which
      # we specifically do not want, and which cannot be overridden in 1.8.7.
      Float(x).to_i rescue x
    end
  end

  def <=>(other)
    other = SemVer.new("#{other}") unless other.is_a? SemVer
    return self.major <=> other.major unless self.major == other.major
    return self.minor <=> other.minor unless self.minor == other.minor
    return self.tiny  <=> other.tiny  unless self.tiny  == other.tiny

    return 0  if self.special  == other.special
    return 1  if self.special  == ''
    return -1 if other.special == ''

    return self.special <=> other.special
  end

  def matched_by?(pattern)
    # For the time being, this is restricted to exact version matches and
    # simple range patterns.  In the future, we should implement some or all of
    # the comparison operators here:
    # https://github.com/isaacs/node-semver/blob/d474801/semver.js#L340

    case pattern
    when SIMPLE_RANGE
      pattern = SIMPLE_RANGE.match(pattern).captures
      pattern[1] = @minor unless pattern[1] && pattern[1] !~ /x/i
      pattern[2] = @tiny  unless pattern[2] && pattern[2] !~ /x/i
      [@major, @minor, @tiny] == pattern.map { |x| x.to_i }
    when VERSION
      self == SemVer.new(pattern)
    else
      false
    end
  end

  def inspect
    "v#{@major}.#{@minor}.#{@tiny}#{@special}"
  end
  alias :to_s :inspect
end

Version data entries

17 entries across 17 versions & 2 rubygems

Version Path
supply_drop-0.11.0 examples/vendored-puppet/vendor/puppet-2.7.8/lib/semver.rb
supply_drop-0.10.2 examples/vendored-puppet/vendor/puppet-2.7.8/lib/semver.rb
supply_drop-0.10.1 examples/vendored-puppet/vendor/puppet-2.7.8/lib/semver.rb
supply_drop-0.10.0 examples/vendored-puppet/vendor/puppet-2.7.8/lib/semver.rb
supply_drop-0.9.0 examples/vendored-puppet/vendor/puppet-2.7.8/lib/semver.rb
supply_drop-0.8.1 examples/vendored-puppet/vendor/puppet-2.7.8/lib/semver.rb
supply_drop-0.8.0 examples/vendored-puppet/vendor/puppet-2.7.8/lib/semver.rb
puppet-2.7.11 lib/semver.rb
supply_drop-0.7.0 examples/vendored-puppet/vendor/puppet-2.7.8/lib/semver.rb
supply_drop-0.6.1 examples/vendored-puppet/vendor/puppet-2.7.8/lib/semver.rb
supply_drop-0.6.0 examples/vendored-puppet/vendor/puppet-2.7.8/lib/semver.rb
puppet-2.7.9 lib/semver.rb
puppet-2.7.8 lib/semver.rb
puppet-2.7.6 lib/semver.rb
puppet-2.7.5 lib/semver.rb
puppet-2.7.4 lib/semver.rb
puppet-2.7.3 lib/semver.rb