Skip to content

Commit f81f524

Browse files
committed
Add configurable support for using a money adapter
Unfortunately `shopify-money` and `money` do not play well together. Both gems provide a `Money` class and an entry point into the gem at `lib/money.rb`. This makes it pretty difficult to include double_entry in a codebase that makes use of the `shopify-money` gem. To work around this I've added a configuration option `money_adapter` that allows double_entry to be configured to internally delegate methods to this adapter instead of using the `money` gem directly. This allows users of double_entry to provide an adapter class that allows integration with arbitrary money backends for better interoperability with their system. This was done by adding a `DoubleEntry::Money` class that delegates its singleton methods to the adapter.
1 parent b05730a commit f81f524

File tree

5 files changed

+42
-2
lines changed

5 files changed

+42
-2
lines changed

lib/double_entry.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
require 'active_record/locking_extensions'
44
require 'active_record/locking_extensions/log_subscriber'
55
require 'active_support/all'
6-
require 'money'
76
require 'rails/railtie'
87

98
require 'double_entry/version'
@@ -16,6 +15,7 @@
1615
require 'double_entry/locking'
1716
require 'double_entry/transfer'
1817
require 'double_entry/validation'
18+
require 'double_entry/money'
1919

2020
# Keep track of all the monies!
2121
#

lib/double_entry/configuration.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,19 @@ module DoubleEntry
44

55
class Configuration
66
attr_accessor :json_metadata
7+
attr_writer :money_adapter
78

89
def initialize
910
@json_metadata = false
1011
end
1112

13+
def money_adapter
14+
@money_adapter ||= begin
15+
require 'money'
16+
::Money
17+
end
18+
end
19+
1220
delegate(
1321
:accounts,
1422
:accounts=,

lib/double_entry/money.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# encoding: utf-8
2+
module DoubleEntry
3+
module Money
4+
class << self
5+
attr_writer :adapter
6+
7+
def adapter
8+
@adapter ||= DoubleEntry.config.money_adapter
9+
end
10+
11+
delegate(:new, to: :adapter)
12+
delegate_missing_to(:adapter)
13+
end
14+
end
15+
end

spec/double_entry/money_spec.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
RSpec.describe DoubleEntry::Money do
2+
it 'delegates singleton methods to the adapter' do
3+
DoubleEntry::Money.adapter = Class.new do
4+
def self.zero
5+
0
6+
end
7+
8+
def test
9+
12345
10+
end
11+
end
12+
expect(DoubleEntry::Money.new.test).to eq(12345)
13+
expect(DoubleEntry::Money.zero).to eq(0)
14+
DoubleEntry::Money.adapter = ::Money
15+
end
16+
end

spec/support/money.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
Money.locale_backend = :i18n
1+
require 'money'
2+
::Money.locale_backend = :i18n

0 commit comments

Comments
 (0)