Sha256: 85a4ab62c8a94454432ac4f58582456443576934061da7f9dedbebad010a0ff9

Contents?: true

Size: 1.29 KB

Versions: 1

Compression:

Stored size: 1.29 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
    DEFAULT_TYPE = :decimal
    attr_reader :type

    def to_decimal(options = {})
      self.setup_for(:decimal)
      self
    end

    def to_currency(options = {})
      self.setup_for(:currency)
      self
    end

    def to_percent(options = {})
      self.setup_for(:percent)
      self
    end

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

    def to_s(options = {})
      opts = options
      opts = { :precision => 0 }.merge(opts) if @base_obj.is_a?(Fixnum)
      @formatter.format(@base_obj, opts)
    end

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

    protected

    def formatter_const
      TwitterCldr::Formatters::DecimalFormatter
    end

    def setup_for(type)
      @type = type
      fmt_class = TwitterCldr::Formatters.const_get("#{(@type || DEFAULT_TYPE).to_s.capitalize}Formatter".to_sym)
      @formatter = fmt_class.new(:locale => locale.to_sym)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
twitter_cldr-1.1.0 lib/ext/numbers/localized_number.rb