Class: Banks::TransactionArray
- Inherits:
-
Array
- Object
- Array
- Banks::TransactionArray
- Defined in:
- lib/banks/transaction.rb
Instance Method Summary collapse
- #<<(transaction) ⇒ Object
-
#by_date(begin_date, end_date) ⇒ Object
Return transactions within a date range.
-
#current_month ⇒ Object
Return current month transactions.
-
#last_month ⇒ Object
Return last month transactions.
-
#merge_array(merging_array) ⇒ Object
Merge array.
-
#previous_month(months_back) ⇒ Object
months_back - how many months to go back from current date.
-
#remove_transfers ⇒ Object
Remove transactions considered transfers to own accounts.
-
#sort_by_amount ⇒ Object
Sort by amount.
-
#sort_by_category ⇒ Object
Sort by category.
-
#sort_by_date ⇒ Object
Sorts transactions by date, from newer to older.
- #to_s ⇒ Object
- #to_s_by_cat ⇒ Object
- #total ⇒ Object
- #total_negative ⇒ Object
- #total_positive ⇒ Object
Instance Method Details
#<<(transaction) ⇒ Object
138 139 140 141 |
# File 'lib/banks/transaction.rb', line 138 def << (transaction) super #transaction.category = (CategoryPolicy.categorize transaction.description).upcase end |
#by_date(begin_date, end_date) ⇒ Object
Return transactions within a date range.
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# 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 ⇒ Object
Return current month transactions.
81 82 83 |
# File 'lib/banks/transaction.rb', line 81 def current_month return previous_month 0 end |
#last_month ⇒ Object
Return last month transactions.
86 87 88 |
# File 'lib/banks/transaction.rb', line 86 def last_month return previous_month 1 end |
#merge_array(merging_array) ⇒ Object
Merge array
132 133 134 135 136 |
# 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) ⇒ Object
months_back - how many months to go back from current date.
91 92 93 94 95 96 97 98 99 |
# 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 ⇒ Object
Remove transactions considered transfers to own accounts.
50 51 52 53 54 55 56 57 58 59 60 |
# 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 ⇒ Object
Sort by amount. Negative to positive.
40 41 42 |
# File 'lib/banks/transaction.rb', line 40 def sort_by_amount sort! {|x, y| x.amount <=> y.amount} end |
#sort_by_category ⇒ Object
Sort by category.
45 46 47 |
# File 'lib/banks/transaction.rb', line 45 def sort_by_category sort! {|x, y| x.category <=> y.category} end |
#sort_by_date ⇒ Object
Sorts transactions by date, from newer to older.
35 36 37 |
# File 'lib/banks/transaction.rb', line 35 def sort_by_date sort! {|x, y| y.date <=> x.date} end |
#to_s ⇒ Object
101 102 103 104 105 106 107 |
# 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 ⇒ Object
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# 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 ⇒ Object
10 11 12 13 14 15 16 |
# 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 ⇒ Object
26 27 28 29 30 31 32 |
# 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 ⇒ Object
18 19 20 21 22 23 24 |
# 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 |