Class: Cliver::Assertion
- Inherits:
-
Object
- Object
- Cliver::Assertion
- 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 =
An exception class raised when assertion is not met
Class.new(ArgumentError)
- DependencyVersionMismatch =
An exception that is raised when executable present is the wrong version
Class.new(DependencyNotMet)
- DependencyNotFound =
An exception that is raised when executable is not present
Class.new(DependencyNotMet)
Instance Method Summary collapse
- #assert! ⇒ Object
-
#initialize(executable, *requirements, options = {}) ⇒ Assertion
constructor
A new instance of Assertion.
-
#installed_version ⇒ nil, ...
Finds the executable on your path using Which; if the executable is present and version requirements are specified, uses the specified detector to get the current version.
Constructor Details
#initialize(executable, *requirements, options = {}) ⇒ Assertion
Returns a new instance of Assertion.
35 36 37 38 39 40 41 |
# File 'lib/cliver/assertion.rb', line 35 def initialize(executable, *args, &detector) = args.last.kind_of?(Hash) ? args.pop : {} @executable = executable.dup.freeze @requirement = Gem::Requirement.new(args) unless args.empty? @detector = detector || .fetch(:detector) { Detector.new } end |
Instance Method Details
#assert! ⇒ Object
45 46 47 48 49 50 51 52 53 |
# File 'lib/cliver/assertion.rb', line 45 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_version ⇒ nil, ...
Finds the executable on your path using Which; if the executable is present and version requirements are specified, uses the specified detector to get the current version.
62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/cliver/assertion.rb', line 62 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 |