”Use“ and some metaprograming to refactor behaviours implimentation

This commit is contained in:
Danil Negrienko 2024-03-07 18:33:38 -05:00
parent 368225f331
commit 1a9d6758fd
23 changed files with 106 additions and 438 deletions

View File

@ -3,32 +3,8 @@ defmodule IbanEx.Country.AT do
Austria IBAN parsing rules
"""
alias IbanEx.Iban
@behaviour IbanEx.Country.Template
@size 20
@rule ~r/^(?<bank_code>[0-9]{5})(?<account_number>[0-9]{11})$/i
@spec size() :: 20
def size(), do: @size
@spec rule() :: Regex.t()
def rule(), do: @rule
@spec to_s(Iban.t()) :: binary()
@spec to_s(Iban.t(), binary()) :: binary()
def to_s(
%Iban{
country_code: country_code,
check_digits: check_digits,
bank_code: bank_code,
account_number: account_number
} = _iban,
joiner \\ " "
) do
[country_code, check_digits, bank_code, account_number]
|> Enum.join(joiner)
end
use IbanEx.Country.Template
end

View File

@ -3,23 +3,15 @@ defmodule IbanEx.Country.BE do
Belgium IBAN parsing rules
"""
alias IbanEx.Iban
@behaviour IbanEx.Country.Template
@size 16
@rule ~r/^(?<bank_code>[0-9]{3})(?<account_number>[0-9]{7})(?<national_check>[0-9]{2})$/i
@spec size() :: 16
def size(), do: @size
use IbanEx.Country.Template
@spec rule() :: Regex.t()
def rule(), do: @rule
@spec to_s(Iban.t()) :: binary()
@spec to_s(Iban.t(), binary()) :: binary()
def to_s(
@impl IbanEx.Country.Template
@spec to_string(Iban.t()) :: binary()
@spec to_string(Iban.t(), binary()) :: binary()
def to_string(
%Iban{
country_code: country_code,
check_digits: check_digits,

View File

@ -3,23 +3,17 @@ defmodule IbanEx.Country.BG do
Bulgaria IBAN parsing rules
"""
alias IbanEx.Iban
@behaviour IbanEx.Country.Template
@size 22
@rule ~r/^(?<bank_code>[A-Z]{4})(?<branch_code>[0-9]{4})(?<account_number>[0-9]{2}[0-9A-Z]{8})$/i
use IbanEx.Country.Template
@spec size() :: 22
def size(), do: @size
@spec rule() :: Regex.t()
def rule(), do: @rule
@spec to_s(Iban.t()) :: binary()
@spec to_s(Iban.t(), binary()) :: binary()
def to_s(
@impl IbanEx.Country.Template
@spec to_string(Iban.t()) :: binary()
@spec to_string(Iban.t(), binary()) :: binary()
def to_string(
%Iban{
country_code: country_code,
check_digits: check_digits,

View File

@ -3,32 +3,8 @@ defmodule IbanEx.Country.CH do
Switzerland IBAN parsing rules
"""
alias IbanEx.Iban
@behaviour IbanEx.Country.Template
@size 21
@rule ~r/^(?<bank_code>[0-9]{5})(?<account_number>[0-9A-Z]{12})$/i
@spec size() :: 21
def size(), do: @size
@spec rule() :: Regex.t()
def rule(), do: @rule
@spec to_s(Iban.t()) :: binary()
@spec to_s(Iban.t(), binary()) :: binary()
def to_s(
%Iban{
country_code: country_code,
check_digits: check_digits,
bank_code: bank_code,
account_number: account_number
} = _iban,
joiner \\ " "
) do
[country_code, check_digits, bank_code, account_number]
|> Enum.join(joiner)
end
use IbanEx.Country.Template
end

View File

@ -3,23 +3,15 @@ defmodule IbanEx.Country.CY do
Cyprus IBAN parsing rules
"""
alias IbanEx.Iban
@behaviour IbanEx.Country.Template
@size 28
@rule ~r/^(?<bank_code>[0-9]{3})(?<branch_code>[0-9]{5})(?<account_number>[0-9A-Z]{16})$/i
@spec size() :: 28
def size(), do: @size
use IbanEx.Country.Template
@spec rule() :: Regex.t()
def rule(), do: @rule
@spec to_s(Iban.t()) :: binary()
@spec to_s(Iban.t(), binary()) :: binary()
def to_s(
@impl IbanEx.Country.Template
@spec to_string(Iban.t()) :: binary()
@spec to_string(Iban.t(), binary()) :: binary()
def to_string(
%Iban{
country_code: country_code,
check_digits: check_digits,

View File

@ -3,34 +3,8 @@ defmodule IbanEx.Country.CZ do
Czech Republic IBAN parsing rules
"""
alias IbanEx.Iban
@behaviour IbanEx.Country.Template
@size 24
@rule ~r/^(?<bank_code>[0-9]{4})(?<account_number>[0-9]{16})$/i
@spec size() :: 24
def size(), do: @size
@spec rule() :: Regex.t()
def rule(), do: @rule
@spec to_s(Iban.t()) :: binary()
@spec to_s(Iban.t(), binary()) :: binary()
def to_s(
%Iban{
country_code: country_code,
check_digits: check_digits,
bank_code: bank_code,
branch_code: _branch_code,
national_check: _national_check,
account_number: account_number
} = _iban,
joiner \\ " "
) do
[country_code, check_digits, bank_code, account_number]
|> Enum.join(joiner)
end
use IbanEx.Country.Template
end

View File

@ -3,34 +3,8 @@ defmodule IbanEx.Country.DE do
Germany IBAN parsing rules
"""
alias IbanEx.Iban
@behaviour IbanEx.Country.Template
@size 22
@rule ~r/^(?<bank_code>[0-9]{8})(?<account_number>[0-9]{10})$/i
@spec size() :: 22
def size(), do: @size
@spec rule() :: Regex.t()
def rule(), do: @rule
@spec to_s(Iban.t()) :: binary()
@spec to_s(Iban.t(), binary()) :: binary()
def to_s(
%Iban{
country_code: country_code,
check_digits: check_digits,
bank_code: bank_code,
branch_code: _branch_code,
national_check: _national_check,
account_number: account_number
} = _iban,
joiner \\ " "
) do
[country_code, check_digits, bank_code, account_number]
|> Enum.join(joiner)
end
use IbanEx.Country.Template
end

View File

@ -2,35 +2,8 @@ defmodule IbanEx.Country.DK do
@moduledoc """
Denmark IBAN parsing rules
"""
alias IbanEx.Iban
@behaviour IbanEx.Country.Template
@size 18
@rule ~r/^(?<bank_code>[0-9]{4})(?<account_number>[0-9]{10})$/i
@spec size() :: 18
def size(), do: @size
@spec rule() :: Regex.t()
def rule(), do: @rule
@spec to_s(Iban.t()) :: binary()
@spec to_s(Iban.t(), binary()) :: binary()
def to_s(
%Iban{
country_code: country_code,
check_digits: check_digits,
bank_code: bank_code,
branch_code: _branch_code,
national_check: _national_check,
account_number: account_number
} = _iban,
joiner \\ " "
) do
[country_code, check_digits, bank_code, account_number]
|> Enum.join(joiner)
end
use IbanEx.Country.Template
end

View File

@ -3,23 +3,15 @@ defmodule IbanEx.Country.EE do
Estonian IBAN parsing rules
"""
alias IbanEx.Iban
@behaviour IbanEx.Country.Template
@size 20
@rule ~r/^(?<bank_code>[0-9]{2})(?<branch_code>[0-9]{2})(?<account_number>[0-9]{11})(?<national_check>[0-9]{1})$/i
@spec size() :: 20
def size(), do: @size
use IbanEx.Country.Template
@spec rule() :: Regex.t()
def rule(), do: @rule
@spec to_s(Iban.t()) :: binary()
@spec to_s(Iban.t(), binary()) :: binary()
def to_s(
@impl IbanEx.Country.Template
@spec to_string(Iban.t()) :: binary()
@spec to_string(Iban.t(), binary()) :: binary()
def to_string(
%Iban{
country_code: country_code,
check_digits: check_digits,

View File

@ -3,23 +3,15 @@ defmodule IbanEx.Country.ES do
Spain IBAN parsing rules
"""
alias IbanEx.Iban
@behaviour IbanEx.Country.Template
@size 24
@rule ~r/^(?<bank_code>[0-9]{4})(?<branch_code>[0-9]{4})(?<national_check>[0-9]{2})(?<account_number>[0-9]{10})$/i
@spec size() :: 24
def size(), do: @size
use IbanEx.Country.Template
@spec rule() :: Regex.t()
def rule(), do: @rule
@spec to_s(Iban.t()) :: binary()
@spec to_s(Iban.t(), binary()) :: binary()
def to_s(
@impl IbanEx.Country.Template
@spec to_string(Iban.t()) :: binary()
@spec to_string(Iban.t(), binary()) :: binary()
def to_string(
%Iban{
country_code: country_code,
check_digits: check_digits,

View File

@ -3,23 +3,15 @@ defmodule IbanEx.Country.FI do
Finland IBAN parsing rules
"""
alias IbanEx.Iban
@behaviour IbanEx.Country.Template
@size 18
@rule ~r/^(?<bank_code>[0-9]{6})(?<account_number>[0-9]{7})(?<national_check>[0-9]{1})$/i
@spec size() :: 18
def size(), do: @size
use IbanEx.Country.Template
@spec rule() :: Regex.t()
def rule(), do: @rule
@spec to_s(Iban.t()) :: binary()
@spec to_s(Iban.t(), binary()) :: binary()
def to_s(
@impl IbanEx.Country.Template
@spec to_string(Iban.t()) :: binary()
@spec to_string(Iban.t(), binary()) :: binary()
def to_string(
%Iban{
country_code: country_code,
check_digits: check_digits,

View File

@ -3,23 +3,15 @@ defmodule IbanEx.Country.FR do
France IBAN parsing rules
"""
alias IbanEx.Iban
@behaviour IbanEx.Country.Template
@size 27
@rule ~r/^(?<bank_code>[0-9]{5})(?<branch_code>[0-9]{5})(?<account_number>[0-9A-Z]{11})(?<national_check>[0-9]{2})$/i
@spec size() :: 27
def size(), do: @size
use IbanEx.Country.Template
@spec rule() :: Regex.t()
def rule(), do: @rule
@spec to_s(Iban.t()) :: binary()
@spec to_s(Iban.t(), binary()) :: binary()
def to_s(
@impl IbanEx.Country.Template
@spec to_string(Iban.t()) :: binary()
@spec to_string(Iban.t(), binary()) :: binary()
def to_string(
%Iban{
country_code: country_code,
check_digits: check_digits,

View File

@ -3,23 +3,15 @@ defmodule IbanEx.Country.GB do
United Kingdom IBAN parsing rules
"""
alias IbanEx.Iban
@behaviour IbanEx.Country.Template
@size 22
@rule ~r/^(?<bank_code>[A-Z]{4})(?<branch_code>[0-9]{6})(?<account_number>[0-9]{8})$/i
@spec size() :: 22
def size(), do: @size
use IbanEx.Country.Template
@spec rule() :: Regex.t()
def rule(), do: @rule
@spec to_s(Iban.t()) :: binary()
@spec to_s(Iban.t(), binary()) :: binary()
def to_s(
@impl IbanEx.Country.Template
@spec to_string(Iban.t()) :: binary()
@spec to_string(Iban.t(), binary()) :: binary()
def to_string(
%Iban{
country_code: country_code,
check_digits: check_digits,

View File

@ -3,34 +3,8 @@ defmodule IbanEx.Country.HR do
Croatia IBAN parsing rules
"""
alias IbanEx.Iban
@behaviour IbanEx.Country.Template
@size 21
@rule ~r/^(?<bank_code>[0-9]{7})(?<account_number>[0-9]{10})$/i
@spec size() :: 21
def size(), do: @size
@spec rule() :: Regex.t()
def rule(), do: @rule
@spec to_s(Iban.t()) :: binary()
@spec to_s(Iban.t(), binary()) :: binary()
def to_s(
%Iban{
country_code: country_code,
check_digits: check_digits,
bank_code: bank_code,
branch_code: _branch_code,
national_check: _national_check,
account_number: account_number
} = _iban,
joiner \\ " "
) do
[country_code, check_digits, bank_code, account_number]
|> Enum.join(joiner)
end
use IbanEx.Country.Template
end

View File

@ -3,34 +3,8 @@ defmodule IbanEx.Country.LT do
Lithuanian IBAN parsing rules
"""
alias IbanEx.Iban
@behaviour IbanEx.Country.Template
@size 20
@rule ~r/^(?<bank_code>[0-9]{5})(?<account_number>[0-9]{11})$/i
@spec size() :: 20
def size(), do: @size
@spec rule() :: Regex.t()
def rule(), do: @rule
@spec to_s(Iban.t()) :: binary()
@spec to_s(Iban.t(), binary()) :: binary()
def to_s(
%Iban{
country_code: country_code,
check_digits: check_digits,
bank_code: bank_code,
branch_code: _branch_code,
national_check: _national_check,
account_number: account_number
} = _iban,
joiner \\ " "
) do
[country_code, check_digits, bank_code, account_number]
|> Enum.join(joiner)
end
use IbanEx.Country.Template
end

View File

@ -3,34 +3,8 @@ defmodule IbanEx.Country.LU do
Luxembourg IBAN parsing rules
"""
alias IbanEx.Iban
@behaviour IbanEx.Country.Template
@size 20
@rule ~r/^(?<bank_code>[0-9]{3})(?<account_number>[0-9A-Z]{13})$/i
@spec size() :: 20
def size(), do: @size
@spec rule() :: Regex.t()
def rule(), do: @rule
@spec to_s(Iban.t()) :: binary()
@spec to_s(Iban.t(), binary()) :: binary()
def to_s(
%Iban{
country_code: country_code,
check_digits: check_digits,
bank_code: bank_code,
branch_code: _branch_code,
national_check: _national_check,
account_number: account_number
} = _iban,
joiner \\ " "
) do
[country_code, check_digits, bank_code, account_number]
|> Enum.join(joiner)
end
use IbanEx.Country.Template
end

View File

@ -3,34 +3,8 @@ defmodule IbanEx.Country.LV do
Latvian IBAN parsing rules
"""
alias IbanEx.Iban
@behaviour IbanEx.Country.Template
@size 21
@rule ~r/^(?<bank_code>[A-Z]{4})(?<account_number>[0-9A-Z]{13})$/i
@spec size() :: 21
def size(), do: @size
@spec rule() :: Regex.t()
def rule(), do: @rule
@spec to_s(Iban.t()) :: binary()
@spec to_s(Iban.t(), binary()) :: binary()
def to_s(
%Iban{
country_code: country_code,
check_digits: check_digits,
bank_code: bank_code,
branch_code: _branch_code,
national_check: _national_check,
account_number: account_number
} = _iban,
joiner \\ " "
) do
[country_code, check_digits, bank_code, account_number]
|> Enum.join(joiner)
end
use IbanEx.Country.Template
end

View File

@ -3,23 +3,15 @@ defmodule IbanEx.Country.MT do
Malta IBAN parsing rules
"""
alias IbanEx.Iban
@behaviour IbanEx.Country.Template
@size 31
@rule ~r/^(?<bank_code>[A-Z]{4})(?<branch_code>[0-9]{5})(?<account_number>[0-9A-Z]{18})$/i
@spec size() :: 31
def size(), do: @size
use IbanEx.Country.Template
@spec rule() :: Regex.t()
def rule(), do: @rule
@spec to_s(Iban.t()) :: binary()
@spec to_s(Iban.t(), binary()) :: binary()
def to_s(
@impl IbanEx.Country.Template
@spec to_string(Iban.t()) :: binary()
@spec to_string(Iban.t(), binary()) :: binary()
def to_string(
%Iban{
country_code: country_code,
check_digits: check_digits,

View File

@ -3,34 +3,8 @@ defmodule IbanEx.Country.NL do
Netherlands IBAN parsing rules
"""
alias IbanEx.Iban
@behaviour IbanEx.Country.Template
@size 18
@rule ~r/^(?<bank_code>[A-Z]{4})(?<account_number>[0-9]{10})$/i
@spec size() :: 18
def size(), do: @size
@spec rule() :: Regex.t()
def rule(), do: @rule
@spec to_s(Iban.t()) :: binary()
@spec to_s(Iban.t(), binary()) :: binary()
def to_s(
%Iban{
country_code: country_code,
check_digits: check_digits,
bank_code: bank_code,
branch_code: _branch_code,
national_check: _national_check,
account_number: account_number
} = _iban,
joiner \\ " "
) do
[country_code, check_digits, bank_code, account_number]
|> Enum.join(joiner)
end
use IbanEx.Country.Template
end

View File

@ -3,23 +3,15 @@ defmodule IbanEx.Country.PL do
Poland IBAN parsing rules
"""
alias IbanEx.Iban
@behaviour IbanEx.Country.Template
@size 28
@rule ~r/^(?<bank_code>[0-9]{3})(?<branch_code>[0-9]{4})(?<national_check>[0-9]{1})(?<account_number>[0-9]{16})$/i
@spec size() :: 28
def size(), do: @size
use IbanEx.Country.Template
@spec rule() :: Regex.t()
def rule(), do: @rule
@spec to_s(Iban.t()) :: binary()
@spec to_s(Iban.t(), binary()) :: binary()
def to_s(
@impl IbanEx.Country.Template
@spec to_string(Iban.t()) :: binary()
@spec to_string(Iban.t(), binary()) :: binary()
def to_string(
%Iban{
country_code: country_code,
check_digits: check_digits,

View File

@ -3,23 +3,15 @@ defmodule IbanEx.Country.PT do
Portugal IBAN parsing rules
"""
alias IbanEx.Iban
@behaviour IbanEx.Country.Template
@size 25
@rule ~r/^(?<bank_code>[0-9]{4})(?<branch_code>[0-9]{4})(?<account_number>[0-9]{11})(?<national_check>[0-9]{2})$/i
@spec size() :: 25
def size(), do: @size
use IbanEx.Country.Template
@spec rule() :: Regex.t()
def rule(), do: @rule
@spec to_s(Iban.t()) :: binary()
@spec to_s(Iban.t(), binary()) :: binary()
def to_s(
@impl IbanEx.Country.Template
@spec to_string(Iban.t()) :: binary()
@spec to_string(Iban.t(), binary()) :: binary()
def to_string(
%Iban{
country_code: country_code,
check_digits: check_digits,

View File

@ -1,4 +1,6 @@
defmodule IbanEx.Country.Template do
@moduledoc false
alias IbanEx.Iban
@type size() :: non_neg_integer()
@type rule() :: Regex.t()
@ -7,6 +9,41 @@ defmodule IbanEx.Country.Template do
@callback size() :: size()
@callback rule() :: rule()
@callback to_s(Iban.t(), joiner()) :: String.t()
@callback to_s(Iban.t()) :: String.t()
@callback to_string(Iban.t(), joiner()) :: String.t()
@callback to_string(Iban.t()) :: String.t()
defmacro __using__(_opts) do
quote do
alias IbanEx.Iban
@behaviour IbanEx.Country.Template
@impl IbanEx.Country.Template
@spec to_string(Iban.t()) :: binary()
@spec to_string(Iban.t(), binary()) :: binary()
def to_string(
%Iban{
country_code: country_code,
check_digits: check_digits,
bank_code: bank_code,
branch_code: _branch_code,
national_check: _national_check,
account_number: account_number
} = _iban,
joiner \\ " "
) do
[country_code, check_digits, bank_code, account_number]
|> Enum.join(joiner)
end
@impl IbanEx.Country.Template
@spec size() :: Integer.t()
def size(), do: @size
@impl IbanEx.Country.Template
@spec rule() :: Regex.t()
def rule(), do: @rule
defoverridable to_string: 1, to_string: 2, size: 0, rule: 0
end
end
end

View File

@ -3,34 +3,8 @@ defmodule IbanEx.Country.UA do
Ukrainian IBAN parsing rules
"""
alias IbanEx.Iban
@behaviour IbanEx.Country.Template
@size 29
@rule ~r/^(?<bank_code>[0-9]{6})(?<account_number>[0-9A-Z]{19})$/i
@spec size() :: 29
def size(), do: @size
@spec rule() :: Regex.t()
def rule(), do: @rule
@spec to_s(Iban.t()) :: binary()
@spec to_s(Iban.t(), binary()) :: binary()
def to_s(
%Iban{
country_code: country_code,
check_digits: check_digits,
bank_code: bank_code,
branch_code: _branch_code,
national_check: _national_check,
account_number: account_number
} = _iban,
joiner \\ " "
) do
[country_code, check_digits, bank_code, account_number]
|> Enum.join(joiner)
end
use IbanEx.Country.Template
end