class Banks::TransactionArray
Public Instance Methods
<<(transaction)
click to toggle source
Calls superclass method
# File lib/banks/transaction.rb, line 138 def << (transaction) super #transaction.category = (CategoryPolicy.categorize transaction.description).upcase end
by_date(begin_date, end_date)
click to toggle source
Return transactions within a date range.
# File lib/banks/transaction.rb, line 63 def by_date (begin_date, end_date) result = TransactionArray.new # Filter all less than end date. temp_array = TransactionArray.new each do |transaction| if transaction.date <= end_date then temp_array << transaction end end # Filter all greater than begin date. temp_array.each do |transaction| if transaction.date >= begin_date then result << transaction end end return result end
current_month()
click to toggle source
Return current month transactions.
# File lib/banks/transaction.rb, line 81 def current_month return previous_month 0 end
last_month()
click to toggle source
Return last month transactions.
# File lib/banks/transaction.rb, line 86 def last_month return previous_month 1 end
merge_array(merging_array)
click to toggle source
Merge array
# File lib/banks/transaction.rb, line 132 def merge_array(merging_array) merging_array.each do |transaction| self << transaction end end
previous_month(months_back)
click to toggle source
months_back - how many months to go back from current date.
# File lib/banks/transaction.rb, line 91 def previous_month(months_back) year = Date.today.year month = Date.today.month begin_date = Date.civil(year, month, 1) - months_back.month end_date = begin_date.end_of_month return by_date(begin_date, end_date) end
remove_transfers()
click to toggle source
Remove transactions considered transfers to own accounts.
# File lib/banks/transaction.rb, line 50 def remove_transfers result = TransactionArray.new each do |transaction| should_add = true # Currently unavailable. puts "Removing transaction. " + transaction.to_s unless should_add == true if should_add then result << transaction end end return result end
sort_by_amount()
click to toggle source
Sort by amount. Negative to positive.
# File lib/banks/transaction.rb, line 40 def sort_by_amount sort! {|x, y| x.amount <=> y.amount} end
sort_by_category()
click to toggle source
Sort by category.
# File lib/banks/transaction.rb, line 45 def sort_by_category sort! {|x, y| x.category <=> y.category} end
sort_by_date()
click to toggle source
Sorts transactions by date, from newer to older.
# File lib/banks/transaction.rb, line 35 def sort_by_date sort! {|x, y| y.date <=> x.date} end
to_s()
click to toggle source
# File lib/banks/transaction.rb, line 101 def to_s result = "" each do |transaction| result = result + transaction.to_s + "\n" end result end
to_s_by_cat()
click to toggle source
# File lib/banks/transaction.rb, line 109 def to_s_by_cat result = "" current_cat = nil total = 0 last_transaction = nil each do |transaction| if !current_cat.nil? && current_cat != transaction.category result = result + "Total for category #{current_cat}: " + Banks.amount_to_s(total) + "\n\n" total = 0 end result = result + transaction.to_s + "\n" total = total + transaction.amount current_cat = transaction.category last_transaction = transaction end if !last_transaction.nil? then result = result + "Total for category #{current_cat}: " + Banks.amount_to_s(total) + "\n\n" total = 0 end result end
total()
click to toggle source
# File lib/banks/transaction.rb, line 10 def total total = 0 each do |transaction| total = total + transaction.amount end Banks.amount_to_s(total) end
total_negative()
click to toggle source
# File lib/banks/transaction.rb, line 26 def total_negative total = 0 each do |transaction| total = total + transaction.amount unless transaction.amount > 0 end Banks.amount_to_s(total) end
total_positive()
click to toggle source
# File lib/banks/transaction.rb, line 18 def total_positive total = 0 each do |transaction| total = total + transaction.amount unless transaction.amount < 0 end Banks.amount_to_s(total) end