class Solve::Constraint
Constants
- COMPARE_FUNS
- OPERATOR_TYPES
- REGEXP
Attributes
Return the Semverse::Version representation of the major, minor, and patch attributes of this instance
@return [Semverse::Version]
Public Class Methods
Coerce the object into a constraint.
@param [Constraint, String]
@return [Constraint]
# File lib/solve/constraint.rb, line 9 def coerce(object) if object.nil? Semverse::DEFAULT_CONSTRAINT else object.is_a?(self) ? object : new(object) end end
@param [Semverse::Constraint] constraint @param [Semverse::Version] target_version
@return [Boolean]
# File lib/solve/constraint.rb, line 110 def compare_approx(constraint, target_version) min = constraint.version max = if constraint.patch.nil? Semverse::Version.new([min.major + 1, 0, 0, 0]) elsif constraint.build identifiers = constraint.version.identifiers(:build) replace = identifiers.last.to_i.to_s == identifiers.last.to_s ? "-" : nil Semverse::Version.new([min.major, min.minor, min.patch, min.pre_release, identifiers.fill(replace, -1).join(".")]) elsif constraint.pre_release identifiers = constraint.version.identifiers(:pre_release) replace = identifiers.last.to_i.to_s == identifiers.last.to_s ? "-" : nil Semverse::Version.new([min.major, min.minor, min.patch, identifiers.fill(replace, -1).join(".")]) else Semverse::Version.new([min.major, min.minor + 1, 0, 0]) end min <= target_version && target_version < max end
@param [Semverse::Constraint] constraint @param [Semverse::Version] target_version
@return [Boolean]
# File lib/solve/constraint.rb, line 70 def compare_equal(constraint, target_version) target_version == constraint.version end
@param [Semverse::Constraint] constraint @param [Semverse::Version] target_version
@return [Boolean]
# File lib/solve/constraint.rb, line 78 def compare_gt(constraint, target_version) target_version > constraint.version end
@param [Semverse::Constraint] constraint @param [Semverse::Version] target_version
@return [Boolean]
# File lib/solve/constraint.rb, line 94 def compare_gte(constraint, target_version) target_version >= constraint.version end
@param [Semverse::Constraint] constraint @param [Semverse::Version] target_version
@return [Boolean]
# File lib/solve/constraint.rb, line 86 def compare_lt(constraint, target_version) target_version < constraint.version end
@param [Semverse::Constraint] constraint @param [Semverse::Version] target_version
@return [Boolean]
# File lib/solve/constraint.rb, line 102 def compare_lte(constraint, target_version) target_version <= constraint.version end
@param [#to_s] constraint
# File lib/solve/constraint.rb, line 164 def initialize(constraint = nil) constraint = constraint.to_s if constraint.nil? || constraint.empty? constraint = ">= 0.0.0" end @operator, @major, @minor, @patch, @pre_release, @build = self.class.split(constraint) unless operator_type == :approx @minor ||= 0 @patch ||= 0 end @version = Semverse::Version.new([ major, minor, patch, pre_release, build, ]) end
Split a constraint string into an Array of two elements. The first element being the operator and second being the version string.
If the given string does not contain a constraint operator then (=) will be used.
If the given string does not contain a valid version string then nil will be returned.
@param [#to_s] constraint
@example splitting a string with a constraint operator and valid version string
Constraint.split(">= 1.0.0") => [ ">=", "1.0.0" ]
@example splitting a string without a constraint operator
Constraint.split("0.0.0") => [ "=", "1.0.0" ]
@example splitting a string without a valid version string
Constraint.split("hello") => nil
@return [Array, nil]
# File lib/solve/constraint.rb, line 38 def split(constraint) if constraint =~ /^[0-9]/ operator = "=" version = constraint else _, operator, version = REGEXP.match(constraint).to_a end if operator.nil? raise Errors::InvalidConstraintFormat.new(constraint) end split_version = case version.to_s when /^(\d+)\.(\d+)\.(\d+)(-([0-9a-z\-\.]+))?(\+([0-9a-z\-\.]+))?$/i [ $1.to_i, $2.to_i, $3.to_i, $5, $7 ] when /^(\d+)\.(\d+)\.(\d+)?$/ [ $1.to_i, $2.to_i, $3.to_i, nil, nil ] when /^(\d+)\.(\d+)?$/ [ $1.to_i, $2.to_i, nil, nil, nil ] when /^(\d+)$/ [ $1.to_i, nil, nil, nil, nil ] else raise Errors::InvalidConstraintFormat.new(constraint) end [ operator, split_version ].flatten end
Public Instance Methods
@param [Object] other
@return [Boolean]
# File lib/solve/constraint.rb, line 216 def ==(other) other.is_a?(self.class) && operator == other.operator && version == other.version end
dep-selector uses include? to determine if a version matches the constriant.
# File lib/solve/constraint.rb, line 223 def inspect "#<#{self.class} #{self}>" end
@return [Symbol]
# File lib/solve/constraint.rb, line 187 def operator_type unless ( type = OPERATOR_TYPES.fetch(operator) ) raise "unknown operator type: #{operator}" end type end
Returns true or false if the given version would be satisfied by the version constraint.
@param [Semverse::Version, to_s
] target
@return [Boolean]
# File lib/solve/constraint.rb, line 201 def satisfies?(target) target = Semverse::Version.coerce(target) return false if !(version == 0) && greedy_match?(target) compare(target) end
# File lib/solve/constraint.rb, line 227 def to_s out = "#{operator} #{major}" out << ".#{minor}" if minor out << ".#{patch}" if patch out << "-#{pre_release}" if pre_release out << "+#{build}" if build out end
Private Instance Methods
@param [Semverse::Version] target
@return [Boolean]
# File lib/solve/constraint.rb, line 251 def compare(target) COMPARE_FUNS.fetch(operator_type).call(self, target) end
Returns true if the given version is a pre-release and if the constraint does not include a pre-release and if the operator isn't < or <=. This avoids greedy matches, e.g. 2.0.0.alpha won't satisfy >= 1.0.0.
@param [Semverse::Version] target_version
# File lib/solve/constraint.rb, line 244 def greedy_match?(target_version) operator_type !~ /less/ && target_version.pre_release? && !version.pre_release? end