Sha256: cc52a38a9017b94f2245f1472c933e2b35b58355a4abf644e01708419cc48d9b

Contents?: true

Size: 1.27 KB

Versions: 1

Compression:

Stored size: 1.27 KB

Contents

# encoding: UTF-8

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

module TwitterCldr

  module LocalizedNumberMixin
    def localize(locale = TwitterCldr.get_locale)
      TwitterCldr::LocalizedNumber.new(self, locale)
    end
  end

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

    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 = {})
      options = { :precision => 0 }.merge(options) if @base_obj.is_a?(Fixnum)
      @formatter.format(@base_obj, options)
    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.capitalize}Formatter")
    end

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

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
twitter_cldr-1.2.0 lib/twitter_cldr/core_ext/numbers/localized_number.rb