Class: Git::VersionConstraint

Inherits:
Data
  • Object
show all
Defined in:
lib/git/version_constraint.rb

Overview

Represents a git version constraint with minimum and upper bound versions

Used by Commands::Base.requires_git_version to declare version requirements and by VersionError to report constraint violations.

Examples:

Minimum version only

constraint = Git::VersionConstraint.new(min: Git::Version.parse('2.30.0'))
constraint.too_old?(Git::Version.parse('2.28.0'))  #=> true
constraint.too_new?(Git::Version.parse('2.28.0'))  #=> false

Upper bound only

constraint = Git::VersionConstraint.new(before: Git::Version.parse('2.50.0'))
constraint.too_old?(Git::Version.parse('2.51.0'))  #=> false
constraint.too_new?(Git::Version.parse('2.51.0'))  #=> true

Both bounds

constraint = Git::VersionConstraint.new(
  min: Git::Version.parse('2.30.0'),
  before: Git::Version.parse('2.50.0')
)
constraint.satisfied_by?(Git::Version.parse('2.40.0'))  #=> true

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(min: nil, before: nil) ⇒ VersionConstraint

Returns a new instance of VersionConstraint.

Parameters:

  • min (Git::Version, nil) (defaults to: nil)

    minimum version (inclusive)

  • before (Git::Version, nil) (defaults to: nil)

    upper bound version (exclusive)



31
32
33
# File 'lib/git/version_constraint.rb', line 31

def initialize(min: nil, before: nil)
  super
end

Instance Attribute Details

#beforeObject (readonly)

Returns the value of attribute before

Returns:

  • (Object)

    the current value of before



28
29
30
# File 'lib/git/version_constraint.rb', line 28

def before
  @before
end

#minObject (readonly)

Returns the value of attribute min

Returns:

  • (Object)

    the current value of min



28
29
30
# File 'lib/git/version_constraint.rb', line 28

def min
  @min
end

Instance Method Details

#satisfied_by?(version) ⇒ Boolean

Check if the given version satisfies this constraint

Parameters:

Returns:

  • (Boolean)

    true if the version satisfies the constraint



65
66
67
# File 'lib/git/version_constraint.rb', line 65

def satisfied_by?(version)
  !too_old?(version) && !too_new?(version)
end

#to_sString

Return a human-readable representation of this constraint

Returns:

  • (String)

    the constraint in git version range form



73
74
75
76
77
78
79
# File 'lib/git/version_constraint.rb', line 73

def to_s
  return ">= #{min}, < #{before}" if min && before
  return ">= #{min}" if min
  return "< #{before}" if before

  'any version'
end

#too_new?(version) ⇒ Boolean

Check if the given version is too new (at or past the upper bound)

Parameters:

Returns:

  • (Boolean)

    true if version is at or past the upper bound, false otherwise



53
54
55
56
57
# File 'lib/git/version_constraint.rb', line 53

def too_new?(version)
  return false unless before

  version >= before
end

#too_old?(version) ⇒ Boolean

Check if the given version is too old (below the minimum)

Parameters:

Returns:

  • (Boolean)

    true if version is below the minimum, false otherwise



41
42
43
44
45
# File 'lib/git/version_constraint.rb', line 41

def too_old?(version)
  return false unless min

  version < min
end