Class: SidekiqUniqueJobs::VersionCheck

Inherits:
Object
  • Object
show all
Defined in:
lib/sidekiq_unique_jobs/version_check.rb

Overview

Handles checking if a version is compliant with given constraint

Author:

Constant Summary collapse

PATTERN =

rubocop:disable Layout/LineLength, Lint/MixedRegexpCaptureTypes

/(?<operator1>[<>=]+)?\s?(?<version1>(\d+.?)+)(\s+&&\s+)?(?<operator2>[<>=]+)?\s?(?<version2>(\d+.?)+)?/m.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(version, constraint) ⇒ VersionCheck

Initialize a new VersionCheck instance

Parameters:

  • version (String)

    a version string ‘5.0.0`

  • constraint (String)

    a version constraint ‘>= 5.0.0 <= 5.1.1`

Raises:

  • (ArgumentError)


65
66
67
68
69
70
71
# File 'lib/sidekiq_unique_jobs/version_check.rb', line 65

def initialize(version, constraint)
  @version   = Gem::Version.new(version)
  @match     = PATTERN.match(constraint.to_s)

  raise ArgumentError, "A version (eg. 5.0) is required to compare against" unless @version
  raise ArgumentError, "At least one operator and version is required (eg. >= 5.1)" unless constraint
end

Instance Attribute Details

#matchObject (readonly)

Returns the value of attribute match.



57
58
59
# File 'lib/sidekiq_unique_jobs/version_check.rb', line 57

def match
  @match
end

#versionObject (readonly)

Returns the value of attribute version.



53
54
55
# File 'lib/sidekiq_unique_jobs/version_check.rb', line 53

def version
  @version
end

Class Method Details

.satisfied?(version, constraint) ⇒ true, false

Checks if a version is constraint is satisfied

Examples:

A satisfied constraint

VersionCheck.satisfied?("5.0.0", ">= 4.0.0") #=> true

An unsatisfied constraint

VersionCheck.satisfied?("5.0.0", "<= 4.0.0") #=> false

Parameters:

  • version (String)

    a version string ‘5.0.0`

  • constraint (String)

    a version constraint ‘>= 5.0.0 <= 5.1.1`

Returns:

  • (true, false)

    <description>



27
28
29
# File 'lib/sidekiq_unique_jobs/version_check.rb', line 27

def self.satisfied?(version, constraint)
  new(version, constraint).satisfied?
end

.unfulfilled?(version, constraint) ⇒ true, false

Checks if a version is constraint is unfulfilled

Examples:

A satisfied constraint

VersionCheck.unfulfilled?("5.0.0", ">= 4.0.0") #=> false

An unfulfilled constraint

VersionCheck.unfulfilled?("5.0.0", "<= 4.0.0") #=> true

Parameters:

  • version (String)

    a version string ‘5.0.0`

  • constraint (String)

    a version constraint ‘>= 5.0.0 <= 5.1.1`

Returns:

  • (true, false)

    <description>



46
47
48
# File 'lib/sidekiq_unique_jobs/version_check.rb', line 46

def self.unfulfilled?(version, constraint)
  !satisfied?(version, constraint)
end

Instance Method Details

#satisfied?true, false

Checks if all constraints were met

Returns:

  • (true, false)


79
80
81
82
83
# File 'lib/sidekiq_unique_jobs/version_check.rb', line 79

def satisfied?
  constraints.all? do |expected, operator|
    compare(expected, operator)
  end
end