Sha256: 2a08ccdfeb0a3ddcf814fc7236d3af90979031c8547d1d8903540a89f0f11f11

Contents?: true

Size: 964 Bytes

Versions: 65

Compression:

Stored size: 964 Bytes

Contents

# encoding: UTF-8

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

module TwitterCldr
  module Tokenizers
    class PatternTokenizer

      attr_reader :data_reader, :tokenizer

      def initialize(data_reader, tokenizer)
        @data_reader = data_reader
        @tokenizer = tokenizer
      end

      def tokenize(pattern)
        tokenizer.tokenize(expand(pattern))
      end

      private

      def expand(pattern)
        if pattern.is_a?(Symbol)
          # symbols mean another path was given
          path = pattern.to_s.split(".").map(&:to_sym)
          data = data_reader.pattern_at_path(path)
          next_pattern = data.is_a?(Hash) ? data[:pattern] : data
          expand_pattern(next_pattern)
        elsif pattern.is_a?(Hash)
          pattern.inject({}) do |ret, (key, val)|
            ret[key] = expand(val)
            ret
          end
        else
          pattern
        end
      end

    end
  end
end

Version data entries

65 entries across 65 versions & 3 rubygems

Version Path
twitter_cldr-6.13.0 lib/twitter_cldr/tokenizers/pattern_tokenizer.rb
twitter_cldr-6.12.1 lib/twitter_cldr/tokenizers/pattern_tokenizer.rb
twitter_cldr-6.12.0 lib/twitter_cldr/tokenizers/pattern_tokenizer.rb
twitter_cldr-6.11.5 lib/twitter_cldr/tokenizers/pattern_tokenizer.rb
twitter_cldr-6.11.4 lib/twitter_cldr/tokenizers/pattern_tokenizer.rb
twitter_cldr-6.11.3 lib/twitter_cldr/tokenizers/pattern_tokenizer.rb
twitter_cldr-6.11.2 lib/twitter_cldr/tokenizers/pattern_tokenizer.rb
twitter_cldr-6.11.1 lib/twitter_cldr/tokenizers/pattern_tokenizer.rb
twitter_cldr-6.11.0 lib/twitter_cldr/tokenizers/pattern_tokenizer.rb
twitter_cldr-6.10.0 lib/twitter_cldr/tokenizers/pattern_tokenizer.rb
twitter_cldr-6.9.0 lib/twitter_cldr/tokenizers/pattern_tokenizer.rb
twitter_cldr-6.8.0 lib/twitter_cldr/tokenizers/pattern_tokenizer.rb
twitter_cldr-6.7.0 lib/twitter_cldr/tokenizers/pattern_tokenizer.rb
twitter_cldr-6.6.2 lib/twitter_cldr/tokenizers/pattern_tokenizer.rb
twitter_cldr-6.6.1 lib/twitter_cldr/tokenizers/pattern_tokenizer.rb
twitter_cldr-6.6.0 lib/twitter_cldr/tokenizers/pattern_tokenizer.rb
twitter_cldr-6.5.0 lib/twitter_cldr/tokenizers/pattern_tokenizer.rb
twitter_cldr-6.4.0 lib/twitter_cldr/tokenizers/pattern_tokenizer.rb
twitter_cldr-6.3.0 lib/twitter_cldr/tokenizers/pattern_tokenizer.rb
twitter_cldr-6.2.0 lib/twitter_cldr/tokenizers/pattern_tokenizer.rb