diff --git a/lib/double_entry.rb b/lib/double_entry.rb index bdabbc84..85cf0532 100644 --- a/lib/double_entry.rb +++ b/lib/double_entry.rb @@ -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' @@ -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! # diff --git a/lib/double_entry/configuration.rb b/lib/double_entry/configuration.rb index 9d361dac..7e60249f 100644 --- a/lib/double_entry/configuration.rb +++ b/lib/double_entry/configuration.rb @@ -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=, diff --git a/lib/double_entry/money.rb b/lib/double_entry/money.rb new file mode 100644 index 00000000..db9dc717 --- /dev/null +++ b/lib/double_entry/money.rb @@ -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 diff --git a/spec/double_entry/money_spec.rb b/spec/double_entry/money_spec.rb new file mode 100644 index 00000000..7c5eab49 --- /dev/null +++ b/spec/double_entry/money_spec.rb @@ -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 diff --git a/spec/support/money.rb b/spec/support/money.rb index 452a5f50..4765ec87 100644 --- a/spec/support/money.rb +++ b/spec/support/money.rb @@ -1 +1,2 @@ -Money.locale_backend = :i18n +require 'money' +::Money.locale_backend = :i18n