Class: Solve::Constraint
- Inherits:
-
Object
- Object
- Solve::Constraint
- Defined in:
- lib/solve/constraint.rb
Overview
Constant Summary collapse
- OPERATORS =
{ "=" => method(:compare_equal), ">" => method(:compare_gt), "<" => method(:compare_lt), ">=" => method(:compare_gte), "<=" => method(:compare_lte), "~>" => method(:compare_aprox) }.freeze
- REGEXP =
/^(#{OPERATORS.keys.join('|')}) (.+)$/
Instance Attribute Summary collapse
-
#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.
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.
112 113 114 115 116 117 118 119 120 |
# File 'lib/solve/constraint.rb', line 112 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 = Version.split(ver_str) @compare_fun = OPERATORS.fetch(self.operator) end |
Instance Attribute Details
#major ⇒ Object (readonly)
Returns the value of attribute major.
107 108 109 |
# File 'lib/solve/constraint.rb', line 107 def major @major end |
#minor ⇒ Object (readonly)
Returns the value of attribute minor.
108 109 110 |
# File 'lib/solve/constraint.rb', line 108 def minor @minor end |
#operator ⇒ Object (readonly)
Returns the value of attribute operator.
106 107 108 |
# File 'lib/solve/constraint.rb', line 106 def operator @operator end |
#patch ⇒ Object (readonly)
Returns the value of attribute patch.
109 110 111 |
# File 'lib/solve/constraint.rb', line 109 def patch @patch end |
Class Method Details
.compare_aprox(constraint, target_version) ⇒ Boolean
83 84 85 86 87 88 89 90 91 92 |
# File 'lib/solve/constraint.rb', line 83 def compare_aprox(constraint, target_version) unless constraint.patch.nil? target_version.patch >= constraint.patch && target_version.minor == constraint.minor && target_version.major == constraint.major else target_version.minor >= constraint.minor && target_version.major == constraint.major end end |
.compare_equal(constraint, target_version) ⇒ Boolean
43 44 45 |
# File 'lib/solve/constraint.rb', line 43 def compare_equal(constraint, target_version) target_version == constraint.version end |
.compare_gt(constraint, target_version) ⇒ Boolean
51 52 53 |
# File 'lib/solve/constraint.rb', line 51 def compare_gt(constraint, target_version) target_version > constraint.version end |
.compare_gte(constraint, target_version) ⇒ Boolean
67 68 69 |
# File 'lib/solve/constraint.rb', line 67 def compare_gte(constraint, target_version) target_version >= constraint.version end |
.compare_lt(constraint, target_version) ⇒ Boolean
59 60 61 |
# File 'lib/solve/constraint.rb', line 59 def compare_lt(constraint, target_version) target_version < constraint.version end |
.compare_lte(constraint, target_version) ⇒ Boolean
75 76 77 |
# File 'lib/solve/constraint.rb', line 75 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.
26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/solve/constraint.rb', line 26 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?
145 146 147 148 149 150 151 |
# File 'lib/solve/constraint.rb', line 145 def ==(other) other.is_a?(self.class) && self.operator == other.operator && self.major == other.minor && self.minor == other.minor && self.patch == other.patch end |
#satisfies?(target_version) ⇒ Boolean
Returns true or false if the given version would be satisfied by the version constraint.
136 137 138 139 140 |
# File 'lib/solve/constraint.rb', line 136 def satisfies?(target_version) target_version = Version.new(target_version.to_s) @compare_fun.call(self, target_version) end |
#to_s ⇒ Object
154 155 156 |
# File 'lib/solve/constraint.rb', line 154 def to_s "#{operator} #{major}.#{minor}.#{patch}" end |
#version ⇒ Solve::Version
Return the Solve::Version representation of the major, minor, and patch attributes of this instance
126 127 128 |
# File 'lib/solve/constraint.rb', line 126 def version @version ||= Version.new([self.major, self.minor, self.patch]) end |