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


40
41
42
# File 'lib/twitter_cldr/formatters/numbers/helpers/integer.rb', line 40

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

#format_groups(string) ⇒ Object


23
24
25
26
27
28
29
30
# 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) while string.length > groups.last
  tokens << string
  tokens.compact.reverse.join(separator)
end

#parse_groups(format) ⇒ Object


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

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


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

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