Class: Cliver::Detector

Inherits:
Struct
  • Object
show all
Defined in:
lib/cliver/detector.rb

Overview

Default implementation of the detector needed by Cliver::Assertion, which will take anything that #respond_to?(:to_proc)

Constant Summary collapse

DEFAULT_VERSION_PATTERN =

Default pattern to use when searching #version_command output

/version [0-9][.0-9a-z]+/i.freeze
DEFAULT_COMMAND_ARG =

Default command argument to use against the executable to get version output

'--version'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(command_arg) ⇒ Detector #initialize(version_pattern) ⇒ Detector #initialize(command_arg, version_pattern) ⇒ Detector

Forgiving input, allows either argument if only one supplied.

Parameters:

  • command_arg (String)
  • version_pattern (Regexp)


23
24
25
26
27
# File 'lib/cliver/detector.rb', line 23

def initialize(*args)
  command_arg = args.shift if args.first.kind_of?(String)
  version_pattern = args.shift
  super(command_arg, version_pattern)
end

Instance Attribute Details

#command_argString

The argument to pass to the executable to get current version Defaults to DEFAULT_COMMAND_ARG

Returns:

  • (String)


7
8
9
# File 'lib/cliver/detector.rb', line 7

def command_arg
  @command_arg
end

#version_patternRegexp

The pattern to match the version in #version_command‘s output. Defaults to DEFAULT_VERSION_PATTERN

Returns:

  • (Regexp)
    • the pattern used against the output

    of the #version_command, which should contain a Gem::Version-parsable substring.



7
8
9
# File 'lib/cliver/detector.rb', line 7

def version_pattern
  @version_pattern
end

Instance Method Details

#detect_version(executable_path) ⇒ String

Returns - should be contain Gem::Version-parsable version number.

Parameters:

  • executable_path (String)
    • the path to the executable to test

Returns:

  • (String)
    • should be contain Gem::Version-parsable

    version number.



32
33
34
35
# File 'lib/cliver/detector.rb', line 32

def detect_version(executable_path)
  output = `#{version_command(executable_path).shelljoin} 2>&1`
  output[version_pattern]
end

#to_procProc

This is the interface that any detector must have. If not overridden, returns a proc that wraps #detect_version

Returns:

See Also:



41
42
43
# File 'lib/cliver/detector.rb', line 41

def to_proc
  method(:detect_version).to_proc
end

#version_command(executable_path) ⇒ Array<String>

Parameters:

  • executable_path (String)

    the executable to test

Returns:

  • (Array<String>)


63
64
65
# File 'lib/cliver/detector.rb', line 63

def version_command(executable_path)
  [executable_path, command_arg]
end