diff --git a/lib/double_entry/account.rb b/lib/double_entry/account.rb index 37bc270..8eb6617 100644 --- a/lib/double_entry/account.rb +++ b/lib/double_entry/account.rb @@ -4,23 +4,14 @@ module DoubleEntry class Account class << self - attr_writer :accounts, :scope_identifier_max_length, :account_identifier_max_length + attr_accessor :scope_identifier_max_length, :account_identifier_max_length + attr_writer :accounts # @api private def accounts @accounts ||= Set.new end - # @api private - def scope_identifier_max_length - @scope_identifier_max_length ||= 23 - end - - # @api private - def account_identifier_max_length - @account_identifier_max_length ||= 31 - end - # @api private def account(identifier, options = {}) account = accounts.find(identifier, (options[:scope].present? || options[:scope_identity].present?)) @@ -160,7 +151,7 @@ def inspect def ensure_scope_is_valid identity = scope_identity - if identity && identity.length > Account.scope_identifier_max_length + if identity && Account.scope_identifier_max_length && identity.length > Account.scope_identifier_max_length fail ScopeIdentifierTooLongError, "scope identifier '#{identity}' is too long. Please limit it to #{Account.scope_identifier_max_length} characters." end @@ -175,7 +166,7 @@ def initialize(args) @positive_only = args[:positive_only] @negative_only = args[:negative_only] @currency = args[:currency] || Money.default_currency - if identifier.length > Account.account_identifier_max_length + if Account.account_identifier_max_length && identifier.length > Account.account_identifier_max_length fail AccountIdentifierTooLongError, "account identifier '#{identifier}' is too long. Please limit it to #{Account.account_identifier_max_length} characters." end diff --git a/lib/double_entry/transfer.rb b/lib/double_entry/transfer.rb index 1cd8736..a410dee 100644 --- a/lib/double_entry/transfer.rb +++ b/lib/double_entry/transfer.rb @@ -4,18 +4,14 @@ module DoubleEntry class Transfer class << self - attr_writer :transfers, :code_max_length + attr_accessor :code_max_length + attr_writer :transfers # @api private def transfers @transfers ||= Set.new end - # @api private - def code_max_length - @code_max_length ||= 47 - end - # @api private def transfer(amount, options = {}) fail TransferIsNegative if amount.negative? @@ -73,7 +69,7 @@ def initialize(attributes) @code = attributes[:code] @from = attributes[:from] @to = attributes[:to] - if code.length > Transfer.code_max_length + if Transfer.code_max_length && code.length > Transfer.code_max_length fail TransferCodeTooLongError, "transfer code '#{code}' is too long. Please limit it to #{Transfer.code_max_length} characters." end diff --git a/spec/double_entry/account_spec.rb b/spec/double_entry/account_spec.rb index 8270814..a2c82c6 100644 --- a/spec/double_entry/account_spec.rb +++ b/spec/double_entry/account_spec.rb @@ -4,17 +4,22 @@ module DoubleEntry let(:identity_scope) { ->(value) { value } } describe '::new' do - context 'given an identifier 31 characters in length' do - let(:identifier) { 'xxxxxxxx 31 characters xxxxxxxx' } - specify do - expect { Account.new(:identifier => identifier) }.to_not raise_error + context 'given a account_identifier_max_length of 31' do + before { Account.account_identifier_max_length = 31 } + after { Account.account_identifier_max_length = nil } + + context 'given an identifier 31 characters in length' do + let(:identifier) { 'xxxxxxxx 31 characters xxxxxxxx' } + specify do + expect { Account.new(:identifier => identifier) }.to_not raise_error + end end - end - context 'given an identifier 32 characters in length' do - let(:identifier) { 'xxxxxxxx 32 characters xxxxxxxxx' } - specify do - expect { Account.new(:identifier => identifier) }.to raise_error AccountIdentifierTooLongError, /'#{identifier}'/ + context 'given an identifier 32 characters in length' do + let(:identifier) { 'xxxxxxxx 32 characters xxxxxxxxx' } + specify do + expect { Account.new(:identifier => identifier) }.to raise_error AccountIdentifierTooLongError, /'#{identifier}'/ + end end end end @@ -41,14 +46,19 @@ module DoubleEntry let(:account) { Account.new(:identifier => 'x', :scope_identifier => identity_scope) } subject(:initialize_account_instance) { Account::Instance.new(:account => account, :scope => scope) } - context 'given a scope identifier 23 characters in length' do - let(:scope) { 'xxxx 23 characters xxxx' } - specify { expect { initialize_account_instance }.to_not raise_error } - end + context 'given a scope_identifier_max_length of 23' do + before { Account.scope_identifier_max_length = 23 } + after { Account.scope_identifier_max_length = nil } + + context 'given a scope identifier 23 characters in length' do + let(:scope) { 'xxxx 23 characters xxxx' } + specify { expect { initialize_account_instance }.to_not raise_error } + end - context 'given a scope identifier 24 characters in length' do - let(:scope) { 'xxxx 24 characters xxxxx' } - specify { expect { initialize_account_instance }.to raise_error ScopeIdentifierTooLongError, /'#{scope}'/ } + context 'given a scope identifier 24 characters in length' do + let(:scope) { 'xxxx 24 characters xxxxx' } + specify { expect { initialize_account_instance }.to raise_error ScopeIdentifierTooLongError, /'#{scope}'/ } + end end end end diff --git a/spec/double_entry/configuration_spec.rb b/spec/double_entry/configuration_spec.rb index a1374bd..d3fe7d1 100644 --- a/spec/double_entry/configuration_spec.rb +++ b/spec/double_entry/configuration_spec.rb @@ -5,9 +5,9 @@ describe 'max lengths' do context 'given a max length has not been set' do - its(:code_max_length) { should be 47 } - its(:scope_identifier_max_length) { should be 23 } - its(:account_identifier_max_length) { should be 31 } + its(:code_max_length) { should be_nil } + its(:scope_identifier_max_length) { should be_nil } + its(:account_identifier_max_length) { should be_nil } end context 'given a code max length of 10 has been set' do diff --git a/spec/double_entry/transfer_spec.rb b/spec/double_entry/transfer_spec.rb index 9350aba..b2384a3 100644 --- a/spec/double_entry/transfer_spec.rb +++ b/spec/double_entry/transfer_spec.rb @@ -2,17 +2,22 @@ module DoubleEntry RSpec.describe Transfer do describe '::new' do - context 'given a code 47 characters in length' do - let(:code) { 'xxxxxxxxxxxxxxxx 47 characters xxxxxxxxxxxxxxxx' } - specify do - expect { Transfer.new(:code => code) }.to_not raise_error - end - end - - context 'given a code 48 characters in length' do - let(:code) { 'xxxxxxxxxxxxxxxx 48 characters xxxxxxxxxxxxxxxxx' } - specify do - expect { Transfer.new(:code => code) }.to raise_error TransferCodeTooLongError, /'#{code}'/ + context 'given a code_max_length of 47' do + before { Transfer.code_max_length = 47 } + after { Transfer.code_max_length = nil } + + context 'given a code 47 characters in length' do + let(:code) { 'xxxxxxxxxxxxxxxx 47 characters xxxxxxxxxxxxxxxx' } + specify do + expect { Transfer.new(:code => code) }.to_not raise_error + end + end + + context 'given a code 48 characters in length' do + let(:code) { 'xxxxxxxxxxxxxxxx 48 characters xxxxxxxxxxxxxxxxx' } + specify do + expect { Transfer.new(:code => code) }.to raise_error TransferCodeTooLongError, /'#{code}'/ + end end end end