Sha256: aa158695ce1ae048e13cc79bb6fb32e9505f8975c4311517ac263ff6302f9199

Contents?: true

Size: 1.67 KB

Versions: 1

Compression:

Stored size: 1.67 KB

Contents

# encoding: UTF-8

module TwitterCldr
  module Tokenizers
    class NumberTokenizer < Base
      def initialize(options = {})
        super(options)
        @type = options[:type] || :decimal
        @token_splitter_regex = /([^0*#,\.]*)([0#,\.]+)([^0*#,\.]*)$/
        @token_type_regexes = [{ :type => :pattern, :regex => /[0?#,\.]*/ },
                               { :type => :plaintext, :regex => // }]
        @base_path = "numbers.formats"
        @symbol_path = "numbers.symbols"
        @paths = { :default => "patterns.default",
                   :positive => "patterns.positive",
                   :negative => "patterns.negative" }
      end

      def tokens(options = {})
        unless self.traverse(self.full_path_for(:positive))
          key_path = self.full_path_for(:default)
          positive, negative = self.traverse(key_path).split(/;/)
          insert_point = self.traverse(KeyPath.dirname(key_path))
          insert_point[:positive] = positive
          insert_point[:negative] = negative ? negative : "#{self.symbols[:minus] || '-'}#{positive}"
        end

        sign = options[:sign] || :positive
        self.tokens_for(self.full_path_for(sign), nil)
      end

      def symbols
        self.traverse(@symbol_path)
      end

      protected

      def full_path_for(path)
        KeyPath.join(@base_path, @type.to_s, self.paths[path])
      end

      def init_resources
        @resource = TwitterCldr.resources.resource_for(@locale, "numbers")[TwitterCldr.convert_locale(@locale)]
      end

      def pattern_for(resource)
        # can't go any deeper, so return original pattern (which should NOT be a hash, by the way)
        resource
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
twitter_cldr-1.0.1 lib/tokenizers/numbers/number_tokenizer.rb