lib/gimchi/char.rb in gimchi-0.1.0 vs lib/gimchi/char.rb in gimchi-0.1.1

- old
+ new

@@ -1,18 +1,23 @@ # encoding: UTF-8 module Gimchi class Korean - # Class representing each Korean character. - # chosung, jungsung and jongsung can be get and set. + # Class representing each Korean character. Its three components, + # `chosung', `jungsung' and `jongsung' can be get and set. # - # to_s merges components into a String. - # to_a returns the three components. + # `to_s' merges components into a String. `to_a' returns the three components. class Char - attr_reader :org - attr_reader :chosung, :jungsung, :jongsung + # @return [String] Chosung component of this character. + attr_reader :chosung + # @return [String] Jungsung component of this character. + attr_reader :jungsung + # @return [String] Jongsung component of this character. + attr_reader :jongsung + # @param [Gimchi::Korean] kor Gimchi::Korean instance + # @param [String] kchar Korean character string def initialize kor, kchar raise ArgumentError('Not a korean character') unless kor.korean_char? kchar @kor = kor @cur = [] @@ -26,20 +31,21 @@ n2 = n / 28; n3 = n % 28; self.chosung = @kor.chosungs[n1] self.jungsung = @kor.jungsungs[n2] self.jongsung = ([nil] + @kor.jongsungs)[n3] - elsif (@kor.chosungs + @kor.jongsungs).include? kchar + elsif @kor.chosungs.include? kchar self.chosung = kchar elsif @kor.jungsungs.include? kchar self.jungsung = kchar + elsif @kor.jongsungs.include? kchar + self.jongsung = kchar end - - @org = self.dup end - # recombine components into korean character + # Recombines components into a korean character. + # @return [String] Combined korean character def to_s if chosung.nil? && jungsung.nil? "" elsif chosung && jungsung n1, n2, n3 = @@ -50,55 +56,68 @@ else chosung || jungsung end end + # Sets the chosung component. + # @param [String] def chosung= c raise ArgumentError.new('Invalid chosung component') if c && @kor.chosungs.include?(c) == false @chosung = c && c.dup.extend(Component).tap { |e| e.kor = @kor } end + # Sets the jungsung component + # @param [String] def jungsung= c raise ArgumentError.new('Invalid jungsung component') if c && @kor.jungsungs.include?(c) == false @jungsung = c && c.dup.extend(Component).tap { |e| e.kor = @kor } end + # Sets the jongsung component + # + # @param [String] def jongsung= c raise ArgumentError.new('Invalid jongsung component') if c && @kor.jongsungs.include?(c) == false @jongsung = c && c.dup.extend(Component).tap { |e| e.kor = @kor } end - # to_a returns the three components. + # Returns Array of three components. + # + # @return [Array] Array of three components def to_a [chosung, jungsung, jongsung] end - # Check if this is a complete Korean character + # Checks if this is a complete Korean character. def complete? chosung.nil? == false && jungsung.nil? == false end - # Check if this is a non-complete Korean character + # Checks if this is a non-complete Korean character. # e.g. ㅇ, ㅏ def partial? chosung.nil? || jungsung.nil? end private - # nodoc # + # Three components of Korean::Char are extended to support #vowel? and #consonant? method. module Component + # @return [Korean] Hosting Korean instance attr_accessor :kor + # Is this component a vowel? def vowel? kor.jungsungs.include? self end + # Is this component a consonant? def consonant? self != 'ㅇ' && kor.chosungs.include?(self) end end#Component end#Char end#Korean end#Gimchi +