class SimpleForm::BankAccountNumber::Formatter

Public Class Methods

formatted_bank_account_number(full_number, country) click to toggle source
# File lib/simple_form/bank_account_number/formatter.rb, line 7
def self.formatted_bank_account_number(full_number, country)
  full_number = full_number.to_s.dup
  format = Format.for_country(country)

  fields = format.parts.inject({}) do |hash, part|
    length = part.format.source.scan(/\b\d}/).map(&:to_i).max
    field = full_number.slice!(0, length || full_number.length)

    hash.merge!(part.label => field)
  end

  unless full_number.empty?
    raise ArgumentError, "format does not match (#{full_number.length} extra digits)"
  end

  new(fields, country)
end
new(fields, country) click to toggle source
# File lib/simple_form/bank_account_number/formatter.rb, line 25
def initialize(fields, country)
  @fields = fields
  @country = country
end

Public Instance Methods

multiline?() click to toggle source
# File lib/simple_form/bank_account_number/formatter.rb, line 50
def multiline?
  Format.for_country(@country).multiline
end
parts() click to toggle source
# File lib/simple_form/bank_account_number/formatter.rb, line 46
def parts
  @fields.values
end
to_h() click to toggle source
# File lib/simple_form/bank_account_number/formatter.rb, line 42
def to_h
  @fields
end
to_s(value_separator: ": ", field_separator: "\n") click to toggle source
# File lib/simple_form/bank_account_number/formatter.rb, line 30
def to_s(value_separator: ": ", field_separator: "\n")
  hash = if multiline?
    to_h
  else
    { "Account Number" => to_h.values.join("-") } # TODO: i18n
  end

  hash.map do |label, value|
    [label, value_separator, value].join
  end.join(field_separator)
end