class TwitterCldr::Formatters::Numbers::Integer

Attributes

format[R]
groups[R]
separator[R]

Public Class Methods

new(token, symbols = {}) click to toggle source
# 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

Public Instance Methods

apply(number, options = {}) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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