-
Notifications
You must be signed in to change notification settings - Fork 68
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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.
- Loading branch information
Showing
5 changed files
with
42 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# encoding: utf-8 | ||
module DoubleEntry | ||
module 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
Money.locale_backend = :i18n | ||
require 'money' | ||
::Money.locale_backend = :i18n |