Class: CoinSync::Outputs::Summary

Inherits:
Base
  • Object
show all
Defined in:
lib/coinsync/outputs/summary.rb

Instance Method Summary collapse

Methods inherited from Base

#initialize, register_output

Constructor Details

This class inherits a constructor from CoinSync::Outputs::Base

Instance Method Details

#process_transactions(transactions, *args) ⇒ Object



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
# File 'lib/coinsync/outputs/summary.rb', line 16

def process_transactions(transactions, *args)
  totals = Hash.new { BigDecimal(0) }

  transactions.each do |tx|
    break if args.first.to_i > 0 && tx.time.year >= args.first.to_i

    if tx.bought_currency.crypto?
      amount = totals[tx.bought_currency]
      totals[tx.bought_currency] = amount + tx.bought_amount
    end

    if tx.sold_currency.crypto?
      amount = totals[tx.sold_currency]
      if amount >= tx.sold_amount
        totals[tx.sold_currency] = amount - tx.sold_amount
      else
        raise "Summary: couldn't sell #{@formatter.format_crypto(tx.sold_amount)} #{tx.sold_currency.code} " +
          "if only #{@formatter.format_crypto(amount)} was owned"
      end
    end
  end

  rows = totals.map do |currency, amount|
    [
      currency.code,
      @formatter.format_crypto(amount)
    ]
  end

  printer = TablePrinter.new
  printer.print_table(['Coin', 'Amount'], rows, alignment: [:ljust, :rjust])
end

#requires_currency_conversion?Boolean

Returns:

  • (Boolean)


12
13
14
# File 'lib/coinsync/outputs/summary.rb', line 12

def requires_currency_conversion?
  false
end