Class: Solve::Constraint

Inherits:
Object
  • Object
show all
Defined in:
lib/solve/constraint.rb

Overview

Author:

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(constraint = ">= 0.0.0") ⇒ Constraint

Returns a new instance of Constraint.

Parameters:

  • constraint (#to_s) (defaults to: ">= 0.0.0")


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

#buildObject (readonly)

Returns the value of attribute build.



120
121
122
# File 'lib/solve/constraint.rb', line 120

def build
  @build
end

#majorObject (readonly)

Returns the value of attribute major.



116
117
118
# File 'lib/solve/constraint.rb', line 116

def major
  @major
end

#minorObject (readonly)

Returns the value of attribute minor.



117
118
119
# File 'lib/solve/constraint.rb', line 117

def minor
  @minor
end

#operatorObject (readonly)

Returns the value of attribute operator.



115
116
117
# File 'lib/solve/constraint.rb', line 115

def operator
  @operator
end

#patchObject (readonly)

Returns the value of attribute patch.



118
119
120
# File 'lib/solve/constraint.rb', line 118

def patch
  @patch
end

#pre_releaseObject (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

Parameters:

Returns:

  • (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

Parameters:

Returns:

  • (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

Parameters:

Returns:

  • (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

Parameters:

Returns:

  • (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

Parameters:

Returns:

  • (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

Parameters:

Returns:

  • (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.

Examples:

splitting a string with a constraint operator and valid version string

Constraint.split(">= 1.0.0") => [ ">=", "1.0.0" ]

splitting a string without a constraint operator

Constraint.split("0.0.0") => [ "=", "1.0.0" ]

splitting a string without a valid version string

Constraint.split("hello") => nil

Parameters:

Returns:

  • (Array, nil)


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?

Parameters:

  • other (Object)

Returns:

  • (Boolean)


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.

Parameters:

  • target_version (#to_s)

Returns:

  • (Boolean)


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_sObject



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

#versionSolve::Version

Return the Solve::Version representation of the major, minor, and patch attributes of this instance

Returns:



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