Sha256: a5022e8a012fd87bac43c6b9315c657a879527987d3b42ee6f9ceb97e91fd7ff

Contents?: true

Size: 1.8 KB

Versions: 7

Compression:

Stored size: 1.8 KB

Contents

# encoding: UTF-8

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

module TwitterCldr
  module Localized
    class LocalizedNumber < LocalizedObject

      attr_reader :type, :format

      def initialize(obj, locale, options = {})
        @type = options[:type]
        @format = options[:format]
        super
      end

      class << self
        def types
          TwitterCldr::DataReaders::NumberDataReader.types
        end
      end

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

      def to_s(options = {})
        data_reader = TwitterCldr::DataReaders::NumberDataReader.new(locale, {
          :type => @type,
          :format => @format
        })

        pattern = data_reader.pattern(base_obj)

        if pattern == 0
          # can't format the number for current locale
          base_obj.to_s
        else
          data_reader.formatter.format(
            data_reader.tokenizer.tokenize(pattern),
            base_obj,
            options.merge(:type => @type)
          )
        end
      end

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

      def spellout
        rbnf.format(
          base_obj, TwitterCldr::Formatters::Rbnf::RbnfFormatter::DEFAULT_SPELLOUT_OPTIONS
        )
      end

      def to_rbnf_s(group_name, rule_set_name)
        rbnf.format(base_obj, {
          :rule_group => group_name,
          :rule_set => rule_set_name
        })
      end

      def rbnf
        @rbnf ||= TwitterCldr::Formatters::Rbnf::RbnfFormatter.new(locale)
      end

      protected

      def to_type(target_type)
        self.class.new(base_obj, locale, {
          :type => target_type,
          :format => @format
        })
      end

    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
twitter_cldr-3.0.10 lib/twitter_cldr/localized/localized_number.rb
twitter_cldr-3.0.9 lib/twitter_cldr/localized/localized_number.rb
twitter_cldr-3.0.8 lib/twitter_cldr/localized/localized_number.rb
twitter_cldr-3.0.7 lib/twitter_cldr/localized/localized_number.rb
twitter_cldr-3.0.6 lib/twitter_cldr/localized/localized_number.rb
twitter_cldr-3.0.5 lib/twitter_cldr/localized/localized_number.rb
twitter_cldr-3.0.4 lib/twitter_cldr/localized/localized_number.rb