bean_sprout 0.0.4 → 0.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/bean_sprout/entry.rb +4 -10
- data/lib/bean_sprout/ledger.rb +13 -26
- data/lib/bean_sprout/transaction.rb +6 -3
- data/lib/bean_sprout/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c152d339fb6536aa158b1c41060934381664be78
|
4
|
+
data.tar.gz: ecaed26f13bbbdfdb2befa88bad5336d4e02fb62
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e0836bf16fac2e63bcf6484eee2bfddf6cb5e8902fd2044b211c4fe6be68de4c4747287dfa62531845659932779d9447c0763b8d1235bad551a83c22f3e360de
|
7
|
+
data.tar.gz: 5b665a4661aa28f7f5167e052f1892d13a532da3da38c6f1d02ad5bc5f4039dd0ea782a07321c3550d79d0028b46c784d83f696727671c5d63e1963ff03dd85f
|
data/lib/bean_sprout/entry.rb
CHANGED
@@ -7,29 +7,23 @@ module BeanSprout
|
|
7
7
|
# 1. The account owns the entry, the currency of which is defined as the local
|
8
8
|
# currency;
|
9
9
|
# 2. The amount to be added to the account balance, in local currency;
|
10
|
-
# 3.
|
11
|
-
# 4. Other arbitrary data.
|
10
|
+
# 3. Other arbitrary data.
|
12
11
|
class Sprout
|
13
12
|
include PackagePrivate::InternalClass
|
14
|
-
attr_reader :id, :bean, :amount
|
13
|
+
attr_reader :id, :bean, :amount
|
15
14
|
|
16
15
|
define_public_interface :Entry
|
17
16
|
|
18
|
-
def initialize id, bean, amount
|
17
|
+
def initialize id, bean, amount
|
19
18
|
@id = id
|
20
19
|
@bean = bean
|
21
20
|
@amount = amount.to_d
|
22
|
-
@rate = rate
|
23
|
-
end
|
24
|
-
|
25
|
-
def unified_amount
|
26
|
-
amount * rate
|
27
21
|
end
|
28
22
|
end
|
29
23
|
|
30
24
|
# Public Interface.
|
31
25
|
class Entry < PackagePrivate::PublicInterfaceBase
|
32
|
-
def_default_delegators :amount
|
26
|
+
def_default_delegators :amount
|
33
27
|
def_private_default_delegators :bean
|
34
28
|
|
35
29
|
def account
|
data/lib/bean_sprout/ledger.rb
CHANGED
@@ -13,6 +13,7 @@ module BeanSprout
|
|
13
13
|
@beans = SparseArray.new
|
14
14
|
@sprout_bunches = SparseArray.new
|
15
15
|
@sprouts = SparseArray.new
|
16
|
+
@dummy_accounts = {}
|
16
17
|
end
|
17
18
|
|
18
19
|
def create_account currency, other_data: nil
|
@@ -23,21 +24,14 @@ module BeanSprout
|
|
23
24
|
Account.new(bean, other_data)
|
24
25
|
end
|
25
26
|
|
26
|
-
def create_entry account, amount,
|
27
|
+
def create_entry account, amount, other_data: nil
|
27
28
|
bean = get_target account
|
28
29
|
if not @beans.has_key? bean.id
|
29
30
|
raise "Unkown account #{bean.to_account} refered."
|
30
31
|
end
|
31
32
|
|
32
|
-
if not (rate or bean.currency == base_currency)
|
33
|
-
raise "Rate must be specified if account is not in base currency " +
|
34
|
-
"#{base_currency}."
|
35
|
-
end
|
36
|
-
rate ||= 1
|
37
|
-
|
38
|
-
|
39
33
|
sprout = @sprouts.store do |next_id|
|
40
|
-
Sprout.new(next_id, bean, amount
|
34
|
+
Sprout.new(next_id, bean, amount)
|
41
35
|
end
|
42
36
|
|
43
37
|
Entry.new(sprout, other_data)
|
@@ -62,21 +56,12 @@ module BeanSprout
|
|
62
56
|
commit_entries [entry0, entry1]
|
63
57
|
end
|
64
58
|
|
65
|
-
def
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
elsif to_acc.currency == @base_currency
|
72
|
-
rate0 = to_amount / from_amount
|
73
|
-
else
|
74
|
-
raise "Forex transfer must be to or from an account of base currency."
|
75
|
-
end
|
76
|
-
|
77
|
-
entry0 = create_entry from_acc, -from_amount, rate0
|
78
|
-
entry1 = create_entry to_acc, to_amount, rate1
|
79
|
-
commit_entries [entry0, entry1]
|
59
|
+
def forex_transfer from_acc, to_acc, from_amount, to_amount
|
60
|
+
entry0 = create_entry from_acc, -from_amount
|
61
|
+
entry1 = create_entry (dummy_account from_acc.currency), from_amount
|
62
|
+
entry2 = create_entry to_acc, to_amount
|
63
|
+
entry3 = create_entry (dummy_account to_acc.currency), -to_amount
|
64
|
+
commit_entries [entry0, entry1, entry2, entry3]
|
80
65
|
end
|
81
66
|
|
82
67
|
# TODO: clients can't access ID.
|
@@ -111,8 +96,10 @@ module BeanSprout
|
|
111
96
|
end
|
112
97
|
end
|
113
98
|
|
114
|
-
def dummy_account
|
115
|
-
|
99
|
+
def dummy_account currency = nil
|
100
|
+
currency ||= @base_currency
|
101
|
+
acc = create_account currency, other_data: "This is a dummy account for #{currency}."
|
102
|
+
@dummy_accounts[currency] ||= acc
|
116
103
|
end
|
117
104
|
|
118
105
|
private
|
@@ -20,11 +20,14 @@ module BeanSprout
|
|
20
20
|
end
|
21
21
|
|
22
22
|
def balanced?
|
23
|
-
|
23
|
+
balances = Hash.new(0)
|
24
24
|
@sprouts.each do |sprout|
|
25
|
-
|
25
|
+
currency = sprout.bean.currency
|
26
|
+
balances[currency] += sprout.amount
|
27
|
+
end
|
28
|
+
balances.values.inject(true) do |acc, currency_balance|
|
29
|
+
acc && currency_balance == 0
|
26
30
|
end
|
27
|
-
balance == 0
|
28
31
|
end
|
29
32
|
|
30
33
|
def balanced!
|
data/lib/bean_sprout/version.rb
CHANGED