Sha256: 15ca01f516c97f80aa327fa45a0fcc37cd67cc7a009979617af7c44d33f7d18a

Contents?: true

Size: 1.89 KB

Versions: 24

Compression:

Stored size: 1.89 KB

Contents

# encoding: UTF-8

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

require 'erb'

module TwitterCldr
  module Resources
    class CasefolderClassGenerator

      attr_reader :template_file, :output_dir

      def initialize(template_file, output_dir)
        @template_file = template_file
        @output_dir = output_dir
      end

      def generate
        output_file = File.basename(template_file).chomp(".erb")
        File.open(File.join(output_dir, output_file), "w+") do |f|
          f.write(
            ERB.new(File.read(template_file)).result(binding)
          )
        end
      end

      def casefolding_char_class_for(status)
        to_regex_char_sequence(casefolding_data_for(status))
      end

      def casefolding_hash_for(statuses)
        statuses.inject({}) do |ret, status|
          ret.merge!(casefolding_data_for(status))
          ret
        end
      end

      def inspect_hash_in_lines(hash, per_line, indent)
        str = "{\n#{"  " * indent}"
        hash.each_with_index do |(key, val), idx|
          if idx > 0 && idx % per_line == 0
            str << "\n#{"  " * indent}"
          end
          str << "#{key.inspect}=>#{val.inspect}"
          str << ", " if idx != (hash.size - 1)
        end
        str << "\n#{"  " * (indent - 1)}}"
      end

      private

      def to_regex_char_sequence(casefold_data)
        casefold_data.map { |(source, _)| to_utf8(source) }.join("|")
      end

      def to_utf8(obj)
        arr = obj.is_a?(Array) ? obj : [obj]
        arr.pack("U*").bytes.to_a.map { |s| "\\" + s.to_s(8) }.join
      end

      def casefolding_data_for(status)
        resource.inject({}) do |ret, data|
          ret[data[:source]] = data[:target] if data[:status] == status
          ret
        end
      end

      def resource
        @@resource ||= TwitterCldr.get_resource("unicode_data", "casefolding")
      end

    end
  end
end

Version data entries

24 entries across 24 versions & 2 rubygems

Version Path
twitter_cldr-4.0.0 lib/twitter_cldr/resources/casefolder_class_generator.rb
twitter_cldr-3.6.0 lib/twitter_cldr/resources/casefolder_class_generator.rb
twitter_cldr-3.5.0 lib/twitter_cldr/resources/casefolder_class_generator.rb
twitter_cldr-3.4.0 lib/twitter_cldr/resources/casefolder_class_generator.rb
twitter_cldr-3.3.0 lib/twitter_cldr/resources/casefolder_class_generator.rb
solidus_backend-1.0.0.pre3 vendor/bundle/gems/twitter_cldr-3.2.1/lib/twitter_cldr/resources/casefolder_class_generator.rb
solidus_backend-1.0.0.pre2 vendor/bundle/gems/twitter_cldr-3.2.1/lib/twitter_cldr/resources/casefolder_class_generator.rb
solidus_backend-1.0.0.pre vendor/bundle/gems/twitter_cldr-3.1.2/lib/twitter_cldr/resources/casefolder_class_generator.rb
twitter_cldr-3.2.1 lib/twitter_cldr/resources/casefolder_class_generator.rb
twitter_cldr-3.2.0 lib/twitter_cldr/resources/casefolder_class_generator.rb
twitter_cldr-3.1.2 lib/twitter_cldr/resources/casefolder_class_generator.rb
twitter_cldr-3.1.1 lib/twitter_cldr/resources/casefolder_class_generator.rb
twitter_cldr-3.1.0 lib/twitter_cldr/resources/casefolder_class_generator.rb
twitter_cldr-3.0.10 lib/twitter_cldr/resources/casefolder_class_generator.rb
twitter_cldr-3.0.9 lib/twitter_cldr/resources/casefolder_class_generator.rb
twitter_cldr-3.0.8 lib/twitter_cldr/resources/casefolder_class_generator.rb
twitter_cldr-3.0.7 lib/twitter_cldr/resources/casefolder_class_generator.rb
twitter_cldr-3.0.6 lib/twitter_cldr/resources/casefolder_class_generator.rb
twitter_cldr-3.0.5 lib/twitter_cldr/resources/casefolder_class_generator.rb
twitter_cldr-3.0.4 lib/twitter_cldr/resources/casefolder_class_generator.rb