class Aruba::Platforms::SimpleTable

Generate simple table

Attributes

hash[R]
opts[R]

Public Class Methods

new(hash, opts) click to toggle source

Create

@param [Hash] hash

Input
# File lib/aruba/platforms/simple_table.rb, line 17
def initialize(hash, opts)
  @hash = hash
  @opts = {
    sort: true
  }.merge opts
end

Public Instance Methods

to_s() click to toggle source

Generate table

@return [String]

The table
# File lib/aruba/platforms/simple_table.rb, line 28
def to_s
  longest_key = hash.keys.map(&:to_s).max_by(&:length)
  return "" if longest_key.nil?

  name_size = longest_key.length

  rows = hash.map do |k, v|
    format("# %-*s => %s", name_size, k, v)
  end

  if opts[:sort] == true
    rows.sort.join("\n")
  else
    rows.join("\n")
  end
end