[logaling-commit] logaling/logaling-command [refactor-structure] Move Repository#show_glossary logic to Glossary#terms

Back to archive index

null+****@clear***** null+****@clear*****
Mon Jun 11 18:38:22 JST 2012


SUZUKI Miho	2012-06-11 18:38:22 +0900 (Mon, 11 Jun 2012)

  New Revision: 498902e49b5456d12763f3fa7ebc3d2e657bf033

  Log:
    Move Repository#show_glossary logic to Glossary#terms

  Added files:
    lib/logaling/glossary.rb
  Modified files:
    lib/logaling/command/application.rb
    lib/logaling/glossary_db.rb
    lib/logaling/project.rb
    lib/logaling/repository.rb

  Modified: lib/logaling/command/application.rb (+4 -2)
===================================================================
--- lib/logaling/command/application.rb    2012-06-11 12:25:14 +0900 (522bdd9)
+++ lib/logaling/command/application.rb    2012-06-11 18:38:22 +0900 (3d5cf7c)
@@ -19,6 +19,7 @@ require 'thor'
 require 'rainbow'
 require 'pathname'
 require "logaling/repository"
+require "logaling/glossary"
 require "logaling/glossary_source"
 require "logaling/config"
 
@@ -275,8 +276,9 @@ module Logaling::Command
       }
       @config.check_required_option(required_options)
       check_logaling_home_exists
-      @repository.index
-      terms =****@repos*****_glossary(glossary_source)
+      project =****@repos*****_project(@config.glossary)
+      glossary = project.find_glossary(@config.source_language, @config.target_language)
+      terms = glossary.terms
       unless terms.empty?
         run_pager
         max_str_size = terms.map{|term| term[:source_term].size}.sort.last

  Added: lib/logaling/glossary.rb (+68 -0) 100644
===================================================================
--- /dev/null
+++ lib/logaling/glossary.rb    2012-06-11 18:38:22 +0900 (296f306)
@@ -0,0 +1,68 @@
+# -*- coding: utf-8 -*-
+#
+# Copyright (C) 2012  Miho SUZUKI
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+module Logaling
+  class Glossary
+    SUPPORTED_FILE_TYPE = %w(yml tsv csv)
+
+    attr_reader :name, :source_language, :target_language
+
+    def initialize(name, source_language, target_language, project=nil)
+      @name = name
+      @source_language = source_language
+      @target_language = target_language
+      @project = project
+    end
+
+    def terms
+      raise Logaling::GlossaryDBNotFound unless File.exist?(@project.glossary_db_path)
+      index
+      terms = []
+      Logaling::GlossaryDB.open(@project.glossary_db_path, "utf8") do |db|
+        terms = db.translation_list(self)
+      end
+      terms
+    end
+
+    private
+    def index
+      Logaling::GlossaryDB.open(@project.glossary_db_path, "utf8") do |db|
+        db.recreate_table
+        glossary_sources.each do |glossary_source|
+          indexed_at = File.mtime(glossary_source)
+          unless db.glossary_source_exist?(glossary_source, indexed_at)
+            puts "now index #{@name}..."
+            db.index_glossary(GlossarySource.load(glossary_source), @name, glossary_source, @source_language, @target_language, indexed_at)
+          end
+        end
+        glossary_string = [@name, @source_language, @target_language].join('.')
+        indexed_glossary_sources = db.glossary_sources_related_on_glossary(glossary_string)
+        (indexed_glossary_sources - glossary_sources).each do |removed_glossary_source|
+          puts "now deindex #{@name}..."
+          db.deindex_glossary(@name, removed_glossary_source)
+        end
+      end
+    end
+
+    def glossary_sources
+      glob_condition = SUPPORTED_FILE_TYPE.map do |type|
+        file_name = [@name, @source_language, @target_language, type].join('.')
+        File.join(@project.glossary_source_path, file_name)
+      end
+      Dir.glob(glob_condition)
+    end
+  end
+end

  Modified: lib/logaling/glossary_db.rb (+11 -4)
===================================================================
--- lib/logaling/glossary_db.rb    2012-06-11 12:25:14 +0900 (0fe03d1)
+++ lib/logaling/glossary_db.rb    2012-06-11 18:38:22 +0900 (4fa3953)
@@ -153,12 +153,12 @@ module Logaling
       records_selected.expression.close if records_selected
     end
 
-    def translation_list(glossary_source, order='ascending')
+    def translation_list(glossary, order='ascending')
       records_raw = Groonga["translations"].select do |record|
         [
-          record.glossary == glossary_source.glossary,
-          record.source_language == glossary_source.source_language,
-          record.target_language == glossary_source.target_language
+          record.glossary == glossary.name,
+          record.source_language == glossary.source_language,
+          record.target_language == glossary.target_language
         ]
       end
 
@@ -212,6 +212,13 @@ module Logaling
       ]).map{|record| record.key}
     end
 
+    def glossary_sources_related_on_glossary(glossary_string)
+      records = Groonga["glossary_sources"].select do |record|
+        [record.key =~ glossary_string]
+      end
+      records.map{|record| record.key.key }
+    end
+
     def get_all_glossary
       Groonga["glossaries"].sort([
         {:key=>"_key", :order=>'ascending'}

  Modified: lib/logaling/project.rb (+15 -2)
===================================================================
--- lib/logaling/project.rb    2012-06-11 12:25:14 +0900 (a97a13a)
+++ lib/logaling/project.rb    2012-06-11 18:38:22 +0900 (0d8a227)
@@ -15,14 +15,27 @@
 
 module Logaling
   class Project
-    attr_reader :path
+    attr_reader :path, :repository
 
-    def initialize(path)
+    def initialize(path, repository=nil)
       @path = path
+      @repository = repository
     end
 
     def name
       File.basename(@path)
     end
+
+    def find_glossary(source_language, target_language)
+      Logaling::Glossary.new(name, source_language, target_language, self)
+    end
+
+    def glossary_source_path
+      File.join(@path, "glossary")
+    end
+
+    def glossary_db_path
+      @repository.logaling_db_home
+    end
   end
 end

  Modified: lib/logaling/repository.rb (+5 -15)
===================================================================
--- lib/logaling/repository.rb    2012-06-11 12:25:14 +0900 (bca00f5)
+++ lib/logaling/repository.rb    2012-06-11 18:38:22 +0900 (3b86fdc)
@@ -78,19 +78,9 @@ module Logaling
       terms
     end
 
-    def show_glossary(glossary_source)
-      raise Logaling::GlossaryDBNotFound unless File.exist?(logaling_db_home)
-
-      terms = []
-      Logaling::GlossaryDB.open(logaling_db_home, "utf8") do |db|
-        terms = db.translation_list(glossary_source)
-      end
-      terms
-    end
-
     def projects
       (imported_glossaries | registered_projects).sort.map do |project_path|
-        Logaling::Project.new(project_path)
+        Logaling::Project.new(project_path, self)
       end
     end
 
@@ -162,6 +152,10 @@ module Logaling
       project = projects.detect{|project| project.name == project_name}
     end
 
+    def logaling_db_home
+      File.join(@path, "db")
+    end
+
     private
     def get_glossary(path)
       glossary_name, source_language, target_language = File::basename(path, ".*").split(".")
@@ -172,10 +166,6 @@ module Logaling
       %w(yml tsv csv).map{|type| File.join(path, "*.#{type}") }
     end
 
-    def logaling_db_home
-      File.join(@path, "db")
-    end
-
     def logaling_projects_path
       File.join(@path, "projects")
     end




More information about the logaling-commit mailing list
Back to archive index