-
Notifications
You must be signed in to change notification settings - Fork 68
/
account.rb
159 lines (129 loc) · 4.21 KB
/
account.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# encoding: utf-8
require 'forwardable'
module DoubleEntry
class Account
class << self
attr_accessor :scope_identifier_max_length, :account_identifier_max_length
attr_writer :accounts
# @api private
def accounts
@accounts ||= Set.new
end
# @api private
def account(identifier, options = {})
account = accounts.find(identifier, (options[:scope].present? || options[:scope_identity].present?))
Instance.new(account: account, scope: options[:scope], scope_identity: options[:scope_identity])
end
# @api private
def currency(identifier)
accounts.find_without_scope(identifier).try(:currency)
end
end
# @api private
class Set
extend Forwardable
delegate [:each, :map] => :all
def define(attributes)
Account.new(attributes).tap do |account|
if find_without_scope(account.identifier)
fail DuplicateAccount
else
backing_collection[account.identifier] = account
end
end
end
def find(identifier, scoped)
found_account = find_without_scope(identifier)
if found_account && found_account.scoped? == scoped
found_account
else
fail UnknownAccount, "account: #{identifier} scoped?: #{scoped}"
end
end
def find_without_scope(identifier)
backing_collection[identifier]
end
def all
backing_collection.values
end
private
def backing_collection
@backing_collection ||= Hash.new
end
end
class Instance
attr_reader :account, :scope
delegate :identifier, :scope_identifier, :scoped?, :positive_only, :negative_only, :currency, to: :account
def initialize(args)
@account = args[:account]
@scope = args[:scope]
@scope_identity = args[:scope_identity]
ensure_scope_is_valid
end
def scope_identity
@scope_identity || call_scope_identifier
end
def call_scope_identifier
scope_identifier.call(scope).to_s if scoped?
end
# Get the current or historic balance of this account.
#
# @option options :from [Time]
# @option options :to [Time]
# @option options :at [Time]
# @option options :code [Symbol]
# @option options :codes [Array<Symbol>]
# @return [Money]
#
def balance(options = {})
BalanceCalculator.calculate(self, options)
end
include Comparable
def ==(other)
other.is_a?(self.class) && identifier == other.identifier && scope_identity == other.scope_identity
end
def eql?(other)
self == other
end
def <=>(other)
[scope_identity.to_s, identifier.to_s] <=> [other.scope_identity.to_s, other.identifier.to_s]
end
def hash
if scoped?
"#{scope_identity}:#{identifier}".hash
else
identifier.hash
end
end
def to_s
"\#{Account account: #{identifier} scope: #{scope} currency: #{currency}}"
end
def inspect
to_s
end
private
def ensure_scope_is_valid
identity = scope_identity
if identity && Account.scope_identifier_max_length && identity.length > Account.scope_identifier_max_length
fail ScopeIdentifierTooLongError,
"scope identifier '#{identity}' is too long. Please limit it to #{Account.scope_identifier_max_length} characters."
end
end
end
attr_reader :identifier, :scope_identifier, :positive_only, :negative_only, :currency
def initialize(args)
@identifier = args[:identifier]
@scope_identifier = args[:scope_identifier]
@positive_only = args[:positive_only]
@negative_only = args[:negative_only]
@currency = args[:currency] || Money.default_currency
if Account.account_identifier_max_length && identifier.length > Account.account_identifier_max_length
fail AccountIdentifierTooLongError,
"account identifier '#{identifier}' is too long. Please limit it to #{Account.account_identifier_max_length} characters."
end
end
def scoped?
!!scope_identifier
end
end
end