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.1.0 lib/twitter_cldr/tokenizers/pattern_tokenizer.rb
metanorma-cli-1.3.4 gems/ruby/2.6.0/gems/twitter_cldr-4.4.5/lib/twitter_cldr/tokenizers/pattern_tokenizer.rb
twitter_cldr-6.0.2 lib/twitter_cldr/tokenizers/pattern_tokenizer.rb
metanorma-cli-1.3.3.1 gems/ruby/2.6.0/gems/twitter_cldr-4.4.5/lib/twitter_cldr/tokenizers/pattern_tokenizer.rb
twitter_cldr-6.0.1 lib/twitter_cldr/tokenizers/pattern_tokenizer.rb
twitter_cldr-6.0.0 lib/twitter_cldr/tokenizers/pattern_tokenizer.rb
twitter_cldr-5.4.0 lib/twitter_cldr/tokenizers/pattern_tokenizer.rb
twitter_cldr-5.3.0 lib/twitter_cldr/tokenizers/pattern_tokenizer.rb
twitter_cldr-5.2.0 lib/twitter_cldr/tokenizers/pattern_tokenizer.rb
twitter_cldr-5.1.0 lib/twitter_cldr/tokenizers/pattern_tokenizer.rb
twitter_cldr-5.0.0 lib/twitter_cldr/tokenizers/pattern_tokenizer.rb
twitter_cldr-4.4.5 lib/twitter_cldr/tokenizers/pattern_tokenizer.rb
twitter_cldr-4.4.4 lib/twitter_cldr/tokenizers/pattern_tokenizer.rb
twitter_cldr-4.4.3 lib/twitter_cldr/tokenizers/pattern_tokenizer.rb
twitter_cldr-4.4.2 lib/twitter_cldr/tokenizers/pattern_tokenizer.rb
twitter_cldr-4.4.1 lib/twitter_cldr/tokenizers/pattern_tokenizer.rb
twitter_cldr-4.4.0 lib/twitter_cldr/tokenizers/pattern_tokenizer.rb
twitter_cldr-4.3.1 lib/twitter_cldr/tokenizers/pattern_tokenizer.rb
twitter_cldr-4.3.0 lib/twitter_cldr/tokenizers/pattern_tokenizer.rb
twitter_cldr-4.2.0 lib/twitter_cldr/tokenizers/pattern_tokenizer.rb