Module: Formatters

Defined in:
lib/cuculungwa/formatters.rb

Instance Method Summary collapse

Instance Method Details

#i18n_number(number_str, locale_spec) ⇒ Object

Function creates localized version of a number represented by a string based on locale_spec eg. 123456.55 -> 123,45.55 (with locale_spec = : ‘,’, :delimiter ‘.’

5123456.55 -> 5 123 45,55 (with locale_spec = {:separator : ' ', :delimiter '.'}


5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/cuculungwa/formatters.rb', line 5

def i18n_number(number_str, locale_spec)
  splits = number_str.split('.')

  unless splits.length == 2
    fail Exception.new("#{number_str} doesn't contain dot.")
  end

  whole_part      = splits[0]
  fractional_part = splits[1]
  separator       = locale_spec[:separator]

  delimiter = unless fractional_part.empty?
                locale_spec[:delimiter]
              else
                ''
  end

  negative = false
  if whole_part[0] == '-'
    negative = true
    whole_part = whole_part[1..-1]
  end

  whole_part_result = ''
  for i in whole_part.length.downto(1)
    j = whole_part.length - i

    whole_part_result = separator + whole_part_result if j % 3 == 0 && j > 0

    whole_part_result = whole_part[i - 1] + whole_part_result
  end

  unless negative
    return whole_part_result + delimiter + fractional_part
  else
    return '-' + whole_part_result + delimiter + fractional_part
  end
end

#pl_iban_formatter(number) ⇒ Object



56
57
58
59
60
# File 'lib/cuculungwa/formatters.rb', line 56

def pl_iban_formatter(number)
  number = number.gsub(/\s+/, '').gsub('PL', '')
  number = number[0..1] + ' ' + number[2..-1].gsub(/(.{4})/, '\1 ').strip
  return number
end

#short_iban(iban) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
# File 'lib/cuculungwa/formatters.rb', line 44

def short_iban(iban)
  if iban.length >= 15
    first_part = iban[0..1] + ' ' + iban[2..3] + ' ' + iban[4..7]
    middle_part = ' ... '
    last_part = iban[-4..-1]
    iban_number = first_part + middle_part + last_part
  else
    iban_number = add_space = iban[0..1] + ' ' + iban[2..iban.length]
  end
  return iban_number
end