Skip to content

Commit

Permalink
Add configurable support for using a money adapter
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jeremycw committed May 24, 2022
1 parent b05730a commit a710673
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/double_entry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
require 'active_record/locking_extensions'
require 'active_record/locking_extensions/log_subscriber'
require 'active_support/all'
require 'money'
require 'rails/railtie'

require 'double_entry/version'
Expand All @@ -16,6 +15,7 @@
require 'double_entry/locking'
require 'double_entry/transfer'
require 'double_entry/validation'
require 'double_entry/money'

# Keep track of all the monies!
#
Expand Down
8 changes: 8 additions & 0 deletions lib/double_entry/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,19 @@ module DoubleEntry

class Configuration
attr_accessor :json_metadata
attr_writer :money_adapter

def initialize
@json_metadata = false
end

def money_adapter
@money_adapter ||= begin
require 'money'
::Money
end
end

delegate(
:accounts,
:accounts=,
Expand Down
15 changes: 15 additions & 0 deletions lib/double_entry/money.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# encoding: utf-8
module DoubleEntry
class Money
class << self
attr_writer :adapter

def adapter
@adapter ||= DoubleEntry.config.money_adapter
end

delegate(:new, to: :adapter)
delegate_missing_to(:adapter)
end
end
end
16 changes: 16 additions & 0 deletions spec/double_entry/money_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
RSpec.describe DoubleEntry::Money do
it 'delegates singleton methods to the adapter' do
DoubleEntry::Money.adapter = Class.new do
def self.zero
0
end

def test
12345
end
end
expect(DoubleEntry::Money.new.test).to eq(12345)
expect(DoubleEntry::Money.zero).to eq(0)
DoubleEntry::Money.adapter = ::Money
end
end
3 changes: 2 additions & 1 deletion spec/support/money.rb
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
Money.locale_backend = :i18n
require 'money'
::Money.locale_backend = :i18n

0 comments on commit a710673

Please sign in to comment.