Class: Aruba::Platforms::SimpleTable

Inherits:
Object
  • Object
show all
Defined in:
lib/aruba/platforms/simple_table.rb

Overview

Generate simple table

Instance Method Summary collapse

Constructor Details

#initialize(hash, opts) ⇒ SimpleTable

Create

Parameters:

  • hash (Hash)

    Input



17
18
19
20
21
22
# File 'lib/aruba/platforms/simple_table.rb', line 17

def initialize(hash, opts)
  @hash = hash
  @opts = {
    :sort => true
  }.merge opts
end

Instance Method Details

#to_sString

Generate table

Returns:



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# 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

  if RUBY_VERSION < '2'
    rows = []

    hash.each do |k,v|
      rows << format("# %-#{name_size}s => %s", k, v)
    end
  else
    rows = hash.each_with_object([]) do |(k,v), a|
      a << format("# %-#{name_size}s => %s", k, v)
    end
  end

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