Sha256: 6b7ad8c359c5da6d6221398165d725a952ec5766f085f00f213b3f3bf0fc2b47

Contents?: true

Size: 986 Bytes

Versions: 1

Compression:

Stored size: 986 Bytes

Contents

module Cnname
  class Name
    attr_reader :first_name, :last_name

    def initialize(name)
      @first_name, @last_name = parse(name)
    end

    private
    def parse(name)
      if hanzi_compound_surnames.include?(name[0..1])
        [name[2..-1], name[0..1]]
      elsif hanzi_singleton_surnames.include?(name[0])
        [name[1..-1], name[0]]
      else
        raise 'Invalid Chinese name'
      end
    end

    def singleton_surnames
      @singleton_surnames ||= Oj.load(File.open(File.join(File.dirname(__FILE__), '../../data/singleton_surnames.json')))
    end

    def compound_surnames
      @compound_surnames ||= Oj.load(File.open(File.join(File.dirname(__FILE__), '../../data/compound_surnames.json')))
    end

    def hanzi_singleton_surnames
      @hanzi_singleton_surnames ||= singleton_surnames.collect { |n| n['hanzi'] }
    end

    def hanzi_compound_surnames
      @hanzi_compound_surnames ||= compound_surnames.collect { |n| n['hanzi'] }
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
cnname-0.1.0 lib/cnname/name.rb