double_entry 0.7.1 → 0.7.2

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 614ed4f674917475306caf7d8ebbd2d71c72a5b0
4
- data.tar.gz: ff2a94aa12cb75fa558ecab769c47a59cf40226e
3
+ metadata.gz: 32faa0991a3e20450e5c310ed76831804f8f8489
4
+ data.tar.gz: b4d643d8739f316dc7d0b3404bfd13d6bbc5f678
5
5
  SHA512:
6
- metadata.gz: 1c98e390fa45e02e0e117c7283964db04b6eb7d4b5c27d3de54f8d541284c40c6c557c46c47ef15d6caadb94db5341da1c4095f6adaa269675f6c5e0f100329f
7
- data.tar.gz: da1243b2adf4f047a90250cd30ecefb49d478869dc1ed2b44bb3d0263e34ed831e8afd155bdfcf7d5c33cba077f282e1e0ac6ae28c2a20a202bda8c5131d12a2
6
+ metadata.gz: 3f23c6af662e48e0dd8e520111628faf5f9d59e5d6b3e10d5a9cd3cb94ec9f6f5d9829d5bc86636ec4f5cac7dc6ca502a6ef011baba6e4c3b70aa7944b5a042d
7
+ data.tar.gz: 34d17bcd0b139a5dad177a588c737a5ef2b3704db52b83812b6dae3e2ac600a4eef4a2b4a642d11474e2cf074eeaebdb3d64e489e1c34033f4c67283bcb0791d
data/.travis.yml CHANGED
@@ -1,6 +1,6 @@
1
1
  language: ruby
2
2
  rvm:
3
- - 2.1.4
3
+ - 2.1.5
4
4
  - 1.9.3
5
5
  env:
6
6
  - DB=mysql
data/README.md CHANGED
@@ -21,7 +21,7 @@ DoubleEntry is tested against:
21
21
 
22
22
  Ruby
23
23
  * 1.9.3
24
- * 2.1.4
24
+ * 2.1.5
25
25
 
26
26
  Rails
27
27
  * 3.2.x
data/lib/double_entry.rb CHANGED
@@ -41,7 +41,7 @@ module DoubleEntry
41
41
  # match that defined on the account.
42
42
  #
43
43
  def account(identifier, options = {})
44
- Account.account(configuration.accounts, identifier, options)
44
+ Account.account(identifier, options)
45
45
  end
46
46
 
47
47
  # Transfer money from one account to another.
@@ -77,7 +77,7 @@ module DoubleEntry
77
77
  # accounts with the provided code is not allowed. Check configuration.
78
78
  #
79
79
  def transfer(amount, options = {})
80
- Transfer.transfer(configuration.transfers, amount, options)
80
+ Transfer.transfer(amount, options)
81
81
  end
82
82
 
83
83
  # Get the current or historic balance of an account.
@@ -135,14 +135,6 @@ module DoubleEntry
135
135
  BalanceCalculator.calculate(account, options)
136
136
  end
137
137
 
138
- # Get the currency of an account.
139
- #
140
- # @param [DoubleEntry::Account:Instance, Symbol] account Find the currency for this account
141
- # @return [Currency] the currency
142
- def currency(account)
143
- Account.currency(configuration.accounts, account)
144
- end
145
-
146
138
  # Lock accounts in preparation for transfers.
147
139
  #
148
140
  # This creates a transaction, and uses database-level locking to ensure
@@ -2,21 +2,24 @@
2
2
  module DoubleEntry
3
3
  class Account
4
4
 
5
- # @api private
6
- def self.account(defined_accounts, identifier, options = {})
7
- account = defined_accounts.find(identifier, options[:scope].present?)
8
- DoubleEntry::Account::Instance.new(:account => account, :scope => options[:scope])
9
- end
5
+ class << self
6
+ attr_writer :accounts
10
7
 
11
- # @api private
12
- def self.currency(defined_accounts, account)
13
- code = account.is_a?(Symbol) ? account : account.identifier
8
+ # @api private
9
+ def accounts
10
+ @accounts ||= Set.new
11
+ end
14
12
 
15
- found_account = defined_accounts.detect do |account|
16
- account.identifier == code
13
+ # @api private
14
+ def account(identifier, options = {})
15
+ account = accounts.find(identifier, options[:scope].present?)
16
+ Instance.new(:account => account, :scope => options[:scope])
17
17
  end
18
18
 
19
- found_account.currency
19
+ # @api private
20
+ def currency(identifier)
21
+ accounts.detect { |a| a.identifier == identifier }.try(:currency)
22
+ end
20
23
  end
21
24
 
22
25
  # @api private
@@ -66,11 +69,12 @@ module DoubleEntry
66
69
  end
67
70
 
68
71
  class Instance
69
- attr_accessor :account, :scope
72
+ attr_reader :account, :scope
70
73
  delegate :identifier, :scope_identifier, :scoped?, :positive_only, :currency, :to => :account
71
74
 
72
- def initialize(attributes)
73
- attributes.each { |name, value| send("#{name}=", value) }
75
+ def initialize(args)
76
+ @account = args[:account]
77
+ @scope = args[:scope]
74
78
  ensure_scope_is_valid
75
79
  end
76
80
 
@@ -128,11 +132,13 @@ module DoubleEntry
128
132
  end
129
133
  end
130
134
 
131
- attr_accessor :identifier, :scope_identifier, :positive_only, :currency
135
+ attr_reader :identifier, :scope_identifier, :positive_only, :currency
132
136
 
133
- def initialize(attributes)
134
- attributes.each { |name, value| send("#{name}=", value) }
135
- self.currency ||= Money.default_currency
137
+ def initialize(args)
138
+ @identifier = args[:identifier]
139
+ @scope_identifier = args[:scope_identifier]
140
+ @positive_only = args[:positive_only]
141
+ @currency = args[:currency] || Money.default_currency
136
142
  end
137
143
 
138
144
  def scoped?
@@ -17,11 +17,10 @@ module DoubleEntry
17
17
  options = Options.new(account, args)
18
18
  relations = RelationBuilder.new(options)
19
19
  lines = relations.build
20
- currency = DoubleEntry.currency(account)
21
20
 
22
21
  if options.between? || options.code?
23
22
  # from and to or code lookups have to be done via sum
24
- Money.new(lines.sum(:amount), currency)
23
+ Money.new(lines.sum(:amount), account.currency)
25
24
  else
26
25
  # all other lookups can be performed with running balances
27
26
  result = lines.
@@ -29,7 +28,7 @@ module DoubleEntry
29
28
  order('id DESC').
30
29
  limit(1).
31
30
  pluck(:balance)
32
- result.empty? ? Money.zero(currency) : Money.new(result.first, currency)
31
+ result.empty? ? Money.zero(account.currency) : Money.new(result.first, account.currency)
33
32
  end
34
33
  end
35
34
 
@@ -3,12 +3,8 @@ module DoubleEntry
3
3
  include Configurable
4
4
 
5
5
  class Configuration
6
- attr_accessor :accounts, :transfers
7
-
8
- def initialize #:nodoc:
9
- @accounts = Account::Set.new
10
- @transfers = Transfer::Set.new
11
- end
6
+ delegate :accounts, :accounts=, :to => "DoubleEntry::Account"
7
+ delegate :transfers, :transfers=, :to => "DoubleEntry::Transfer"
12
8
 
13
9
  def define_accounts
14
10
  yield accounts
@@ -47,7 +47,7 @@ module DoubleEntry
47
47
  end
48
48
 
49
49
  def currency
50
- DoubleEntry.currency(account)
50
+ DoubleEntry::Account.currency(account)
51
51
  end
52
52
 
53
53
  def clear_old_aggregates
@@ -64,7 +64,7 @@ module DoubleEntry
64
64
  end
65
65
 
66
66
  def currency
67
- DoubleEntry.currency(account)
67
+ DoubleEntry::Account.currency(account)
68
68
  end
69
69
  end
70
70
  end
@@ -2,13 +2,23 @@
2
2
  module DoubleEntry
3
3
  class Transfer
4
4
 
5
- # @api private
6
- def self.transfer(defined_transfers, amount, options = {})
7
- raise TransferIsNegative if amount < Money.zero
8
- from, to, code, detail = options[:from], options[:to], options[:code], options[:detail]
9
- defined_transfers.
10
- find!(from, to, code).
11
- process(amount, from, to, code, detail)
5
+ class << self
6
+ attr_writer :transfers
7
+
8
+ # @api private
9
+ def transfers
10
+ @transfers ||= Set.new
11
+ end
12
+
13
+ # @api private
14
+ def transfer(amount, options = {})
15
+ raise TransferIsNegative if amount < Money.zero
16
+ from = options[:from]
17
+ to = options[:to]
18
+ code = options[:code]
19
+ detail = options[:detail]
20
+ transfers.find!(from, to, code).process(amount, from, to, code, detail)
21
+ end
12
22
  end
13
23
 
14
24
  # @api private
@@ -1,5 +1,5 @@
1
1
  # encoding: utf-8
2
2
 
3
3
  module DoubleEntry
4
- VERSION = "0.7.1"
4
+ VERSION = "0.7.2"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: double_entry
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.1
4
+ version: 0.7.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Anthony Sellitti
@@ -15,7 +15,7 @@ authors:
15
15
  autorequire:
16
16
  bindir: bin
17
17
  cert_chain: []
18
- date: 2014-11-17 00:00:00.000000000 Z
18
+ date: 2014-11-18 00:00:00.000000000 Z
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
21
  name: money