Sha256: c31006dd7712fc6f3de7d7267c434ee0920c604a30c0723f6686fb9128ed2097

Contents?: true

Size: 1.47 KB

Versions: 45

Compression:

Stored size: 1.47 KB

Contents

# encoding: UTF-8

# Copyright 2012 Twitter, Inc
# http://www.apache.org/licenses/LICENSE-2.0

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

45 entries across 45 versions & 2 rubygems

Version Path
solidus_backend-1.0.0.pre3 vendor/bundle/gems/twitter_cldr-3.2.1/lib/twitter_cldr/formatters/numbers/helpers/integer.rb
solidus_backend-1.0.0.pre2 vendor/bundle/gems/twitter_cldr-3.2.1/lib/twitter_cldr/formatters/numbers/helpers/integer.rb
solidus_backend-1.0.0.pre vendor/bundle/gems/twitter_cldr-3.1.2/lib/twitter_cldr/formatters/numbers/helpers/integer.rb
twitter_cldr-3.2.1 lib/twitter_cldr/formatters/numbers/helpers/integer.rb
twitter_cldr-3.2.0 lib/twitter_cldr/formatters/numbers/helpers/integer.rb
twitter_cldr-3.1.2 lib/twitter_cldr/formatters/numbers/helpers/integer.rb
twitter_cldr-3.1.1 lib/twitter_cldr/formatters/numbers/helpers/integer.rb
twitter_cldr-3.1.0 lib/twitter_cldr/formatters/numbers/helpers/integer.rb
twitter_cldr-3.0.10 lib/twitter_cldr/formatters/numbers/helpers/integer.rb
twitter_cldr-3.0.9 lib/twitter_cldr/formatters/numbers/helpers/integer.rb
twitter_cldr-3.0.8 lib/twitter_cldr/formatters/numbers/helpers/integer.rb
twitter_cldr-3.0.7 lib/twitter_cldr/formatters/numbers/helpers/integer.rb
twitter_cldr-3.0.6 lib/twitter_cldr/formatters/numbers/helpers/integer.rb
twitter_cldr-3.0.5 lib/twitter_cldr/formatters/numbers/helpers/integer.rb
twitter_cldr-3.0.4 lib/twitter_cldr/formatters/numbers/helpers/integer.rb
twitter_cldr-3.0.3 lib/twitter_cldr/formatters/numbers/helpers/integer.rb
twitter_cldr-3.0.2 lib/twitter_cldr/formatters/numbers/helpers/integer.rb
twitter_cldr-3.0.1 lib/twitter_cldr/formatters/numbers/helpers/integer.rb
twitter_cldr-2.4.3 lib/twitter_cldr/formatters/numbers/helpers/integer.rb
twitter_cldr-3.0.0 lib/twitter_cldr/formatters/numbers/helpers/integer.rb