Skip to content

Commit

Permalink
Remove default max length values
Browse files Browse the repository at this point in the history
  • Loading branch information
orien committed Oct 3, 2018
1 parent fcc78d6 commit b224237
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 50 deletions.
17 changes: 4 additions & 13 deletions lib/double_entry/account.rb
Expand Up @@ -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?))
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
10 changes: 3 additions & 7 deletions lib/double_entry/transfer.rb
Expand Up @@ -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?
Expand Down Expand Up @@ -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
Expand Down
42 changes: 26 additions & 16 deletions spec/double_entry/account_spec.rb
Expand Up @@ -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
Expand All @@ -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
Expand Down
6 changes: 3 additions & 3 deletions spec/double_entry/configuration_spec.rb
Expand Up @@ -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
Expand Down
27 changes: 16 additions & 11 deletions spec/double_entry/transfer_spec.rb
Expand Up @@ -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
Expand Down

0 comments on commit b224237

Please sign in to comment.