twobook 0.1.1
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 +7 -0
- data/.gitignore +14 -0
- data/.rspec +3 -0
- data/.rubocop.yml +59 -0
- data/.travis.yml +5 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +37 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/twobook/account.rb +167 -0
- data/lib/twobook/account_query.rb +208 -0
- data/lib/twobook/agreement.rb +79 -0
- data/lib/twobook/configuration.rb +18 -0
- data/lib/twobook/corrections.rb +111 -0
- data/lib/twobook/entry.rb +15 -0
- data/lib/twobook/event.rb +87 -0
- data/lib/twobook/event_processing.rb +61 -0
- data/lib/twobook/handler/booking_helpers.rb +67 -0
- data/lib/twobook/handler/query_helpers.rb +83 -0
- data/lib/twobook/handler.rb +59 -0
- data/lib/twobook/number_handling.rb +22 -0
- data/lib/twobook/serialization.rb +82 -0
- data/lib/twobook/utilities.rb +39 -0
- data/lib/twobook/version.rb +3 -0
- data/lib/twobook.rb +19 -0
- data/twobook.gemspec +31 -0
- metadata +155 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 821400fab063ba0c19515c146c267e7b2fb934a5
|
4
|
+
data.tar.gz: 76a76e0812bfee424e16523da971fbe0c409438c
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 9666b1a26e7eaaf9e10e5d8d893141c40166ceaaab4ce326caa15ef8ef7b98fd9538e3ba5ead40951dba1f1273eb26a1c951174f1d22147e9da145813abf36a3
|
7
|
+
data.tar.gz: 0874b4b4aed90a687f3e2c94270d63cb46134636a3db54b27d226bcd87296898dc383504f974832ba917960af3a2009f744e9e0b89c893e46403a70686d2cda8
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
AllCops:
|
2
|
+
TargetRubyVersion: 2.3
|
3
|
+
|
4
|
+
Metrics/LineLength:
|
5
|
+
Max: 120 # ~the width of a github code panel
|
6
|
+
|
7
|
+
Metrics/MethodLength:
|
8
|
+
CountComments: false
|
9
|
+
Max: 50
|
10
|
+
|
11
|
+
Metrics/BlockLength:
|
12
|
+
Exclude:
|
13
|
+
- "Rakefile"
|
14
|
+
- "**/*.rake"
|
15
|
+
- "spec/**/*.rb"
|
16
|
+
|
17
|
+
Metrics/ClassLength:
|
18
|
+
Enabled: false
|
19
|
+
Metrics/ModuleLength:
|
20
|
+
Enabled: false
|
21
|
+
|
22
|
+
Style/TrailingCommaInLiteral:
|
23
|
+
EnforcedStyleForMultiline: consistent_comma
|
24
|
+
Style/TrailingCommaInArguments:
|
25
|
+
EnforcedStyleForMultiline: comma
|
26
|
+
|
27
|
+
Style/Documentation:
|
28
|
+
Enabled: false
|
29
|
+
|
30
|
+
Style/StringLiterals:
|
31
|
+
EnforcedStyle: single_quotes
|
32
|
+
|
33
|
+
Style/CollectionMethods:
|
34
|
+
Enabled: true
|
35
|
+
PreferredMethods:
|
36
|
+
collect: 'map'
|
37
|
+
collect!: 'map!'
|
38
|
+
inject: 'reduce'
|
39
|
+
find: 'detect'
|
40
|
+
find_all: 'select'
|
41
|
+
|
42
|
+
Style/AsciiComments:
|
43
|
+
Enabled: false
|
44
|
+
|
45
|
+
Style/Lambda:
|
46
|
+
EnforcedStyle: literal
|
47
|
+
|
48
|
+
Style/FrozenStringLiteralComment:
|
49
|
+
Enabled: false
|
50
|
+
|
51
|
+
Style/MutableConstant:
|
52
|
+
Enabled: false
|
53
|
+
|
54
|
+
Metrics/AbcSize:
|
55
|
+
Max: 45
|
56
|
+
Metrics/CyclomaticComplexity:
|
57
|
+
Max: 9
|
58
|
+
Metrics/PerceivedComplexity:
|
59
|
+
Max: 10
|
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2017 Michael Parry
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
# Twobook
|
2
|
+
|
3
|
+
Double-entry accounting with superpowers (alpha!)
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'twobook'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install twobook
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
TODO
|
24
|
+
|
25
|
+
## Development
|
26
|
+
|
27
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
28
|
+
|
29
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
30
|
+
|
31
|
+
## Contributing
|
32
|
+
|
33
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/michael.parry@novicap.com/twobook.
|
34
|
+
|
35
|
+
## License
|
36
|
+
|
37
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "twobook"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,167 @@
|
|
1
|
+
module Twobook
|
2
|
+
class Account
|
3
|
+
ACCOUNT_TYPES = %i(assets liabilities revenue expenses records)
|
4
|
+
|
5
|
+
attr_reader :name, :balance, :data, :tags, :entries, :ledger
|
6
|
+
|
7
|
+
def initialize(balance: 0, **data)
|
8
|
+
@balance = Twobook.wrap_number(balance)
|
9
|
+
@entries = []
|
10
|
+
|
11
|
+
@data = data
|
12
|
+
@name = define_name
|
13
|
+
|
14
|
+
valid_data = self.class.name_includes + self.class.has
|
15
|
+
data.keys.each do |key|
|
16
|
+
raise "Invalid data #{key} for #{self.class.category}" unless key.in?(valid_data)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def clone
|
21
|
+
c = super
|
22
|
+
c.instance_variable_set(:@entries, @entries.map(&:clone))
|
23
|
+
c.instance_variable_set(:@data, @data.deep_dup)
|
24
|
+
c
|
25
|
+
end
|
26
|
+
|
27
|
+
def <<(other)
|
28
|
+
raise 'Can only append entries to accounts' unless other.is_a?(Entry)
|
29
|
+
|
30
|
+
@entries << other
|
31
|
+
@entries.sort_by!(&:event)
|
32
|
+
@balance = Twobook.wrap_number(@balance + other.amount)
|
33
|
+
update_mutable_data(other.data)
|
34
|
+
self
|
35
|
+
end
|
36
|
+
|
37
|
+
def +(other)
|
38
|
+
clone << other
|
39
|
+
end
|
40
|
+
|
41
|
+
def balance_before_event(event)
|
42
|
+
@entries.reduce(0) do |running_total, entry|
|
43
|
+
return running_total if entry.event >= event
|
44
|
+
running_total + entry.amount
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
def data_before_event(event)
|
49
|
+
@entries.reduce(@data.slice(*self.class.name_includes)) do |running_total, entry|
|
50
|
+
return running_total if entry.event >= event
|
51
|
+
running_total.merge(entry.data)
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
def balance_before(time)
|
56
|
+
@entries.reduce(0) do |running_total, entry|
|
57
|
+
return running_total if entry.event.happened_at >= time
|
58
|
+
running_total + entry.amount
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
def data_before(time)
|
63
|
+
@entries.reduce(@data.slice(*self.class.name_includes)) do |running_total, entry|
|
64
|
+
return running_total if entry.event.happened_at >= time
|
65
|
+
running_total.merge(entry.data)
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
def update_mutable_data(data)
|
70
|
+
validate_data_mutable(data)
|
71
|
+
@data = @data.merge(data).sort.to_h
|
72
|
+
self
|
73
|
+
end
|
74
|
+
|
75
|
+
def validate_data_mutable(new_data)
|
76
|
+
new_data.keys.each do |k|
|
77
|
+
raise "Attribute #{k} cannot be modified in #{inspect}" if k.in?(self.class.name_includes)
|
78
|
+
raise "Unknown parameter #{k} given to #{inspect}" unless k.in?(self.class.has)
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
# Account equality is based only on name, which must be unique
|
83
|
+
def ==(other)
|
84
|
+
@name == other.name
|
85
|
+
end
|
86
|
+
alias eql? ==
|
87
|
+
|
88
|
+
def hash
|
89
|
+
@name.hash
|
90
|
+
end
|
91
|
+
|
92
|
+
def inspect
|
93
|
+
inspected_balance = @balance.to_f
|
94
|
+
"<#{self.class.name} @name=#{@name} @balance=#{inspected_balance} entry_count=#{@entries.count}>"
|
95
|
+
end
|
96
|
+
|
97
|
+
def self.category
|
98
|
+
name.underscore.gsub("#{Twobook.configuration.accounting_namespace.underscore}/accounts/", '')
|
99
|
+
end
|
100
|
+
|
101
|
+
def self.from_name(name)
|
102
|
+
category_part = name.split(':').first
|
103
|
+
match = types.detect do |t|
|
104
|
+
t.name =~ /#{category_part.camelize}$/
|
105
|
+
end
|
106
|
+
raise "Bad account name: #{name}" unless match
|
107
|
+
match
|
108
|
+
end
|
109
|
+
|
110
|
+
def self.types
|
111
|
+
Utilities.types(Twobook::Account)
|
112
|
+
end
|
113
|
+
|
114
|
+
def self.tagged?(tag)
|
115
|
+
if tag.is_a?(Array)
|
116
|
+
tag.empty? || tag.all? { |t| tagged?(t) }
|
117
|
+
else
|
118
|
+
tags.include?(tag)
|
119
|
+
end
|
120
|
+
end
|
121
|
+
|
122
|
+
def self.account_type(*args)
|
123
|
+
return @account_type if args.empty?
|
124
|
+
unless args.first.in?(ACCOUNT_TYPES)
|
125
|
+
raise "Invalid account type #{args.first} for #{name}. Valid types: #{ACCOUNT_TYPES}"
|
126
|
+
end
|
127
|
+
@account_type = args.first
|
128
|
+
end
|
129
|
+
|
130
|
+
def self.name_includes(*args)
|
131
|
+
@name_includes ||= []
|
132
|
+
return @name_includes if args.empty?
|
133
|
+
@name_includes += args
|
134
|
+
end
|
135
|
+
|
136
|
+
def self.tags(*args)
|
137
|
+
@tags ||= []
|
138
|
+
return @tags if args.empty?
|
139
|
+
@tags += args
|
140
|
+
end
|
141
|
+
|
142
|
+
def self.has(*args)
|
143
|
+
@has ||= []
|
144
|
+
return @has if args.empty?
|
145
|
+
@has += args
|
146
|
+
end
|
147
|
+
|
148
|
+
def self.description(*args)
|
149
|
+
@description ||= ''
|
150
|
+
return @description if args.empty?
|
151
|
+
@description = args.first
|
152
|
+
end
|
153
|
+
|
154
|
+
private
|
155
|
+
|
156
|
+
def define_name
|
157
|
+
default = self.class.category
|
158
|
+
custom_parts = self.class.name_includes.sort.map do |key|
|
159
|
+
value = @data.dig(key)
|
160
|
+
raise "Cannot initialize #{self.class.name}: needed #{key}" if value.blank?
|
161
|
+
value
|
162
|
+
end
|
163
|
+
|
164
|
+
([default] + custom_parts).join(':')
|
165
|
+
end
|
166
|
+
end
|
167
|
+
end
|
@@ -0,0 +1,208 @@
|
|
1
|
+
module Twobook
|
2
|
+
class AccountQuery
|
3
|
+
def after(event)
|
4
|
+
@after = event
|
5
|
+
self
|
6
|
+
end
|
7
|
+
|
8
|
+
def none?
|
9
|
+
false
|
10
|
+
end
|
11
|
+
|
12
|
+
def execute(array)
|
13
|
+
array
|
14
|
+
end
|
15
|
+
|
16
|
+
def and(other)
|
17
|
+
AndQuery.new(self, other)
|
18
|
+
end
|
19
|
+
alias & and
|
20
|
+
|
21
|
+
def or(other)
|
22
|
+
OrQuery.new(self, other)
|
23
|
+
end
|
24
|
+
alias | or
|
25
|
+
|
26
|
+
def where(constraints)
|
27
|
+
WhereQuery.new(self, constraints)
|
28
|
+
end
|
29
|
+
|
30
|
+
def named(constraints)
|
31
|
+
WhereQuery.new(self, constraints).convert_to_name_query
|
32
|
+
end
|
33
|
+
|
34
|
+
def none
|
35
|
+
NoneQuery.new
|
36
|
+
end
|
37
|
+
|
38
|
+
def self.where(constraints)
|
39
|
+
new.where(constraints)
|
40
|
+
end
|
41
|
+
|
42
|
+
def self.named(constraints)
|
43
|
+
new.named(constraints)
|
44
|
+
end
|
45
|
+
|
46
|
+
def self.none
|
47
|
+
new.none
|
48
|
+
end
|
49
|
+
|
50
|
+
class NoneQuery < AccountQuery
|
51
|
+
def execute(_)
|
52
|
+
[]
|
53
|
+
end
|
54
|
+
|
55
|
+
def none?
|
56
|
+
true
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
class WhereQuery < AccountQuery
|
61
|
+
ATTRIBUTE_CONSTRAINTS = %i(ledger name)
|
62
|
+
CLASS_CONSTRAINTS = %i(category account_type)
|
63
|
+
|
64
|
+
def initialize(child_query, original_constraints)
|
65
|
+
constraints = original_constraints.deep_dup
|
66
|
+
|
67
|
+
# Slice up the constraints into types
|
68
|
+
@attribute_constraints = constraints.extract!(*ATTRIBUTE_CONSTRAINTS)
|
69
|
+
@class_constraints = constraints.extract!(*CLASS_CONSTRAINTS)
|
70
|
+
|
71
|
+
# Support category shorthand and check it exists
|
72
|
+
# e.g. "sme/liabilities/payout" instead of Accounting::Accounts::Sme::Liabilities::Payout.name
|
73
|
+
category = @attribute_constraints[:category]
|
74
|
+
if category.present?
|
75
|
+
unless category_name.in?(Twobook::Account.types.map { |t| t.class.category })
|
76
|
+
raise "Invalid category: #{category}"
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
@balance_constraint = constraints.delete(:balance)
|
81
|
+
@tags_constraint = constraints.delete(:tags)
|
82
|
+
@data_constraints = constraints
|
83
|
+
|
84
|
+
@child_query = child_query
|
85
|
+
end
|
86
|
+
|
87
|
+
def none?
|
88
|
+
@child_query.none?
|
89
|
+
end
|
90
|
+
|
91
|
+
def execute(array)
|
92
|
+
@child_query.execute(array).select do |account|
|
93
|
+
next unless matches_attributes(account)
|
94
|
+
next unless matches_class(account)
|
95
|
+
next unless matches_data(account)
|
96
|
+
next unless matches_balance(account)
|
97
|
+
|
98
|
+
next if @tags_constraint && !account.class.tagged?(@tags_constraint)
|
99
|
+
|
100
|
+
true
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
def category
|
105
|
+
@class_constraints[:category]
|
106
|
+
end
|
107
|
+
|
108
|
+
def data
|
109
|
+
@data_constraints
|
110
|
+
end
|
111
|
+
|
112
|
+
def convert_to_name_query
|
113
|
+
if @attribute_constraints[:name].present?
|
114
|
+
NameQuery.new(@attribute_constraints[:name])
|
115
|
+
else
|
116
|
+
account = construct_account
|
117
|
+
NameQuery.new(account.name, account: construct_account)
|
118
|
+
end
|
119
|
+
end
|
120
|
+
|
121
|
+
def construct_account
|
122
|
+
klass = Account.types.detect { |t| t.category == category }
|
123
|
+
raise "Can't find matching class for category #{category}" unless klass.present?
|
124
|
+
klass.new(balance: 0, **data)
|
125
|
+
end
|
126
|
+
|
127
|
+
def inspect
|
128
|
+
"<#{self.class.name} @attribute_constraints=#{@attribute_constraints} " \
|
129
|
+
"@class_constraints=#{@class_constraints} " \
|
130
|
+
"@data_constraints=#{@data_constraints} " \
|
131
|
+
"@tags_constraint=#{@tags_constraint || 'nil'}>"
|
132
|
+
end
|
133
|
+
|
134
|
+
private
|
135
|
+
|
136
|
+
def matches_data(account)
|
137
|
+
account_data = account.data.to_a
|
138
|
+
@data_constraints.none? { |pair| !pair.in?(account_data) }
|
139
|
+
end
|
140
|
+
|
141
|
+
def matches_attributes(account)
|
142
|
+
@attribute_constraints.none? { |k, v| account.public_send(k) != v }
|
143
|
+
end
|
144
|
+
|
145
|
+
def matches_class(account)
|
146
|
+
@class_constraints.none? { |k, v| account.class.public_send(k) != v }
|
147
|
+
end
|
148
|
+
|
149
|
+
def matches_balance(account)
|
150
|
+
return true if @balance_constraint.nil?
|
151
|
+
return account.balance.positive? if @balance_constraint == :positive
|
152
|
+
account.balance == @balance_constraint
|
153
|
+
end
|
154
|
+
end
|
155
|
+
|
156
|
+
class NameQuery < AccountQuery
|
157
|
+
attr_reader :name
|
158
|
+
|
159
|
+
def initialize(name, account: nil)
|
160
|
+
@name = name
|
161
|
+
@account = account
|
162
|
+
end
|
163
|
+
|
164
|
+
def construct_account
|
165
|
+
raise 'Could not construct an account from this name query: no data or category' if @account.nil?
|
166
|
+
@account
|
167
|
+
end
|
168
|
+
|
169
|
+
def execute(accounts)
|
170
|
+
accounts.select { |account| account.name == @name }
|
171
|
+
end
|
172
|
+
end
|
173
|
+
|
174
|
+
class AndQuery < AccountQuery
|
175
|
+
attr_reader :first, :second
|
176
|
+
|
177
|
+
def initialize(first, second)
|
178
|
+
@first = first
|
179
|
+
@second = second
|
180
|
+
end
|
181
|
+
|
182
|
+
def execute(array)
|
183
|
+
@first.execute(array) & @second.execute(array)
|
184
|
+
end
|
185
|
+
|
186
|
+
def none?
|
187
|
+
@first.none? || @second.none?
|
188
|
+
end
|
189
|
+
end
|
190
|
+
|
191
|
+
class OrQuery < AccountQuery
|
192
|
+
attr_reader :first, :second
|
193
|
+
|
194
|
+
def initialize(first, second)
|
195
|
+
@first = first
|
196
|
+
@second = second
|
197
|
+
end
|
198
|
+
|
199
|
+
def none?
|
200
|
+
@first.none? && @second.none?
|
201
|
+
end
|
202
|
+
|
203
|
+
def execute(array)
|
204
|
+
@first.execute(array) | @second.execute(array)
|
205
|
+
end
|
206
|
+
end
|
207
|
+
end
|
208
|
+
end
|
@@ -0,0 +1,79 @@
|
|
1
|
+
module Twobook
|
2
|
+
class Agreement
|
3
|
+
attr_reader :data
|
4
|
+
|
5
|
+
def initialize(data = {})
|
6
|
+
@data = data
|
7
|
+
|
8
|
+
remaining_keys_to_match = self.class.has.deep_dup
|
9
|
+
@data.each do |k, v|
|
10
|
+
raise "Cannot initialize agreement #{inspect}: unexpected parameter #{k}" if remaining_keys_to_match.delete(k).nil?
|
11
|
+
raise "Cannot initialize agreement #{inspect}: #{k} is nil" if v.nil?
|
12
|
+
define_singleton_method k, -> { @data.dig(k) }
|
13
|
+
|
14
|
+
@data[k] = Twobook.wrap_number(v) if v.is_a?(Numeric)
|
15
|
+
end
|
16
|
+
|
17
|
+
if remaining_keys_to_match.any?
|
18
|
+
raise "Cannot initialize agreement #{inspect}: required #{remaining_keys_to_match}"
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def handlers_for(event)
|
23
|
+
handler_names = self.class.handles[event.class.event_name]
|
24
|
+
|
25
|
+
if handler_names.nil? || handler_names.none?
|
26
|
+
raise "Missing handler: #{inspect} cannot handle #{event.class.event_name}"
|
27
|
+
end
|
28
|
+
|
29
|
+
handler_names.map do |handler_name|
|
30
|
+
Handler.from_name(handler_name).new(
|
31
|
+
event: event,
|
32
|
+
**@data,
|
33
|
+
)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
def inspect
|
38
|
+
"<#{self.class.name} @data=#{@data}>"
|
39
|
+
end
|
40
|
+
|
41
|
+
def self.agreement_name
|
42
|
+
name.underscore.gsub("#{Twobook.configuration.accounting_namespace.underscore}/agreements/", '')
|
43
|
+
end
|
44
|
+
|
45
|
+
def self.handles(event_name = nil, with: nil)
|
46
|
+
@handles ||= {}
|
47
|
+
return @handles if event_name.nil?
|
48
|
+
|
49
|
+
if @handles[event_name].present?
|
50
|
+
raise "Duplicate handler: more than one handler defined for #{event_name} on #{name}"
|
51
|
+
end
|
52
|
+
|
53
|
+
# Check that all names map to valid classes
|
54
|
+
Event.from_name(event_name)
|
55
|
+
handler_names = with.is_a?(Array) ? with : [with]
|
56
|
+
handler_names.each { |handler_name| Handler.from_name(handler_name) }
|
57
|
+
|
58
|
+
@handles[event_name] = handler_names
|
59
|
+
end
|
60
|
+
|
61
|
+
def self.has(*args)
|
62
|
+
@has ||= []
|
63
|
+
return @has if args.empty?
|
64
|
+
@has += args
|
65
|
+
end
|
66
|
+
|
67
|
+
def self.from_name(name)
|
68
|
+
match = types.detect do |t|
|
69
|
+
t.name =~ /#{name.camelize}$/
|
70
|
+
end
|
71
|
+
raise "Bad agreement name: #{name}" unless match
|
72
|
+
match
|
73
|
+
end
|
74
|
+
|
75
|
+
def self.types
|
76
|
+
Utilities.types(self)
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
module Twobook
|
2
|
+
class Configuration
|
3
|
+
attr_accessor :accounting_namespace
|
4
|
+
end
|
5
|
+
|
6
|
+
def self.configuration
|
7
|
+
@configuration
|
8
|
+
end
|
9
|
+
|
10
|
+
def self.configure
|
11
|
+
@configuration ||= Configuration.new
|
12
|
+
yield(@configuration)
|
13
|
+
end
|
14
|
+
|
15
|
+
configure do |config|
|
16
|
+
config.accounting_namespace = 'Accounting'
|
17
|
+
end
|
18
|
+
end
|