Class: Solve::Constraint
- Inherits:
-
Object
- Object
- Solve::Constraint
- Defined in:
- lib/solve/constraint.rb
Overview
Constant Summary collapse
- OPERATORS =
{ "~>" => method(:compare_aprox), ">=" => method(:compare_gte), "<=" => method(:compare_lte), "=" => method(:compare_equal), "~" => method(:compare_aprox), ">" => method(:compare_gt), "<" => method(:compare_lt) }.freeze
- REGEXP =
/^(#{OPERATORS.keys.join('|')})\s?(.+)$/
Instance Attribute Summary collapse
-
#build ⇒ Object
readonly
Returns the value of attribute build.
-
#major ⇒ Object
readonly
Returns the value of attribute major.
-
#minor ⇒ Object
readonly
Returns the value of attribute minor.
-
#operator ⇒ Object
readonly
Returns the value of attribute operator.
-
#patch ⇒ Object
readonly
Returns the value of attribute patch.
-
#pre_release ⇒ Object
readonly
Returns the value of attribute pre_release.
Class Method Summary collapse
- .compare_aprox(constraint, target_version) ⇒ Boolean
- .compare_equal(constraint, target_version) ⇒ Boolean
- .compare_gt(constraint, target_version) ⇒ Boolean
- .compare_gte(constraint, target_version) ⇒ Boolean
- .compare_lt(constraint, target_version) ⇒ Boolean
- .compare_lte(constraint, target_version) ⇒ Boolean
-
.split(string) ⇒ Array?
Split a constraint string into an Array of two elements.
Instance Method Summary collapse
- #==(other) ⇒ Boolean (also: #eql?)
-
#initialize(constraint = ">= 0.0.0") ⇒ Constraint
constructor
A new instance of Constraint.
-
#satisfies?(target_version) ⇒ Boolean
Returns true or false if the given version would be satisfied by the version constraint.
- #to_s ⇒ Object
-
#version ⇒ Solve::Version
Return the Solve::Version representation of the major, minor, and patch attributes of this instance.
Constructor Details
#initialize(constraint = ">= 0.0.0") ⇒ Constraint
Returns a new instance of Constraint.
123 124 125 126 127 128 129 130 131 |
# File 'lib/solve/constraint.rb', line 123 def initialize(constraint = ">= 0.0.0") @operator, ver_str = self.class.split(constraint) if @operator.nil? || ver_str.nil? raise Errors::InvalidConstraintFormat.new(constraint) end @major, @minor, @patch, @pre_release, @build = Version.split(ver_str) @compare_fun = OPERATORS.fetch(self.operator) end |
Instance Attribute Details
#build ⇒ Object (readonly)
Returns the value of attribute build.
120 121 122 |
# File 'lib/solve/constraint.rb', line 120 def build @build end |
#major ⇒ Object (readonly)
Returns the value of attribute major.
116 117 118 |
# File 'lib/solve/constraint.rb', line 116 def major @major end |
#minor ⇒ Object (readonly)
Returns the value of attribute minor.
117 118 119 |
# File 'lib/solve/constraint.rb', line 117 def minor @minor end |
#operator ⇒ Object (readonly)
Returns the value of attribute operator.
115 116 117 |
# File 'lib/solve/constraint.rb', line 115 def operator @operator end |
#patch ⇒ Object (readonly)
Returns the value of attribute patch.
118 119 120 |
# File 'lib/solve/constraint.rb', line 118 def patch @patch end |
#pre_release ⇒ Object (readonly)
Returns the value of attribute pre_release.
119 120 121 |
# File 'lib/solve/constraint.rb', line 119 def pre_release @pre_release end |
Class Method Details
.compare_aprox(constraint, target_version) ⇒ Boolean
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/solve/constraint.rb', line 84 def compare_aprox(constraint, target_version) min = constraint.version if constraint.patch == nil max = 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 max = 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 max = Version.new([min.major, min.minor, min.patch, identifiers.fill(replace, -1).join('.')]) else max = Version.new([min.major, min.minor + 1, 0, 0]) end min <= target_version && target_version < max end |
.compare_equal(constraint, target_version) ⇒ Boolean
44 45 46 |
# File 'lib/solve/constraint.rb', line 44 def compare_equal(constraint, target_version) target_version == constraint.version end |
.compare_gt(constraint, target_version) ⇒ Boolean
52 53 54 |
# File 'lib/solve/constraint.rb', line 52 def compare_gt(constraint, target_version) target_version > constraint.version end |
.compare_gte(constraint, target_version) ⇒ Boolean
68 69 70 |
# File 'lib/solve/constraint.rb', line 68 def compare_gte(constraint, target_version) target_version >= constraint.version end |
.compare_lt(constraint, target_version) ⇒ Boolean
60 61 62 |
# File 'lib/solve/constraint.rb', line 60 def compare_lt(constraint, target_version) target_version < constraint.version end |
.compare_lte(constraint, target_version) ⇒ Boolean
76 77 78 |
# File 'lib/solve/constraint.rb', line 76 def compare_lte(constraint, target_version) target_version <= constraint.version end |
.split(string) ⇒ Array?
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.
27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/solve/constraint.rb', line 27 def split(string) if string =~ /^[0-9]/ op = "=" ver = string else _, op, ver = REGEXP.match(string).to_a end return nil unless op || ver [ op, ver ] end |
Instance Method Details
#==(other) ⇒ Boolean Also known as: eql?
164 165 166 167 168 |
# File 'lib/solve/constraint.rb', line 164 def ==(other) other.is_a?(self.class) && self.operator == other.operator && self.version == other.version end |
#satisfies?(target_version) ⇒ Boolean
Returns true or false if the given version would be satisfied by the version constraint.
155 156 157 158 159 |
# File 'lib/solve/constraint.rb', line 155 def satisfies?(target_version) target_version = Version.new(target_version.to_s) @compare_fun.call(self, target_version) end |
#to_s ⇒ Object
171 172 173 174 175 176 177 |
# File 'lib/solve/constraint.rb', line 171 def to_s str = operator str += " #{major}.#{minor}.#{patch}" str += "-#{pre_release}" if pre_release str += "+#{build}" if build str end |
#version ⇒ Solve::Version
Return the Solve::Version representation of the major, minor, and patch attributes of this instance
137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/solve/constraint.rb', line 137 def version @version ||= Version.new( [ self.major, self.minor, self.patch, self.pre_release, self.build ] ) end |