Class: Cldr::Format::Decimal::Integer

Inherits:
Base
  • Object
show all
Defined in:
lib/cldr/format/decimal/integer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#interpolate

Constructor Details

#initialize(format, symbols = {}) ⇒ Integer

Returns a new instance of Integer.



7
8
9
10
11
12
# File 'lib/cldr/format/decimal/integer.rb', line 7

def initialize(format, symbols = {})
  format     = format.split('.')[0]
  @format    = prepare_format(format, symbols)
  @groups    = parse_groups(format)
  @separator = symbols[:group] || ','
end

Instance Attribute Details

#formatObject (readonly)

Returns the value of attribute format.



5
6
7
# File 'lib/cldr/format/decimal/integer.rb', line 5

def format
  @format
end

#groupsObject (readonly)

Returns the value of attribute groups.



5
6
7
# File 'lib/cldr/format/decimal/integer.rb', line 5

def groups
  @groups
end

#separatorObject (readonly)

Returns the value of attribute separator.



5
6
7
# File 'lib/cldr/format/decimal/integer.rb', line 5

def separator
  @separator
end

Instance Method Details

#apply(number, options = {}) ⇒ Object



14
15
16
# File 'lib/cldr/format/decimal/integer.rb', line 14

def apply(number, options = {})
  format_groups(interpolate(format, number.to_i))
end

#chop_group(string, size) ⇒ Object



35
36
37
# File 'lib/cldr/format/decimal/integer.rb', line 35

def chop_group(string, size)
  string.slice!(-size, size) if string.length > size
end

#format_groups(string) ⇒ Object



18
19
20
21
22
23
24
25
# File 'lib/cldr/format/decimal/integer.rb', line 18

def format_groups(string)
  return string if groups.empty?
  tokens = []
  tokens << chop_group(string, groups.first)
  tokens << chop_group(string, groups.last) while string.length > groups.last
  tokens << string
  tokens.compact.reverse.join(separator)
end

#parse_groups(format) ⇒ Object



27
28
29
30
31
32
33
# File 'lib/cldr/format/decimal/integer.rb', line 27

def parse_groups(format)
  return [] unless index = format.rindex(',')
  rest   = format[0, index]
  widths = [format.length - index - 1]
  widths << rest.length - rest.rindex(',') - 1 if rest.rindex(',')
  widths.compact.uniq
end

#prepare_format(format, symbols) ⇒ Object



39
40
41
42
# File 'lib/cldr/format/decimal/integer.rb', line 39

def prepare_format(format, symbols)
  signs = symbols.values_at(:plus_sign, :minus_sign)
  format.tr(',', '').tr('+-', signs.join)
end