Skip to content

Commit

Permalink
Ensure duplicated code details persistence on operation details
Browse files Browse the repository at this point in the history
  • Loading branch information
joanabertoldi committed Nov 28, 2024
1 parent b6fef0c commit c44da38
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 8 deletions.
1 change: 1 addition & 0 deletions lib/cfonb.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
require 'date'

require_relative 'cfonb/refinements/strings'
require_relative 'cfonb/refinements/arrays'

require_relative 'cfonb/error'
require_relative 'cfonb/parser'
Expand Down
33 changes: 32 additions & 1 deletion lib/cfonb/operation_details/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,48 @@
module CFONB
module OperationDetails
class Base
using CFONB::Refinements::Arrays

def self.inherited(base)
base.singleton_class.prepend(
Module.new do
def apply(details, line)
details.instance_variable_set(:"@#{line.detail_code}", line.detail)
return unless line.respond_to?(:detail)

current_details = attributes.map { |attribute| [attribute, details.send(attribute)] }.to_h
current_instance = details.instance_variable_get(:"@#{line.detail_code}")
new_instance = merge_details(current_instance, line.detail)

details.instance_variable_set(:"@#{line.detail_code}", new_instance)

super

include_duplicated_code_details(current_details, details)
end

def merge_details(existing_detail, new_detail)
[existing_detail, new_detail].join_sanitized
end

private

def attributes
const_get(:ATTRIBUTES)
end

def include_duplicated_code_details(existing_details, details)
attributes.each do |attribute|
details.send(
"#{attribute}=",
merge_details(existing_details[attribute], details.send(attribute)),
)
end
end
end,
)
end
end
end
end

# return if attributes.include?(:unknown)
2 changes: 1 addition & 1 deletion lib/cfonb/operation_details/lib.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class LIB < Base
ATTRIBUTES = %i[free_label].freeze

def self.apply(details, line)
details.free_label = [details.free_label, line.detail.strip].compact.join("\n")
details.free_label = line.detail
end

CFONB::OperationDetails.register('LIB', self)
Expand Down
15 changes: 15 additions & 0 deletions lib/cfonb/refinements/arrays.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# frozen_string_literal: true

module CFONB
module Refinements
module Arrays
refine Array do
def join_sanitized
return last unless last.is_a?(String)

compact.map(&:strip).join("\n")
end
end
end
end
end
12 changes: 6 additions & 6 deletions spec/cfonb/parser_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
reference: '',
)
expect(statements[0].operations[0].details).to have_attributes(
free_label: 'MENSUEAUHTR13133',
free_label: "MENSUEAUHTR13133\nMENSUEAUHTR13DUP",
original_currency: nil,
original_amount: nil,
exchange_rate: nil,
Expand Down Expand Up @@ -272,7 +272,7 @@

expect(statements[0].operations[0].details).to have_attributes(
operation_reference: 'REFERENCE',
free_label: 'MENSUEAUHTR13133',
free_label: "MENSUEAUHTR13133\nMENSUEAUHTR13DUP",
debtor: 'INTERNET SFR',
client_reference: 'OTHER REFERENCE',
original_currency: nil,
Expand Down Expand Up @@ -684,10 +684,10 @@
end
end

context 'with an unhandled line code' do
context 'with a duplicated line code' do
let(:input) { File.read('spec/files/example.txt') }

it 'ignores the unhandled lines' do
it 'concatenates the duplicated line' do
expect(operation).to have_attributes(
amount: -32.21,
currency: 'EUR',
Expand All @@ -705,8 +705,8 @@
expect(operation.details).to have_attributes(
operation_reference: 'REFERENCE',
client_reference: 'OTHER REFERENCE',
free_label: "MENSUEAUHTR13133\nP051928612 22793301700040",
debtor: 'ELEC ERDF',
free_label: "MENSUEAUHTR13133\nMENSUEAUHTR13DUP\nP051928612 22793301700040",
debtor: "INTERNET SFR\nELEC ERDF",
original_currency: nil,
original_amount: nil,
exchange_rate: nil,
Expand Down
1 change: 1 addition & 0 deletions spec/files/example.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

0415589916200000EUR2 98765432100B1160519 160519PRLV SEPA TEST CABINET 0000000000000000000322J
0515589916200000EUR2 98765432100B1160519 LIBMENSUEAUHTR13133
0515589916200000EUR2 98765432100B1160519 LIBMENSUEAUHTR13DUP
0515589916200000EUR2 98765432100B1160519 REFREFERENCE
0515589916200000EUR2 98765432100B1160519 RCNOTHER REFERENCE PURPOSE
0515589916200000EUR2 98765432100B1160519 NPYINTERNET SFR
Expand Down

0 comments on commit c44da38

Please sign in to comment.