bean_sprout 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8f8703dcac93814b06f78ef5c7f6081d41e3809c
4
- data.tar.gz: 3d50d400de7ff74305f88db06a0feef6c6816658
3
+ metadata.gz: c152d339fb6536aa158b1c41060934381664be78
4
+ data.tar.gz: ecaed26f13bbbdfdb2befa88bad5336d4e02fb62
5
5
  SHA512:
6
- metadata.gz: 73181700e7e1a3e20b552bc4010cdd4820718fc19c7990968183fad0e8f3e4aa99a49b043099e261e5a60556b06a7d5e56de2744516f54babbb0776dcba93c41
7
- data.tar.gz: 85fb4800498552ed1a3a5f3b491c07456467044f048682ea3198387bef22f1ff407634449f8723ca9991d5646939242f7c58fd9b9a9c7c0edc24559b76e32757
6
+ metadata.gz: e0836bf16fac2e63bcf6484eee2bfddf6cb5e8902fd2044b211c4fe6be68de4c4747287dfa62531845659932779d9447c0763b8d1235bad551a83c22f3e360de
7
+ data.tar.gz: 5b665a4661aa28f7f5167e052f1892d13a532da3da38c6f1d02ad5bc5f4039dd0ea782a07321c3550d79d0028b46c784d83f696727671c5d63e1963ff03dd85f
@@ -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. Convention rate from local currency to the base currency;
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, :rate
13
+ attr_reader :id, :bean, :amount
15
14
 
16
15
  define_public_interface :Entry
17
16
 
18
- def initialize id, bean, amount, rate = 1
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, :unified_amount, :rate
26
+ def_default_delegators :amount
33
27
  def_private_default_delegators :bean
34
28
 
35
29
  def account
@@ -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, rate = nil, other_data: nil
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, rate)
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 base_currency_forex_transfer from_acc, to_acc, from_amount, to_amount
66
- raise "Amount can't be 0." unless from_amount != 0 && to_amount != 0
67
-
68
- rate0 = rate1 = nil
69
- if from_acc.currency == @base_currency
70
- rate1 = from_amount / to_amount
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
- @dummy_account ||= create_account @base_currency, other_data: "This is a dummy account."
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
- balance = 0
23
+ balances = Hash.new(0)
24
24
  @sprouts.each do |sprout|
25
- balance += sprout.unified_amount
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!
@@ -1,3 +1,3 @@
1
1
  module BeanSprout
2
- VERSION = '0.0.4'
2
+ VERSION = '0.0.5'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bean_sprout
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Liqing Muyi