ledger_web 1.4.3 → 1.4.4
Sign up to get free protection for your applications and to get access to all the features.
- data/bin/ledger_web +4 -0
- data/lib/ledger_web/app.rb +0 -2
- data/lib/ledger_web/db.rb +22 -12
- data/lib/ledger_web/version.rb +1 -1
- data/lib/ledger_web/watcher.rb +2 -1
- data/test/database_load_spec.rb +26 -0
- data/test/fixtures/complex_costs.dat +1 -0
- metadata +4 -2
data/bin/ledger_web
CHANGED
@@ -3,6 +3,8 @@
|
|
3
3
|
require 'rubygems'
|
4
4
|
require 'optparse'
|
5
5
|
|
6
|
+
STDOUT.sync = true
|
7
|
+
|
6
8
|
begin
|
7
9
|
require 'ledger_web'
|
8
10
|
rescue LoadError => e
|
@@ -27,5 +29,7 @@ OptionParser.new do |opts|
|
|
27
29
|
end
|
28
30
|
end.parse!
|
29
31
|
|
32
|
+
LedgerWeb::Database.connect
|
33
|
+
|
30
34
|
LedgerWeb::Watcher.run!
|
31
35
|
LedgerWeb::Application.run!(:port => LedgerWeb::Config.instance.get(:port))
|
data/lib/ledger_web/app.rb
CHANGED
data/lib/ledger_web/db.rb
CHANGED
@@ -32,15 +32,15 @@ module LedgerWeb
|
|
32
32
|
ledger_bin_path = LedgerWeb::Config.instance.get :ledger_bin_path
|
33
33
|
ledger_file = LedgerWeb::Config.instance.get :ledger_file
|
34
34
|
ledger_format = LedgerWeb::Config.instance.get :ledger_format
|
35
|
-
|
36
|
-
|
35
|
+
|
36
|
+
puts "Dumping ledger to file..."
|
37
37
|
file = Tempfile.new('ledger')
|
38
38
|
system "#{ledger_bin_path} -f #{ledger_file} --format='#{ledger_format}' reg > #{file.path}"
|
39
39
|
replaced_file = Tempfile.new('ledger')
|
40
40
|
replaced_file.write(file.read.gsub('\"', '""'))
|
41
41
|
replaced_file.flush
|
42
42
|
|
43
|
-
puts "
|
43
|
+
puts "Dump finished"
|
44
44
|
return replaced_file
|
45
45
|
end
|
46
46
|
|
@@ -49,12 +49,12 @@ module LedgerWeb
|
|
49
49
|
@@db.transaction do
|
50
50
|
|
51
51
|
LedgerWeb::Config.instance.run_hooks(:before_load, @@db)
|
52
|
-
|
53
|
-
|
52
|
+
|
53
|
+
puts "Clearing ledger table...."
|
54
54
|
@@db["DELETE FROM ledger"].delete
|
55
|
-
puts "
|
55
|
+
puts "Done clearing ledger table"
|
56
56
|
|
57
|
-
|
57
|
+
puts "Loading into database...."
|
58
58
|
|
59
59
|
CSV.foreach(file.path) do |row|
|
60
60
|
counter += 1
|
@@ -75,21 +75,31 @@ module LedgerWeb
|
|
75
75
|
|
76
76
|
row[:xtn_month] = xtn_date.strftime('%Y/%m/01')
|
77
77
|
row[:xtn_year] = xtn_date.strftime('%Y/01/01')
|
78
|
-
row[:cost] = row[:cost]
|
78
|
+
row[:cost] = parse_cost(row[:cost])
|
79
79
|
|
80
80
|
row = LedgerWeb::Config.instance.run_hooks(:before_insert_row, row)
|
81
81
|
@@db[:ledger].insert(row)
|
82
82
|
LedgerWeb::Config.instance.run_hooks(:after_insert_row, row)
|
83
83
|
end
|
84
84
|
|
85
|
-
puts "
|
85
|
+
puts "Running after_load hooks"
|
86
86
|
LedgerWeb::Config.instance.run_hooks(:after_load, @@db)
|
87
87
|
end
|
88
|
-
puts "
|
88
|
+
puts "Analyzing ledger table"
|
89
89
|
@@db.fetch('VACUUM ANALYZE ledger').all
|
90
|
-
puts "
|
90
|
+
puts "Done!"
|
91
91
|
counter
|
92
92
|
end
|
93
|
+
|
94
|
+
def self.parse_cost(cost)
|
95
|
+
match = cost.match(/([\d\.-]+) (.+) {(.+)} \[(.+)\]/)
|
96
|
+
if match
|
97
|
+
amount = match[1].to_f
|
98
|
+
price = match[3].gsub(/[^\d\.-]/, '').to_f
|
99
|
+
return price * amount
|
100
|
+
end
|
101
|
+
cost.gsub(/[^\d\.-]/, '')
|
102
|
+
end
|
93
103
|
|
94
104
|
def self.load_prices
|
95
105
|
query = <<HERE
|
@@ -113,7 +123,7 @@ module LedgerWeb
|
|
113
123
|
) x
|
114
124
|
HERE
|
115
125
|
|
116
|
-
puts "Deleting prices"
|
126
|
+
puts " Deleting prices"
|
117
127
|
@@db["DELETE FROM prices"].delete
|
118
128
|
|
119
129
|
rows = @@db.fetch(query)
|
data/lib/ledger_web/version.rb
CHANGED
data/lib/ledger_web/watcher.rb
CHANGED
@@ -15,11 +15,12 @@ module LedgerWeb
|
|
15
15
|
@@dw.interval = LedgerWeb::Config.instance.get :watch_interval
|
16
16
|
@@dw.stable = LedgerWeb::Config.instance.get :watch_stable_count
|
17
17
|
|
18
|
+
LedgerWeb::Database.connect
|
19
|
+
|
18
20
|
@@dw.add_observer do |*args|
|
19
21
|
args.each do |event|
|
20
22
|
if event[0] == :stable
|
21
23
|
puts "Loading database"
|
22
|
-
LedgerWeb::Database.connect
|
23
24
|
LedgerWeb::Database.run_migrations
|
24
25
|
file = LedgerWeb::Database.dump_ledger_to_csv
|
25
26
|
count = LedgerWeb::Database.load_database(file)
|
data/test/database_load_spec.rb
CHANGED
@@ -218,5 +218,31 @@ describe LedgerWeb::Database do
|
|
218
218
|
])
|
219
219
|
|
220
220
|
end
|
221
|
+
|
222
|
+
it "should load the database from csv with a complicated cost string" do
|
223
|
+
count = 0
|
224
|
+
File.open(fixture('complex_costs')) do |file|
|
225
|
+
count = LedgerWeb::Database.load_database(file)
|
226
|
+
end
|
227
|
+
count.should eq(1)
|
228
|
+
|
229
|
+
convert_bd_to_string(LedgerWeb::Database.handle[:ledger].all()).should eq([
|
230
|
+
{
|
231
|
+
:xtn_date => Date.new(2011,8,30),
|
232
|
+
:checknum => nil,
|
233
|
+
:note => 'Tttttttttt Pagamento',
|
234
|
+
:account => 'Liabilities:Funds:Retirement Plan',
|
235
|
+
:commodity => '"B PGBL F10"',
|
236
|
+
:amount => "103.59",
|
237
|
+
:tags => '',
|
238
|
+
:xtn_month => Date.new(2011,8,1),
|
239
|
+
:xtn_year => Date.new(2011,1,1),
|
240
|
+
:virtual => true,
|
241
|
+
:xtn_id => 7288,
|
242
|
+
:cleared => false,
|
243
|
+
:cost => "442.33"
|
244
|
+
},
|
245
|
+
])
|
246
|
+
end
|
221
247
|
end
|
222
248
|
end
|
@@ -0,0 +1 @@
|
|
1
|
+
"7288","2011/08/30","Tttttttttt Pagamento","Liabilities:Funds:Retirement Plan","""B PGBL F10""","103.59","false","true","","103.59 ""B PGBL F10"" {R$4.2701032918} [2011/08/30]"
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ledger_web
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.4.
|
4
|
+
version: 1.4.4
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-03-
|
12
|
+
date: 2012-03-24 00:00:00.000000000Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: pg
|
@@ -297,6 +297,7 @@ files:
|
|
297
297
|
- lib/ledger_web/watcher.rb
|
298
298
|
- test/config_spec.rb
|
299
299
|
- test/database_load_spec.rb
|
300
|
+
- test/fixtures/complex_costs.dat
|
300
301
|
- test/fixtures/quoted.dat
|
301
302
|
- test/fixtures/small.dat
|
302
303
|
- test/report_spec.rb
|
@@ -329,6 +330,7 @@ summary: A web-based, sql-backed front-end for the Ledger command-line accountin
|
|
329
330
|
test_files:
|
330
331
|
- test/config_spec.rb
|
331
332
|
- test/database_load_spec.rb
|
333
|
+
- test/fixtures/complex_costs.dat
|
332
334
|
- test/fixtures/quoted.dat
|
333
335
|
- test/fixtures/small.dat
|
334
336
|
- test/report_spec.rb
|