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



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

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



17
18
19
20
21
# File 'lib/vagrant/devcommands/util.rb', line 17

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

.collect_mandatory_params(params) ⇒ Object



23
24
25
26
27
# File 'lib/vagrant/devcommands/util.rb', line 23

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

.collect_optional_params(params) ⇒ Object



29
30
31
32
33
# File 'lib/vagrant/devcommands/util.rb', line 29

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

.did_you_mean(command, registry) ⇒ Object



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

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)


49
50
51
# File 'lib/vagrant/devcommands/util.rb', line 49

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

.max_pad(items_list) ⇒ Object



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

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



65
66
67
68
69
# File 'lib/vagrant/devcommands/util.rb', line 65

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



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

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