Module: LMCAdm::Helpers

Defined in:
lib/lmcadm/helpers/password_helper.rb,
lib/lmcadm/helpers/args_helpers.rb,
lib/lmcadm/helpers/find_helpers.rb,
lib/lmcadm/helpers/device_helpers.rb,
lib/lmcadm/helpers/string_helpers.rb

Overview

This module includes helpers for shared functionality across the application.

Class Method Summary collapse

Class Method Details

.complete_service_name(name) ⇒ Object



11
12
13
14
15
16
17
18
19
# File 'lib/lmcadm/helpers/string_helpers.rb', line 11

def self.complete_service_name(name)
  if name.start_with? 'cloud-service-'
    return name
  end
  if name.start_with? 'service-'
    return 'cloud-' + name
  end
  return'cloud-service-' + name
end

.ensure_arg(args, kind: 'argument', message: nil) ⇒ Object

Verifies that args contains at least one object. Raises RuntimeError with either the given message or an error explaining that kind is expected.

Parameters:

  • args (Object)
  • kind (String (frozen)) (defaults to: 'argument')

    optional

  • message (Object) (defaults to: nil)

    optional



10
11
12
13
14
15
16
17
18
# File 'lib/lmcadm/helpers/args_helpers.rb', line 10

def self.ensure_arg(args, kind: 'argument', message: nil)
  error = ""
  if args.length < 1
    error = "Argument missing: No #{kind} specified."
    error = message if message
  end
  raise error unless error.empty?
  return args
end

.find_by_id_or_name(list, search) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
# File 'lib/lmcadm/helpers/find_helpers.rb', line 4

def self.find_by_id_or_name(list, search)
  found = []
  found += list.select do |item|
    item.id == search
  end
  found += list.select do |item|
    item.name == search
  end
  puts "More than one item found for: #{search}" if found.length > 1
  raise "Not found: #{search}" if found.length < 1
  return found.first
end

.find_device(devices, name: '', id: '') ⇒ Object



4
5
6
7
8
9
10
11
# File 'lib/lmcadm/helpers/device_helpers.rb', line 4

def self.find_device(devices, name: '', id: '')
  found = devices.select do |device|
    (id == device.id) || (name == device.name)
  end
  raise "More than one device found for: #{name} #{id}" if found.length > 1
  raise "Device not found: #{name} #{id}" if found.length < 1
  return found.first
end

.longest_in_collection(coll) ⇒ Object



3
4
5
6
7
8
9
# File 'lib/lmcadm/helpers/string_helpers.rb', line 3

def self.longest_in_collection(coll)
  max = coll.reduce(0) do |memo, str|
    memo > str.length ? memo : str.length

  end
  return max
end

.read_pw(promptstring = '') ⇒ Object



8
9
10
11
12
13
# File 'lib/lmcadm/helpers/password_helper.rb', line 8

def self.read_pw(promptstring = '')
  print promptstring
  pw = STDIN.noecho(&:gets).strip
  print "\n"
  pw
end