module Forkforge::Category

Public Class Methods

included(base) click to toggle source

Letter::all_raw | Letter::uppercase_raw | Letter::uppercase 'Alexei' # ⇒ 'A' Letter::uppercase_code_point | Mark::non_spacing_bidirectional_category

# File lib/forkforge/category.rb, line 10
def self.included base
  base.const_get(:TYPES).each { |type|
    base.class_eval %Q{
      def #{type.last}_raw
        @@#{type.last} ||= Forkforge::UnicodeData::all_general_category /#{type.first}/
      end
      def is_#{type.last} s
        UnicodeData::infos(s).inject(true) { |memo, kv|
          memo &&= kv[:general_category] == "#{type.first}"
        }
      end
      def #{type.last} s = nil
        @@#{type.last}_array ||= #{type.last}_raw.map { |k, v| Forkforge::UnicodeData::to_char k }
        s.respond_to?(:scan) ? s.scan(Regexp.new(@@#{type.last}_array.join '|')) : @@#{type.last}_array
      end
    }
    CodePoint::UNICODE_FIELDS.each { |method|
      base.class_eval %Q{
        def #{type.last}_#{method}
          @@#{type.last}_#{method} ||= #{type.last}_raw.map { |k, v|
            [ Forkforge::UnicodeData::to_char(k), Forkforge::UnicodeData::get_#{method}(k) ]
          }.to_h
        end
      }
    }
  }
  base.extend base
end