Sha256: aef912f350c50d9111b75f42c73fa4d0f94f61c4bf418f1ec66167676544a462

Contents?: true

Size: 1.21 KB

Versions: 1

Compression:

Stored size: 1.21 KB

Contents

# encoding: UTF-8

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.0.1 lib/ext/numbers/localized_number.rb