class CoinSync::Transaction

Constants

TYPE_PURCHASE
TYPE_SALE
TYPE_SWAP

Attributes

converted[RW]
exchange[R]
number[RW]
time[R]

Public Class Methods

new(number: nil, exchange:, bought_currency:, sold_currency:, time:, bought_amount:, sold_amount:) click to toggle source
# File lib/coinsync/transaction.rb, line 86
def initialize(number: nil, exchange:, bought_currency:, sold_currency:, time:, bought_amount:, sold_amount:)
  @number = number
  @exchange = exchange

  if time.is_a?(Time)
    @time = time.getlocal
  else
    raise "Transaction: '#{time}' is not a valid Time object"
  end

  if bought_amount.is_a?(BigDecimal)
    @bought_amount = bought_amount
  else
    raise "Transaction: '#{bought_amount}' should be a BigDecimal"
  end

  if bought_currency.is_a?(Currency)
    @bought_currency = bought_currency
  else
    raise "Transaction: '#{bought_currency}' is not a valid currency"
  end

  (bought_amount >= 0) or raise "Transaction: bought_amount should not be negative (#{bought_amount})"

  if sold_amount.is_a?(BigDecimal)
    @sold_amount = sold_amount
  else
    raise "Transaction: '#{sold_amount}' should be a BigDecimal"
  end

  if sold_currency.is_a?(Currency) || sold_amount == 0
     @sold_currency = sold_currency
  else
    raise "Transaction: '#{sold_currency}' is not a valid currency"
  end

  (sold_amount >= 0) or raise "Transaction: sold_amount should not be negative (#{sold_amount})"
end