Sha256: f2a1743d570b6edc3b411f894a30d13516516c37964cb571de0ed65f09902a68

Contents?: true

Size: 1.24 KB

Versions: 37

Compression:

Stored size: 1.24 KB

Contents

# encoding: UTF-8

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

module TwitterCldr
  module Shared

    class UnsupportedNumberingSystemError < StandardError; end

    class NumberingSystem

      class << self
        def for_name(name)
          system_cache[name] ||= begin
            if system = resource[name.to_sym]
              if system[:type] != "numeric"
                raise UnsupportedNumberingSystemError.new("#{system[:type]} numbering systems not supported.")
              else
                new(system[:name], system[:digits])
              end
            end
          end
        end

        protected

        def system_cache
          @system_cache ||= {}
        end

        def resource
          @resource ||= TwitterCldr.get_resource(:shared, :numbering_systems)[:numbering_systems]
        end
      end

      attr_reader :name, :digits

      def initialize(name, digits)
        @name = name
        @digits = split_digits(digits)
      end

      def transliterate(number)
        number.to_s.gsub(/\d/) do |digit|
          digits[digit.to_i]
        end
      end

      protected

      def split_digits(str)
        str.unpack("U*").map { |digit| [digit].pack("U*") }
      end

    end
  end
end

Version data entries

37 entries across 37 versions & 3 rubygems

Version Path
metanorma-cli-1.3.4 gems/ruby/2.6.0/gems/twitter_cldr-4.4.5/lib/twitter_cldr/shared/numbering_system.rb
metanorma-cli-1.3.3.1 gems/ruby/2.6.0/gems/twitter_cldr-4.4.5/lib/twitter_cldr/shared/numbering_system.rb
twitter_cldr-5.0.0 lib/twitter_cldr/shared/numbering_system.rb
twitter_cldr-4.4.5 lib/twitter_cldr/shared/numbering_system.rb
twitter_cldr-4.4.4 lib/twitter_cldr/shared/numbering_system.rb
twitter_cldr-4.4.3 lib/twitter_cldr/shared/numbering_system.rb
twitter_cldr-4.4.2 lib/twitter_cldr/shared/numbering_system.rb
twitter_cldr-4.4.1 lib/twitter_cldr/shared/numbering_system.rb
twitter_cldr-4.4.0 lib/twitter_cldr/shared/numbering_system.rb
twitter_cldr-4.3.1 lib/twitter_cldr/shared/numbering_system.rb
twitter_cldr-4.3.0 lib/twitter_cldr/shared/numbering_system.rb
twitter_cldr-4.2.0 lib/twitter_cldr/shared/numbering_system.rb
twitter_cldr-4.1.0 lib/twitter_cldr/shared/numbering_system.rb
twitter_cldr-4.0.0 lib/twitter_cldr/shared/numbering_system.rb
twitter_cldr-3.6.0 lib/twitter_cldr/shared/numbering_system.rb
twitter_cldr-3.5.0 lib/twitter_cldr/shared/numbering_system.rb
twitter_cldr-3.4.0 lib/twitter_cldr/shared/numbering_system.rb
twitter_cldr-3.3.0 lib/twitter_cldr/shared/numbering_system.rb
solidus_backend-1.0.0.pre3 vendor/bundle/gems/twitter_cldr-3.2.1/lib/twitter_cldr/shared/numbering_system.rb
solidus_backend-1.0.0.pre2 vendor/bundle/gems/twitter_cldr-3.2.1/lib/twitter_cldr/shared/numbering_system.rb