Sha256: eb472cbefec417e4338b34ac0498c7b3e458b7ce77d07e03c6b83a57515aee24

Contents?: true

Size: 1.25 KB

Versions: 11

Compression:

Stored size: 1.25 KB

Contents

# encoding: UTF-8

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

module TwitterCldr
  module Localized

    class LocalizedNumber < LocalizedObject
      DEFAULT_TYPE = :decimal
      TYPES = [
        :decimal,
        :short_decimal,
        :long_decimal,
        :currency,
        :percent,
      ]

      attr_reader :type

      def initialize(obj, locale, options = {})
        @options = options.dup

        @type = @options.delete(:type) || DEFAULT_TYPE
        raise ArgumentError.new("type #{@type} is not supported") unless @type && TYPES.include?(@type.to_sym)

        super(obj, locale, @options)
      end

      TYPES.each do |type|
        define_method "to_#{type}" do
          to_type(type)
        end
      end

      def to_s(options = {})
        @formatter.format(@base_obj, options.merge(:type => @type))
      end

      def plural_rule
        TwitterCldr::Formatters::Plurals::Rules.rule_for(@base_obj, @locale)
      end

      protected

      def formatter_const
        TwitterCldr::Formatters.const_get("#{@type.to_s.split("_").map(&:capitalize).join}Formatter")
      end

      def to_type(target_type)
        self.class.new(@base_obj, @locale, @options.merge(:type => target_type))
      end
    end

  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
twitter_cldr-2.4.3 lib/twitter_cldr/localized/localized_number.rb
twitter_cldr-3.0.0.beta1 lib/twitter_cldr/localized/localized_number.rb
twitter_cldr-2.4.2 lib/twitter_cldr/localized/localized_number.rb
twitter_cldr-2.4.1 lib/twitter_cldr/localized/localized_number.rb
twitter_cldr-2.4.0 lib/twitter_cldr/localized/localized_number.rb
twitter_cldr-2.2.0 lib/twitter_cldr/localized/localized_number.rb
twitter_cldr-2.1.1 lib/twitter_cldr/localized/localized_number.rb
twitter_cldr-2.1.0 lib/twitter_cldr/localized/localized_number.rb
twitter_cldr-2.0.2 lib/twitter_cldr/localized/localized_number.rb
twitter_cldr-2.0.1 lib/twitter_cldr/localized/localized_number.rb
twitter_cldr-2.0.0 lib/twitter_cldr/localized/localized_number.rb