Class: Reckon::LedgerParser

Inherits:
Object
  • Object
show all
Defined in:
lib/reckon/ledger_parser.rb

Overview

Parses ledger files

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ LedgerParser

ledger is an object that response to #each_line, (i.e. a StringIO or an IO object)



114
115
116
117
# File 'lib/reckon/ledger_parser.rb', line 114

def initialize(options = {})
  @options = options
  @date_format = options[:ledger_date_format] || options[:date_format] || '%Y-%m-%d'
end

Instance Method Details

#format_row(row, line1, line2) ⇒ Object



177
178
179
180
181
182
183
# File 'lib/reckon/ledger_parser.rb', line 177

def format_row(row, line1, line2)
  note = row[:note] ? "\t; #{row[:note]}" : ""
  out = "#{row[:pretty_date]}\t#{row[:description]}#{note}\n"
  out += "\t#{line1.first}\t\t\t#{line1.last}\n"
  out += "\t#{line2.first}\t\t\t#{line2.last}\n\n"
  out
end

#parse(ledger) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/reckon/ledger_parser.rb', line 119

def parse(ledger)
  entries = []
  new_entry = {}
  in_comment = false
  comment_chars = ';#%*|'
  ledger.each_line do |entry|
    entry.rstrip!
    # strip comment lines
    in_comment = true if entry == 'comment'
    in_comment = false if entry == 'end comment'
    next if in_comment
    next if entry =~ /^\s*[#{comment_chars}]/

    # (date, type, code, description), type and code are optional
    if (m = entry.match(%r{^(\d+[^\s]+)\s+([*!])?\s*(\([^)]+\))?\s*(.*)$}))
      add_entry(entries, new_entry)
      new_entry = {
        date: try_parse_date(m[1]),
        type: m[2] || "",
        code: m[3] && m[3].tr('()', '') || "",
        desc: m[4].strip,
        accounts: []
      }
    elsif entry =~ /^\s*$/ && new_entry[:date]
      add_entry(entries, new_entry)
      new_entry = {}
    elsif new_entry[:date] && entry =~ /^\s+/
      LOGGER.info("Adding new account #{entry}")
      new_entry[:accounts] << (entry)
    else
      LOGGER.info("Unknown entry type: #{entry}")
      add_entry(entries, new_entry)
      new_entry = {}
    end
  end
  add_entry(entries, new_entry)
  entries
end

#to_csv(ledger) ⇒ Object

roughly matches ledger csv format



159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/reckon/ledger_parser.rb', line 159

def to_csv(ledger)
  return parse(ledger).flat_map do |n|
    n[:accounts].map do |a|
      row = [
        n[:date].strftime(@date_format),
        n[:code],
        n[:desc],
        a[:name],
        "", # currency (not implemented)
        a[:amount],
        n[:type],
        "", # account comment (not implemented)
      ]
      CSV.generate_line(row).strip
    end
  end
end