class Composer::Semver::Semver

Constants

SORT_ASC
SORT_DESC

Public Class Methods

rsort(versions) click to toggle source

Sort given array of versions in reverse.

@param versions array

@return array

# File lib/composer/semver/semver.rb, line 63
def rsort(versions)
  self::usort(versions, self::SORT_DESC)
end
satisfied_by(versions, constraints) click to toggle source

Return all versions that satisfy given constraints.

@param versions array @param constraints string

@return array

# File lib/composer/semver/semver.rb, line 38
def satisfied_by(versions, constraints)
  satisfied = []
  # versions.select {|version| self::satisfies?(version, constraints) }
  versions.map do |version|
    if self::satisfies?(version, constraints)
      satisfied << version
    end
  end
  satisfied
end
satisfies?(version, constraints) click to toggle source

Determine if given version satisfies given constraints.

@param version string @param constraints string

@return bool

# File lib/composer/semver/semver.rb, line 26
def satisfies?(version, constraints)
  provider = ::Composer::Semver::Constraint::Constraint.new('==', self.version_parser.normalize(version))
  constraints = self::version_parser.parse_constraints(constraints)
  constraints.matches?(provider)
end
sort(versions) click to toggle source

Sort given array of versions.

@param versions array

@return array

# File lib/composer/semver/semver.rb, line 54
def sort(versions)
  self::usort(versions, self::SORT_ASC)
end

Protected Class Methods

apply_sorting(left, right, direction) click to toggle source
# File lib/composer/semver/semver.rb, line 101
def apply_sorting(left, right, direction)

  if left[:normalized] === right[:normalized]
    return 0
  end

  if ::Composer::Semver::Comparator::less_than?(left[:normalized], right[:normalized])
    return -direction
  end

  direction
end
usort(versions, direction) click to toggle source

@param versions array @param direction integer

@return array

# File lib/composer/semver/semver.rb, line 78
def usort(versions, direction)
  # if @@version_parser.nil?
  #     @@version_parser = ::Composer::Semver::VersionParser.new()
  # end

  # version_parser = @@version_parser
  normalized = []

  # Normalize outside of usort() scope for minor performance increase.
  # Creates an array of arrays: [[normalized, key], ...]
  versions.each_index { |i| normalized.push({ normalized: self::version_parser.normalize(versions[i]), index: i }) }

  normalized_sorted = normalized.sort {|a,b| apply_sorting(a, b, direction)}

  # Recreate input array, using the original indexes which are now in sorted order.
  sorted = []
  normalized_sorted.each do |item|
    sorted.push versions[item[:index]]
  end

  sorted
end
version_parser() click to toggle source

@var VersionParser */

# File lib/composer/semver/semver.rb, line 70
def version_parser
  @version_parser ||= ::Composer::Semver::VersionParser.new
end