Method: Solve::Constraint.split

Defined in:
lib/solve/constraint.rb

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

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:

  • constraint (#to_s)

Returns:

  • (Array, nil)


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/solve/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 Errors::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 Errors::InvalidConstraintFormat.new(constraint)
  end

  [ operator, split_version ].flatten
end