Skip to content

Commit

Permalink
Add a Base class to all OperationDetails classes to automate the deta…
Browse files Browse the repository at this point in the history
…il_code value setting
  • Loading branch information
RDeckard committed Oct 30, 2024
1 parent 32cd704 commit 9f522c9
Show file tree
Hide file tree
Showing 21 changed files with 68 additions and 48 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ gem 'cfonb'

## Available Operation Details

`OperationDetail` are lines starting with `05`. They aim at providing additional information about the operation.
`OperationDetails` are lines starting with `05`. They aim at providing additional information about the operation.

Find bellow the list of additional details available for each operation.
If you encouter new ones, please open an issue or a pull request with the appropriate implementation.
We aimed at making it as easy as possible to add new details. You just need to do the following on initialization:

```ruby
CFONB::OperationDetail.register('FEE', self)
CFONB::OperationDetails.register('FEE', self)
```

| Detail Code | Attributes | Description |
Expand Down
1 change: 1 addition & 0 deletions lib/cfonb.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
require_relative 'cfonb/line_parser/operation_detail'
require_relative 'cfonb/line_parser/new_balance'

require_relative 'cfonb/operation_detail/base'
require_relative 'cfonb/operation_detail/lib'
require_relative 'cfonb/operation_detail/lcc'
require_relative 'cfonb/operation_detail/lc2'
Expand Down
2 changes: 1 addition & 1 deletion lib/cfonb/line_parser/operation_detail.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

module CFONB
module LineParser
class OperationDetail < Base
class OperationDetails < Base
DICTIONARY = [
['internal_operation_code', (7..10)],
['interbank_operation_code', (32..33)],
Expand Down
2 changes: 1 addition & 1 deletion lib/cfonb/operation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def initialize(line)

def merge_detail(line)
self.raw += "\n#{line.body}"
OperationDetail.for(line)&.apply(self, line)
OperationDetails.for(line)&.apply(self, line)
end

def type_code
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

module CFONB
module OperationDetail
module OperationDetails
@details = {}

def self.register(code, klass)
Expand Down
19 changes: 19 additions & 0 deletions lib/cfonb/operation_details/base.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# frozen_string_literal: true

module CFONB
module OperationDetails
class Base
def self.inherited(base)
base.singleton_class.prepend(
Module.new do
def apply(operation, line)
operation.instance_variable_set(line.detail_code, line.detail)

super
end
end
)
end
end
end
end
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# frozen_string_literal: true

module CFONB
module OperationDetail
class FEE
module OperationDetails
class FEE < Base
ATTRIBUTES = %i[fee fee_currency].freeze

def self.apply(operation, line)
Expand All @@ -12,7 +12,7 @@ def self.apply(operation, line)
operation.fee = BigDecimal(line.detail[4..17]) / (10**scale)
end

CFONB::OperationDetail.register('FEE', self)
CFONB::OperationDetails.register('FEE', self)
end
end
end
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
# frozen_string_literal: true

module CFONB
module OperationDetail
class IBE
module OperationDetails
class IBE < Base
ATTRIBUTES = %i[creditor_identifier creditor_identifier_type].freeze

def self.apply(operation, line)
operation.creditor_identifier = line.detail[0..34].strip
operation.creditor_identifier_type = line.detail[35..-1].strip
end

CFONB::OperationDetail.register('IBE', self)
CFONB::OperationDetails.register('IBE', self)
end
end
end
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
# frozen_string_literal: true

module CFONB
module OperationDetail
class IPY
module OperationDetails
class IPY < Base
ATTRIBUTES = %i[debtor_identifier debtor_identifier_type].freeze

def self.apply(operation, line)
operation.debtor_identifier = line.detail[0..34].strip
operation.debtor_identifier_type = line.detail[35..-1].strip
end

CFONB::OperationDetail.register('IPY', self)
CFONB::OperationDetails.register('IPY', self)
end
end
end
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
# frozen_string_literal: true

module CFONB
module OperationDetail
class LC2
module OperationDetails
class LC2 < Base
def self.apply(operation, line)
operation.label += "\n#{line.detail.strip}"
end

CFONB::OperationDetail.register('LC2', self)
CFONB::OperationDetails.register('LC2', self)
end
end
end
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
# frozen_string_literal: true

module CFONB
module OperationDetail
class LCC
module OperationDetails
class LCC < Base
def self.apply(operation, line)
operation.label += "\n#{line.detail.strip}"
end

CFONB::OperationDetail.register('LCC', self)
CFONB::OperationDetails.register('LCC', self)
end
end
end
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
# frozen_string_literal: true

module CFONB
module OperationDetail
class LCS
module OperationDetails
class LCS < Base
def self.apply(operation, line)
operation.label += "\n#{line.detail[0..35].strip}"
end

CFONB::OperationDetail.register('LCS', self)
CFONB::OperationDetails.register('LCS', self)
end
end
end
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
# frozen_string_literal: true

module CFONB
module OperationDetail
class LIB
module OperationDetails
class LIB < Base
def self.apply(operation, line)
operation.label += "\n#{line.detail.strip}"
end

CFONB::OperationDetail.register('LIB', self)
CFONB::OperationDetails.register('LIB', self)
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
require 'bigdecimal'

module CFONB
module OperationDetail
class MMO
module OperationDetails
class MMO < Base
ATTRIBUTES = %i[original_currency original_amount exchange_rate].freeze

def self.apply(operation, line)
Expand All @@ -22,7 +22,7 @@ def self.apply(operation, line)
operation.exchange_rate = BigDecimal(exchange_rate_value) / (10**BigDecimal(exchange_rate_scale))
end

CFONB::OperationDetail.register('MMO', self)
CFONB::OperationDetails.register('MMO', self)
end
end
end
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
# frozen_string_literal: true

module CFONB
module OperationDetail
class NBE
module OperationDetails
class NBE < Base
ATTRIBUTES = %i[creditor].freeze

def self.apply(operation, line)
operation.creditor = line.detail.strip
end

CFONB::OperationDetail.register('NBE', self)
CFONB::OperationDetails.register('NBE', self)
end
end
end
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
# frozen_string_literal: true

module CFONB
module OperationDetail
class NBU
module OperationDetails
class NBU < Base
ATTRIBUTES = %i[ultimate_creditor].freeze

def self.apply(operation, line)
operation.ultimate_creditor = line.detail.strip
end

CFONB::OperationDetail.register('NBU', self)
CFONB::OperationDetails.register('NBU', self)
end
end
end
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
# frozen_string_literal: true

module CFONB
module OperationDetail
class NPO
module OperationDetails
class NPO < Base
ATTRIBUTES = %i[ultimate_debtor].freeze

def self.apply(operation, line)
operation.ultimate_debtor = line.detail.strip
end

CFONB::OperationDetail.register('NPO', self)
CFONB::OperationDetails.register('NPO', self)
end
end
end
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
# frozen_string_literal: true

module CFONB
module OperationDetail
class NPY
module OperationDetails
class NPY < Base
ATTRIBUTES = %i[debtor].freeze

def self.apply(operation, line)
operation.debtor = line.detail.strip
end

CFONB::OperationDetail.register('NPY', self)
CFONB::OperationDetails.register('NPY', self)
end
end
end
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# frozen_string_literal: true

module CFONB
module OperationDetail
class RCN
module OperationDetails
class RCN < Base
using CFONB::Refinements::Strings

ATTRIBUTES = %i[reference purpose].freeze
Expand All @@ -16,7 +16,7 @@ def self.apply(operation, line)
operation.purpose = line.detail[35..-1]&.strip
end

CFONB::OperationDetail.register('RCN', self)
CFONB::OperationDetails.register('RCN', self)
end
end
end
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# frozen_string_literal: true

module CFONB
module OperationDetail
class REF
module OperationDetails
class REF < Base
using CFONB::Refinements::Strings

ATTRIBUTES = %i[reference].freeze
Expand All @@ -14,7 +14,7 @@ def self.apply(operation, line)
].filter_map(&:presence).join(' - ')
end

CFONB::OperationDetail.register('REF', self)
CFONB::OperationDetails.register('REF', self)
end
end
end
2 changes: 1 addition & 1 deletion spec/cfonb/line_parser/operation_detail_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

require 'cfonb'

describe CFONB::LineParser::OperationDetail do
describe CFONB::LineParser::OperationDetails do
describe '.initialize' do
subject(:line) { described_class.new(input) }

Expand Down

0 comments on commit 9f522c9

Please sign in to comment.