Class: Solve::Constraint

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

Overview

Author:

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

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")


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

#majorObject (readonly)

Returns the value of attribute major.



107
108
109
# File 'lib/solve/constraint.rb', line 107

def major
  @major
end

#minorObject (readonly)

Returns the value of attribute minor.



108
109
110
# File 'lib/solve/constraint.rb', line 108

def minor
  @minor
end

#operatorObject (readonly)

Returns the value of attribute operator.



106
107
108
# File 'lib/solve/constraint.rb', line 106

def operator
  @operator
end

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

Parameters:

Returns:

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

Parameters:

Returns:

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

Parameters:

Returns:

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

Parameters:

Returns:

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

Parameters:

Returns:

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

Parameters:

Returns:

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

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)


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?

Parameters:

  • other (Object)

Returns:

  • (Boolean)


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.

Parameters:

  • target_version (#to_s)

Returns:

  • (Boolean)


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_sObject



154
155
156
# File 'lib/solve/constraint.rb', line 154

def to_s
  "#{operator} #{major}.#{minor}.#{patch}"
end

#versionSolve::Version

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

Returns:



126
127
128
# File 'lib/solve/constraint.rb', line 126

def version
  @version ||= Version.new([self.major, self.minor, self.patch])
end