Class: Teri::Entry
- Inherits:
-
Object
- Object
- Teri::Entry
- Defined in:
- lib/teri/transaction.rb
Overview
Represents a single entry (debit or credit) in a transaction
Instance Attribute Summary collapse
-
#account ⇒ Object
readonly
Returns the value of attribute account.
-
#amount ⇒ Object
readonly
Returns the value of attribute amount.
-
#currency ⇒ Object
readonly
Returns the value of attribute currency.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Instance Method Summary collapse
-
#initialize(account:, amount:, type:, currency: 'USD') ⇒ Entry
constructor
Initialize a new entry.
-
#normalize_currency(currency) ⇒ String
Normalize currency to ensure consistent representation.
-
#signed_amount ⇒ Float
Get the signed amount (positive for debits, negative for credits).
-
#to_ledger ⇒ String
Convert the entry to a ledger format string.
Constructor Details
#initialize(account:, amount:, type:, currency: 'USD') ⇒ Entry
Initialize a new entry
11 12 13 14 15 16 17 18 19 |
# File 'lib/teri/transaction.rb', line 11 def initialize(account:, amount:, type:, currency: 'USD') @account = 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
#account ⇒ Object (readonly)
Returns the value of attribute account.
4 5 6 |
# File 'lib/teri/transaction.rb', line 4 def account @account end |
#amount ⇒ Object (readonly)
Returns the value of attribute amount.
4 5 6 |
# File 'lib/teri/transaction.rb', line 4 def amount @amount end |
#currency ⇒ Object (readonly)
Returns the value of attribute currency.
4 5 6 |
# File 'lib/teri/transaction.rb', line 4 def currency @currency end |
#type ⇒ Object (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
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_amount ⇒ Float
Get the signed amount (positive for debits, negative for credits)
32 33 34 |
# File 'lib/teri/transaction.rb', line 32 def signed_amount @type == :debit ? @amount : -@amount end |
#to_ledger ⇒ String
Convert the entry to a ledger format string
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 |