Class: Semverse::Constraint
- Inherits:
-
Object
- Object
- Semverse::Constraint
- Defined in:
- lib/semverse/constraint.rb
Constant Summary collapse
- OPERATOR_TYPES =
{ "~>" => :approx, "~" => :approx, ">=" => :greater_than_equal, "<=" => :less_than_equal, "=" => :equal, ">" => :greater_than, "<" => :less_than, }.freeze
- COMPARE_FUNS =
{ approx: method(:compare_approx), greater_than_equal: method(:compare_gte), greater_than: method(:compare_gt), less_than_equal: method(:compare_lte), less_than: method(:compare_lt), equal: method(:compare_equal) }.freeze
- REGEXP =
/^(#{OPERATOR_TYPES.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.
-
#version ⇒ Semverse::Version
readonly
Return the Semverse::Version representation of the major, minor, and patch attributes of this instance.
Class Method Summary collapse
-
.coerce(object) ⇒ Constraint
Coerce the object into a constraint.
- .compare_approx(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(constraint) ⇒ Array?
Split a constraint string into an Array of two elements.
Instance Method Summary collapse
- #==(other) ⇒ Boolean (also: #eql?)
-
#initialize(constraint = nil) ⇒ Constraint
constructor
A new instance of Constraint.
- #inspect ⇒ Object
- #operator_type ⇒ Symbol
-
#satisfies?(target) ⇒ Boolean
(also: #include?)
Returns true or false if the given version would be satisfied by the version constraint.
- #to_s ⇒ Object
Constructor Details
#initialize(constraint = nil) ⇒ Constraint
Returns a new instance of Constraint.
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 |
# File 'lib/semverse/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 = Version.new([ self.major, self.minor, self.patch, self.pre_release, self.build, ]) end |
Instance Attribute Details
#build ⇒ Object (readonly)
Returns the value of attribute build.
155 156 157 |
# File 'lib/semverse/constraint.rb', line 155 def build @build end |
#major ⇒ Object (readonly)
Returns the value of attribute major.
151 152 153 |
# File 'lib/semverse/constraint.rb', line 151 def major @major end |
#minor ⇒ Object (readonly)
Returns the value of attribute minor.
152 153 154 |
# File 'lib/semverse/constraint.rb', line 152 def minor @minor end |
#operator ⇒ Object (readonly)
Returns the value of attribute operator.
150 151 152 |
# File 'lib/semverse/constraint.rb', line 150 def operator @operator end |
#patch ⇒ Object (readonly)
Returns the value of attribute patch.
153 154 155 |
# File 'lib/semverse/constraint.rb', line 153 def patch @patch end |
#pre_release ⇒ Object (readonly)
Returns the value of attribute pre_release.
154 155 156 |
# File 'lib/semverse/constraint.rb', line 154 def pre_release @pre_release end |
#version ⇒ Semverse::Version (readonly)
Return the Semverse::Version representation of the major, minor, and patch attributes of this instance
161 162 163 |
# File 'lib/semverse/constraint.rb', line 161 def version @version end |
Class Method Details
.coerce(object) ⇒ Constraint
Coerce the object into a constraint.
9 10 11 12 13 14 15 |
# File 'lib/semverse/constraint.rb', line 9 def coerce(object) if object.nil? DEFAULT_CONSTRAINT else object.is_a?(self) ? object : new(object) end end |
.compare_approx(constraint, target_version) ⇒ Boolean
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/semverse/constraint.rb', line 110 def compare_approx(constraint, target_version) min = constraint.version max = if constraint.patch.nil? 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 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 Version.new([min.major, min.minor, min.patch, identifiers.fill(replace, -1).join('.')]) else Version.new([min.major, min.minor + 1, 0, 0]) end min <= target_version && target_version < max end |
.compare_equal(constraint, target_version) ⇒ Boolean
70 71 72 |
# File 'lib/semverse/constraint.rb', line 70 def compare_equal(constraint, target_version) target_version == constraint.version end |
.compare_gt(constraint, target_version) ⇒ Boolean
78 79 80 |
# File 'lib/semverse/constraint.rb', line 78 def compare_gt(constraint, target_version) target_version > constraint.version end |
.compare_gte(constraint, target_version) ⇒ Boolean
94 95 96 |
# File 'lib/semverse/constraint.rb', line 94 def compare_gte(constraint, target_version) target_version >= constraint.version end |
.compare_lt(constraint, target_version) ⇒ Boolean
86 87 88 |
# File 'lib/semverse/constraint.rb', line 86 def compare_lt(constraint, target_version) target_version < constraint.version end |
.compare_lte(constraint, target_version) ⇒ Boolean
102 103 104 |
# File 'lib/semverse/constraint.rb', line 102 def compare_lte(constraint, target_version) target_version <= constraint.version end |
.split(constraint) ⇒ 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.
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/semverse/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 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 InvalidConstraintFormat.new(constraint) end [ operator, split_version ].flatten end |
Instance Method Details
#==(other) ⇒ Boolean Also known as: eql?
216 217 218 219 220 |
# File 'lib/semverse/constraint.rb', line 216 def ==(other) other.is_a?(self.class) && self.operator == other.operator && self.version == other.version end |
#inspect ⇒ Object
223 224 225 |
# File 'lib/semverse/constraint.rb', line 223 def inspect "#<#{self.class.to_s} #{to_s}>" end |
#operator_type ⇒ Symbol
187 188 189 190 191 192 193 |
# File 'lib/semverse/constraint.rb', line 187 def operator_type unless type = OPERATOR_TYPES.fetch(operator) raise RuntimeError, "unknown operator type: #{operator}" end type end |
#satisfies?(target) ⇒ Boolean Also known as: include?
Returns true or false if the given version would be satisfied by the version constraint.
201 202 203 204 205 206 207 |
# File 'lib/semverse/constraint.rb', line 201 def satisfies?(target) target = Version.coerce(target) return false if !version.zero? && greedy_match?(target) compare(target) end |
#to_s ⇒ Object
227 228 229 230 231 232 233 234 |
# File 'lib/semverse/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 |