Class: Cliver::Assertion

Inherits:
Object
  • Object
show all
Includes:
Which
Defined in:
lib/cliver/assertion.rb

Overview

The core of Cliver, Assertion is responsible for detecting the installed version of a binary and determining if it meets the requirements

Constant Summary collapse

DependencyNotMet =
Class.new(ArgumentError)
DependencyVersionMismatch =
Class.new(DependencyNotMet)
DependencyNotFound =
Class.new(DependencyNotMet)
EXECUTABLE_PATTERN =
/\A[a-z][a-zA-Z0-9\-_]*\z/.freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(executable, *requirements, options = {}) ⇒ Assertion

Returns a new instance of Assertion.

Parameters:

  • executable (String)
  • requirements (Array<String>, String)

    splat of strings whose elements follow the pattern

    [<operator>] <version>
    

    Where <operator> is optional (default ‘=”) and in the set

    '=', '!=', '>', '<', '>=', '<=', or '~>'
    

    And <version> is dot-separated integers with optional alphanumeric pre-release suffix @see Gem::Requirement::new

  • options (Hash<Symbol,Object>)

Yield Parameters:

  • full (String)

    path to executable

Yield Returns:

  • (String)

    Gem::Version-parsable string version

Raises:

  • (ArgumentError)


38
39
40
41
42
43
44
45
46
# File 'lib/cliver/assertion.rb', line 38

def initialize(executable, *args, &detector)
  raise ArgumentError, 'executable' unless executable[EXECUTABLE_PATTERN]

  options = args.last.kind_of?(Hash) ? args.pop : {}

  @executable = executable.dup.freeze
  @requirement = Gem::Requirement.new(args) unless args.empty?
  @detector = detector || options.fetch(:detector) { Detector.new }
end

Class Method Details

.assert!(*args, &block) ⇒ Object

Creates a new instance with the args and calls #assert.

See Also:

  • #assert


20
21
22
# File 'lib/cliver/assertion.rb', line 20

def self.assert!(*args, &block)
  new(*args, &block).assert!
end

Instance Method Details

#assert!Object

Raises:



50
51
52
53
54
55
56
57
58
# File 'lib/cliver/assertion.rb', line 50

def assert!
  version = installed_version
  raise(DependencyNotFound, "#{@executable} missing.") unless version

  if @requirement && !@requirement.satisfied_by?(Gem::Version.new(version))
    raise DependencyVersionMismatch,
          "expected #{@executable} to be #{@requirement}, got #{version}"
  end
end

#installed_versionnil, ...

Returns:

  • (nil)

    if no version present

  • (String)

    Gem::Version-parsable string version

  • (true)

    if present and no requirements (optimization)



64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/cliver/assertion.rb', line 64

def installed_version
  executable_path = which(@executable)
  return nil unless executable_path
  return true unless @requirement

  @detector.to_proc.call(executable_path).tap do |version|
    unless version
      raise ArgumentError,
            "found #{@executable} at '#{executable_path}' " +
            'but could not detect its version.'
    end
  end
end