Class: Teri::Entry

Inherits:
Object
  • Object
show all
Defined in:
lib/teri/transaction.rb

Overview

Represents a single entry (debit or credit) in a transaction

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(account:, amount:, type:, currency: 'USD') ⇒ Entry

Initialize a new entry

Parameters:

  • account (String)

    The account name

  • amount (Float)

    The amount (positive value)

  • currency (String) (defaults to: 'USD')

    The currency code

  • type (Symbol)

    Either :debit or :credit

Raises:

  • (ArgumentError)


11
12
13
14
15
16
17
18
19
# File 'lib/teri/transaction.rb', line 11

def initialize(account:, amount:, type:, currency: 'USD')
  @account = 
  @amount = amount.abs # Always store as positive
  @currency = normalize_currency(currency)
  @type = type.to_sym

  raise ArgumentError, 'Type must be :debit or :credit' unless [:debit, :credit].include?(@type)
  raise ArgumentError, 'Amount must be positive' unless @amount.positive?
end

Instance Attribute Details

#accountObject (readonly)

Returns the value of attribute account.



4
5
6
# File 'lib/teri/transaction.rb', line 4

def 
  @account
end

#amountObject (readonly)

Returns the value of attribute amount.



4
5
6
# File 'lib/teri/transaction.rb', line 4

def amount
  @amount
end

#currencyObject (readonly)

Returns the value of attribute currency.



4
5
6
# File 'lib/teri/transaction.rb', line 4

def currency
  @currency
end

#typeObject (readonly)

Returns the value of attribute type.



4
5
6
# File 'lib/teri/transaction.rb', line 4

def type
  @type
end

Instance Method Details

#normalize_currency(currency) ⇒ String

Normalize currency to ensure consistent representation

Parameters:

  • currency (String)

    The currency string to normalize

Returns:

  • (String)

    The normalized currency string



24
25
26
27
28
# File 'lib/teri/transaction.rb', line 24

def normalize_currency(currency)
  return 'USD' if currency == '$' || currency.to_s.strip.upcase == 'USD'

  currency.to_s.strip.upcase
end

#signed_amountFloat

Get the signed amount (positive for debits, negative for credits)

Returns:

  • (Float)

    The signed amount



32
33
34
# File 'lib/teri/transaction.rb', line 32

def signed_amount
  @type == :debit ? @amount : -@amount
end

#to_ledgerString

Convert the entry to a ledger format string

Returns:

  • (String)

    The entry in ledger format



38
39
40
41
# File 'lib/teri/transaction.rb', line 38

def to_ledger
  # Always format as $ for consistency with source transactions
  "    #{@account}  $#{signed_amount}"
end