-
Notifications
You must be signed in to change notification settings - Fork 0
/
txt_reader.rb
56 lines (47 loc) · 1.32 KB
/
txt_reader.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# -*- encoding : utf-8 -*-
require 'rubygems'
# require 'matrix'
require_relative 'depenses'
require_relative 'file_reader'
class TxtReader < FileReader
def initialize(filename, config)
@filename = filename
parse
end
protected
def parse
@participants = nil
f = File.open(filename, "r")
# Scan for participants
while true
line = f.gets
if line.nil?
# end of reached without finding participants ?
raise 'participants list not found in file'
break
end
if line.match(/^Participants:\s*([\w-]+(\s*,\s*[\w-]+)*)/)
@participants = $1.scan(/[\w-]+/)
break
end
end
# Scan for expenses
@depenses = Depenses.new(@participants)
while line = f.gets
next if line[0] == '#' or line.strip.empty? # Ignore commented lines
if line.match(/^\s*[-*]\s*([\w-]+)\s+(\d+([\.,]\d+)?)\s+([^\[\]]+)\s+(\[([\w-]+)(\s*,\s*[\w-]+)*\])?/)
# Match something like "- Chris 4.5 tickets for boat [Bruno, Chris]"
payer = $1
amount = to_f($2)
why = $4
concerned_persons = $5 ? $5.scan(/[\w-]+/) : :tous
@depenses.ajouter_depense(payer, amount, concerned_persons, why)
else
raise "Unparsed line: #{line}"
end
end
end
def to_f(str)
return str.tr(',','.').to_f
end
end