bean_sprout 0.0.2 → 0.0.3

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: c78c75ce139193f5d079d27d004b5753e929a359
4
- data.tar.gz: bd2f0336d6869c9b207c6c202edd1eb2aa669fc6
3
+ metadata.gz: 4733d40d0fe67374824dead281510cda6053ec71
4
+ data.tar.gz: e8865c4596897622f821e6c379c3d766732def86
5
5
  SHA512:
6
- metadata.gz: 52c30a7a443a6e54d3c532d8ad2df273cec319995688244ba78951bac657163856d021d795078339dd448fe503093e50718433aebd50f8372e1760c845eb5f02
7
- data.tar.gz: e43fd99707829ccf80964f31b2c60d2052747645594623b20686ce2485e919802b40f6269a0b9d8aa3b77e40b0f2a7814ee5cfb9f4a42cf870012767ae210b7a
6
+ metadata.gz: d13adb40a9589069865f7c72e217fc92017fc1db491040dc87fba2f30e8a6fa73dff009cb10a63b6a5956243e1a9a7885b5549ac87ff1f1652ba45602d201b39
7
+ data.tar.gz: 8a608a9d8d6822c9c04f4b0ae26493eedef5b9ce3c5ceb2ba39a05a35f4532e8295cc92a19486ea94cd8b12bf3192374917969428b04177ef59edfad2f08e87c
@@ -2,19 +2,22 @@ require 'bean_sprout/struct_from_hash_mixin'
2
2
  require 'bean_sprout/struct_archive_mixin'
3
3
 
4
4
  module BeanSprout
5
- class Account < Struct.new(:currency, :other_data)
5
+ class Account < Struct.new(:currency, :external_id, :other_data)
6
6
  include StructFromHashMixin
7
7
  include StructArchiveMixin
8
8
 
9
+ class BalanceHolder < Struct.new(:value)
10
+ end
11
+
9
12
  def initialize *fields
10
13
  super *fields
11
14
  @entries = []
12
- @balances = [0]
15
+ @balance = BalanceHolder.new(0)
13
16
  end
14
17
 
15
18
  def append_entry entry
16
19
  @entries.push entry
17
- @balances.push balance + entry.accurate_amount
20
+ @balance.value += entry.accurate_amount
18
21
  end
19
22
 
20
23
  def entries
@@ -22,7 +25,7 @@ module BeanSprout
22
25
  end
23
26
 
24
27
  def balance
25
- @balances.last
28
+ @balance.value
26
29
  end
27
30
  end
28
31
  end
@@ -4,13 +4,13 @@ require 'bigdecimal'
4
4
  require 'bigdecimal/util'
5
5
 
6
6
  module BeanSprout
7
- # AccountingEntry is made up of the following fields:
7
+ # Entry is made up of the following fields:
8
8
  # 1. The account owns the entry, the currency of which is defined as the local
9
9
  # currency;
10
10
  # 2. The amount to be added to the account balance, in local currency;
11
11
  # 3. Convention rate from local currency to the base currency;
12
12
  # 4. Other arbitrary data.
13
- class AccountingEntry < Struct.new(:account, :amount, :rate, :other_data)
13
+ class Entry < Struct.new(:account, :amount, :rate, :other_data)
14
14
  include StructFromHashMixin
15
15
  include StructArchiveMixin
16
16
 
@@ -1,5 +1,7 @@
1
1
  module BeanSprout
2
2
  class GlassJar
3
+ # TODO: maybe each transaction can have its own
4
+ # currency.
3
5
  attr_reader :base_currency
4
6
 
5
7
  def initialize base_currency
@@ -7,6 +9,7 @@ module BeanSprout
7
9
 
8
10
  @accounts = {}
9
11
  @account_id = 0
12
+ @account_external_ids = {}
10
13
 
11
14
  @transactions = {}
12
15
  @transaction_id = 0
@@ -14,21 +17,29 @@ module BeanSprout
14
17
  @entries = {}
15
18
  end
16
19
 
17
- def create_account account
20
+ def open_account account
18
21
  account.archive_in_glass_jar self, next_account_id
19
22
  @accounts[account.id] = account
23
+ @account_external_ids[account.external_id] = account
24
+ end
25
+
26
+ def create_account currency, external_id = nil, other_data: nil
27
+ account = Account.new currency, external_id, other_data
28
+ open_account account
20
29
  end
21
30
 
22
31
  def accounts
23
32
  @accounts.values
24
33
  end
25
34
 
26
- # External ID is not supported. Clients must implement
27
- # external ID to internal ID mapping.
28
35
  def account id
29
36
  @accounts[id]
30
37
  end
31
38
 
39
+ def account_for external_id
40
+ @account_external_ids[external_id]
41
+ end
42
+
32
43
  def transactions
33
44
  @transactions.values
34
45
  end
@@ -75,6 +86,18 @@ module BeanSprout
75
86
  commit_transaction(trans)
76
87
  end
77
88
 
89
+ def new_account *args
90
+ Account.new args
91
+ end
92
+
93
+ def new_transaction *args
94
+ Transaction.new args
95
+ end
96
+
97
+ def new_entry *args
98
+ Entry.new args
99
+ end
100
+
78
101
  private
79
102
  def next_account_id
80
103
  @account_id += 1
@@ -2,6 +2,7 @@ require 'bean_sprout/struct_archive_mixin'
2
2
 
3
3
  module BeanSprout
4
4
  class Transaction < Struct.new(:entries, :other_data)
5
+ include StructFromHashMixin
5
6
  include StructArchiveMixin
6
7
 
7
8
  alias entries_data entries
@@ -1,3 +1,3 @@
1
1
  module BeanSprout
2
- VERSION = '0.0.2'
2
+ VERSION = '0.0.3'
3
3
  end
data/lib/bean_sprout.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  require 'bean_sprout/account'
2
- require 'bean_sprout/accounting_entry'
2
+ require 'bean_sprout/entry'
3
3
  require 'bean_sprout/glass_jar'
4
4
  require 'bean_sprout/struct_archive_mixin'
5
5
  require 'bean_sprout/struct_from_hash_mixin'
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.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Liqing Muyi
@@ -63,7 +63,7 @@ files:
63
63
  - Rakefile
64
64
  - lib/bean_sprout.rb
65
65
  - lib/bean_sprout/account.rb
66
- - lib/bean_sprout/accounting_entry.rb
66
+ - lib/bean_sprout/entry.rb
67
67
  - lib/bean_sprout/glass_jar.rb
68
68
  - lib/bean_sprout/struct_archive_mixin.rb
69
69
  - lib/bean_sprout/struct_from_hash_mixin.rb