Class: TwitterCldr::Formatters::Numbers::Integer

Inherits:
Base
  • Object
show all
Defined in:
lib/twitter_cldr/formatters/numbers/helpers/integer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#interpolate

Constructor Details

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

Returns a new instance of Integer.



12
13
14
15
16
17
# File 'lib/twitter_cldr/formatters/numbers/helpers/integer.rb', line 12

def initialize(token, symbols = {})
  format     = token.value.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.



10
11
12
# File 'lib/twitter_cldr/formatters/numbers/helpers/integer.rb', line 10

def format
  @format
end

#groupsObject (readonly)

Returns the value of attribute groups.



10
11
12
# File 'lib/twitter_cldr/formatters/numbers/helpers/integer.rb', line 10

def groups
  @groups
end

#separatorObject (readonly)

Returns the value of attribute separator.



10
11
12
# File 'lib/twitter_cldr/formatters/numbers/helpers/integer.rb', line 10

def separator
  @separator
end

Instance Method Details

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



19
20
21
# File 'lib/twitter_cldr/formatters/numbers/helpers/integer.rb', line 19

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

#chop_group(string, size) ⇒ Object



42
43
44
# File 'lib/twitter_cldr/formatters/numbers/helpers/integer.rb', line 42

def chop_group(string, size)
  string.slice!([string.size - size, 0].max, size)
end

#format_groups(string) ⇒ Object



23
24
25
26
27
28
29
30
31
32
# File 'lib/twitter_cldr/formatters/numbers/helpers/integer.rb', line 23

def format_groups(string)
  return string if groups.empty?

  tokens = []

  tokens << chop_group(string, groups.first)
  tokens << chop_group(string, groups.last) until string.empty?

  tokens.compact.reverse.join(separator)
end

#parse_groups(format) ⇒ Object



34
35
36
37
38
39
40
# File 'lib/twitter_cldr/formatters/numbers/helpers/integer.rb', line 34

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

#prepare_format(format, symbols) ⇒ Object



46
47
48
49
# File 'lib/twitter_cldr/formatters/numbers/helpers/integer.rb', line 46

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