update.
[chise/ruby.git] / chise / character.rb
index 190792f..6fb8fbf 100755 (executable)
@@ -4,6 +4,8 @@ require "singleton"
 require "chise/parser"
 require "chise/chisedb"
 require "chise/iconv"
+require "chise/utf8"
+require "chise/ids"
 
 module CHISE
   class CharacterFactory # generate Character object and cache them
@@ -33,16 +35,21 @@ module CHISE
   end
 
   class Character
+    include UTF8Value
+    include CharacterIDC
+    include CharacterIDS
+
     def initialize(char_id)
       raise if char_id.nil?
-      raise unless char_id.is_a?(Fixnum) # char_id sure is a Fixnum.
-      raise if char_id < 0 # char_id sure is a positive value.
+      raise unless char_id.kind_of?(Integer) # make sure char_id is Integer.
+      raise if char_id < 0 # make sure char_id is positive.
       @char_id = char_id
       @char_id.freeze
-      @utf8_mcs = CHISE.i_tou8(@char_id)
+      # @utf8_mcs = CHISE.i_tou8(@char_id)
+      @utf8_mcs = itou8(@char_id)
       @utf8_mcs.freeze
       @feature = {}
-      @check_all_done = nil
+      # @check_all_done = nil
     end
     attr_reader :char_id
     attr_reader :utf8_mcs
@@ -83,8 +90,8 @@ module CHISE
     def []=(k,v)
       f = normalize_feature_name(k)
       cd = ChiseDB.instance
-      ft = cd.get_feature(f)
-      ft.set_value(@char_id, v)
+      feature = cd.get_feature(f)
+      feature.set_value(@char_id, v)
       @feature[f] = v;
     end
 
@@ -106,11 +113,35 @@ module CHISE
       en.to_er(self)
     end
 
+    def each_feature
+      cd = ChiseDB.instance
+      #return
+      cd.each_feature_name {|f|
+       #p f
+       feature = cd.get_feature(f)
+       begin
+         v = feature.get_value(@char_id)
+         next if v.nil?
+         yield(f, v)
+       ensure
+         feature.close # important
+       end
+      }
+    end
+
+    def hash_feature
+      h = {}
+      each_feature {|k, v|
+       h[k] = v
+      }
+      h
+    end
+
     private
 
     def get_feature(f)
       cd = ChiseDB.instance
-      cd.load_feature(f, @char_id)
+      cd.load_feature(@char_id, f)
     end
 
     def normalize_feature_name(a)
@@ -122,5 +153,6 @@ module CHISE
       a = a.sub(/\Afrom-/, "<-")
       a
     end
+
   end
 end