class Table

Public Class Methods

new(map) click to toggle source
# File lib/bean/table.rb, line 6
def initialize(map)
  @map = map
  @left_dash = 20
  @right_dash = 20
end

Public Instance Methods

table() click to toggle source
# File lib/bean/table.rb, line 12
def table
  r = header
  for (key, value) in @map 
    left = @left_dash - key.to_s.length
    right = @right_dash - value.to_s.length
    r << "| #{key}#{indent(left)} | #{value}#{indent(right)} |\n"
  end
  r << line
end

Private Instance Methods

find_largest(arr, target) click to toggle source
# File lib/bean/table.rb, line 42
def find_largest(arr, target)
  largest = target
  for item in arr 
    if item > largest
      largest = item 
    end
  end
  largest
end
header() click to toggle source
# File lib/bean/table.rb, line 24
def header
  max
  left = @left_dash - 6
  right = @right_dash - 12

  r = line
  r << "| Option#{indent(left)} | Configuraton#{indent(right)} |\n"
  r << line
end
indent(n) click to toggle source
# File lib/bean/table.rb, line 52
def indent(n)
  " " * n if n > 0
end
line() click to toggle source
# File lib/bean/table.rb, line 56
def line 
  "+" + "-" * (@left_dash + @right_dash + 5) + "+\n"
end
max() click to toggle source
# File lib/bean/table.rb, line 34
def max()
  keys = @map.keys.flat_map { |e| e.to_s.length }
  values = @map.values.flat_map { |e| e.to_s.length }

  @left_dash = find_largest(keys, @left_dash)
  @right_dash = find_largest(values, @right_dash)
end