Sha256: 6c927853953daf003dda46465199dc54dba311fdc3f6567ed799a41b8d29d3b4
Contents?: true
Size: 1.4 KB
Versions: 1
Compression:
Stored size: 1.4 KB
Contents
# encoding: UTF-8 module TwitterCldr module Formatters module Numbers class Integer < Base attr_reader :format, :separator, :groups def initialize(token, symbols = {}) format = token.value.split('.')[0] @format = prepare_format(format, symbols) @groups = parse_groups(format) @separator = symbols[:group] || ',' end def apply(number, options = {}) format_groups(interpolate(format, number.to_i)) end 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 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 def chop_group(string, size) string.slice!(-size, size) if string.length > size end def prepare_format(format, symbols) signs = symbols.values_at(:plus_sign, :minus_sign) format.tr(',', '').tr('+-', signs.join) end end end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
twitter_cldr-1.0.1 | lib/formatters/numbers/helpers/integer.rb |