Class: VagrantPlugins::DevCommands::Util

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant/devcommands/util.rb,
lib/vagrant/devcommands/util/jaro_winkler.rb

Overview

Utility module

Defined Under Namespace

Classes: JaroWinkler

Class Method Summary collapse

Class Method Details

.argv_command(argv, env) ⇒ Object



5
6
7
8
9
10
11
12
13
# File 'lib/vagrant/devcommands/util.rb', line 5

def self.argv_command(argv, env)
  return nil if argv.empty?

  if machine_name?(argv[0].to_s, env.machine_index) && argv.length > 1
    argv[1].to_s
  else
    argv[0].to_s
  end
end

.collect_flags(flags) ⇒ Object



15
16
17
18
19
# File 'lib/vagrant/devcommands/util.rb', line 15

def self.collect_flags(flags)
  flags.collect do |key, _opts|
    "[--#{key}]"
  end
end

.collect_mandatory_params(params) ⇒ Object



21
22
23
24
25
# File 'lib/vagrant/devcommands/util.rb', line 21

def self.collect_mandatory_params(params)
  params.collect do |key, opts|
    "--#{key}=<#{key}>" unless opts[:optional]
  end
end

.collect_optional_params(params) ⇒ Object



27
28
29
30
31
# File 'lib/vagrant/devcommands/util.rb', line 27

def self.collect_optional_params(params)
  params.collect do |key, opts|
    "[--#{key}=<#{key}>]" if opts[:optional]
  end
end

.did_you_mean(command, registry) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/vagrant/devcommands/util.rb', line 33

def self.did_you_mean(command, registry)
  alternatives = registry.commands.keys +
                 registry.chains.keys +
                 Registry::RESERVED_COMMANDS
  distances    = {}

  alternatives.each do |alternative|
    calculator             = Util::JaroWinkler.new(command, alternative)
    distances[alternative] = calculator.distance
  end

  distances
end

.machine_name?(name, machine_index) ⇒ Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/vagrant/devcommands/util.rb', line 47

def self.machine_name?(name, machine_index)
  machine_index.any? { |machine| name == machine.name }
end

.max_pad(items_list) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
# File 'lib/vagrant/devcommands/util.rb', line 51

def self.max_pad(items_list)
  paddings = items_list.map do |items|
    if items.nil? || items.empty?
      0
    else
      pad_to(items)
    end
  end

  paddings.max
end

.pad_to(items) ⇒ Object



63
64
65
66
67
# File 'lib/vagrant/devcommands/util.rb', line 63

def self.pad_to(items)
  items = items.keys unless items.is_a?(Array)

  items.map(&:length).max
end

.padded_columns(pad_to, left, right = nil) ⇒ Object



69
70
71
72
73
74
75
76
77
78
# File 'lib/vagrant/devcommands/util.rb', line 69

def self.padded_columns(pad_to, left, right = nil)
  left  = left.to_s  unless left.is_a?(String)
  right = right.to_s unless right.is_a?(String)

  if right.nil?
    "     #{left}"
  else
    "     #{left.ljust(pad_to)}   #{right}"
  end
end