Class: Journala::Journal
- Inherits:
-
Object
- Object
- Journala::Journal
- Defined in:
- lib/journala/journal.rb
Instance Attribute Summary collapse
-
#entries ⇒ Object
Returns the value of attribute entries.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize ⇒ Journal
constructor
A new instance of Journal.
-
#merge(other_journal) ⇒ Object
merge journal with another journal.
- #print ⇒ Object
- #sort_by_date ⇒ Object
- #write_to_file(filename, overwrite = false) ⇒ Object
Constructor Details
#initialize ⇒ Journal
Returns a new instance of Journal.
5 6 7 |
# File 'lib/journala/journal.rb', line 5 def initialize @entries = [] end |
Instance Attribute Details
#entries ⇒ Object
Returns the value of attribute entries.
3 4 5 |
# File 'lib/journala/journal.rb', line 3 def entries @entries end |
Class Method Details
.create_from_file(filename) ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/journala/journal.rb', line 59 def create_from_file(filename) file = File.open(filename, 'r') journal = Journal.new current_entry = nil file.each_line do |line| if line =~ /^$/ next elsif line =~ /^(\d\d\d\d\/\d\d\/\d\d)\s*(\*)*\s*(.*)/ date = $1 confirmed = $2 description = $3 current_entry = Entry.new(:date => date, :confirmed => confirmed.present?, :description => description) journal.entries << current_entry # row with amount elsif line =~ /\s+(.*)\s{2,}(-?)\$?([\d\.]+)/ row = Row.new(:account => $1, :amount => $2.blank? ? $3 : (0.0 - $3.to_f)) current_entry.rows << row # row without amount elsif line =~ /\s+(.*)/ # already have 2 rows and try to do one without an amount? sorry, that's not allowed raise "Must specify amount for each row for entries with more than 2 rows" if current_entry.rows.count >= 2 other_amount = current_entry.rows.first.amount row = Row.new(:account => $1, :amount => 0-other_amount) current_entry.rows << row end end file.close journal end |
Instance Method Details
#merge(other_journal) ⇒ Object
merge journal with another journal. when an identical transaction is found, keep our own entry, and discard other_journal’s entry. the idea is that we read in an existing journal from a file, and then add new entries only to it
46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/journala/journal.rb', line 46 def merge(other_journal) puts "before merge: #{@entries.count} entries" other_journal.entries.each do |entry| transaction_id = entry.description.match(/\(transaction id: (\d+)\)/)[1] if @entries.none? {|e| e.description.match(/\(transaction id: #{transaction_id}\)/) } @entries << entry end end puts "after merge: #{@entries.count} entries" end |
#print ⇒ Object
9 10 11 12 13 14 15 16 17 |
# File 'lib/journala/journal.rb', line 9 def print sort_by_date @entries.each do |x| puts x puts "\n" end nil end |
#sort_by_date ⇒ Object
19 20 21 22 23 |
# File 'lib/journala/journal.rb', line 19 def sort_by_date @entries = @entries.sort do |a,b| a.date <=> b.date end end |
#write_to_file(filename, overwrite = false) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/journala/journal.rb', line 25 def write_to_file(filename, overwrite=false) if File.exist?(filename) && !overwrite raise "File #{filename} already exists. Set second parameter to true to overwrite anyway" end sort_by_date f = File.open(filename, 'w') @entries.each do |e| f.puts(e) f.puts "\n" end f.close end |