Class: Tablifier

Inherits:
Object
  • Object
show all
Defined in:
lib/coinmon/tablifier.rb

Class Method Summary collapse

Class Method Details

.run(data) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/coinmon/tablifier.rb', line 14

def run(data)
  total_investment, total_value, total_earnings = 0, 0, 0
  table = TTY::Table.new(header: HEADER) do |t|
    data.each do |symbol, data|
      investment = data[:investment]
      value = data[:price]
      earnings = data[:earnings]
      total_investment += investment
      total_value += value
      total_earnings += earnings

      t << [
        symbol.upcase,
        data[:paid_per_unit].round(2),
        data[:price_per_unit].round(2),
        investment.round(2),
        value.round(2),
        earnings.round(2)
      ]
    end
    t << [
      'Total',
      'NA',
      'NA',
      total_investment.round(2),
      total_value.round(2),
      total_earnings.round(2)
    ]
  end

  p = Pastel.new
  filter = proc do |val, row, col|
    if [0, table.rows.length].include? row
      p.white.on_bright_black val
    elsif row > 0 && col == 5
      val.to_i > 0 ? p.black.on_green(val) : p.black.on_red(val)
    else
      val
    end
  end

  table.render :ascii, padding: [0, 2, 0, 2], filter: filter
end