null+****@clear*****
null+****@clear*****
Fri Jan 6 10:59:02 JST 2012
SUZUKI Miho 2012-01-06 10:59:02 +0900 (Fri, 06 Jan 2012) New Revision: bebab5c32f0417fc793999a11cce3eeb1966a049 Merged 9db2d20: Merge pull request #14 from logaling/add-list-command Log: [#10] add 'logaling list' command Modified files: lib/logaling/command.rb lib/logaling/glossary_db.rb lib/logaling/repository.rb Modified: lib/logaling/command.rb (+26 -0) =================================================================== --- lib/logaling/command.rb 2011-12-23 18:16:42 +0900 (ff0d609) +++ lib/logaling/command.rb 2012-01-06 10:59:02 +0900 (5cbc52a) @@ -176,6 +176,32 @@ class Logaling::Command < Thor say "logaling-command version #{Logaling::Command::VERSION}" end + desc 'list', 'Show list.' + def list + required_options = { + "glossary" => "input glossary name '-g <glossary name>'", + "source-language" => "input source-language code '-S <source-language code>'", + "target-language" => "input target-language code '-T <target-language code>'" + } + config = load_config_and_merge_options(required_options) + + repository.index + terms = repository.list(config["glossary"], config["source-language"], config["target-language"]) + unless terms.empty? + max_str_size = terms.map{|term| term[:source_term].size}.sort.last + terms.each do |term| + target_string = "#{term[:target_term]}" + target_string << "\t# #{term[:note]}" unless term[:note].empty? + printf(" %-#{max_str_size+10}s %s\n", term[:source_term], target_string) + end + else + "glossary <#{config['glossary']}> not found" + end + + rescue Logaling::CommandFailed, Logaling::TermError => e + say e.message + end + private def repository @repository ||= Logaling::Repository.new(LOGALING_HOME) Modified: lib/logaling/glossary_db.rb (+25 -0) =================================================================== --- lib/logaling/glossary_db.rb 2011-12-23 18:16:42 +0900 (7305d30) +++ lib/logaling/glossary_db.rb 2012-01-06 10:59:02 +0900 (6a9119e) @@ -94,6 +94,31 @@ module Logaling end end + def list(glossary, source_language, target_language) + records_raw = Groonga["glossaries"].select do |record| + [ + record.name == glossary, + record.source_language == source_language, + record.target_language == target_language + ] + end + + records = records_raw.sort([ + {:key=>"source_term", :order=>'ascending'}, + {:key=>"target_term", :order=>'ascending'}]) + + records.map do |record| + term = record.key + + {:name => term.name, + :source_language => term.source_language, + :target_language => term.target_language, + :source_term => term.source_term, + :target_term => term.target_term, + :note => term.note || ''} + end + end + private def add_glossary(name, source_language, target_language, source_term, target_term, note) Groonga["glossaries"].add(:name => name, Modified: lib/logaling/repository.rb (+10 -0) =================================================================== --- lib/logaling/repository.rb 2011-12-23 18:16:42 +0900 (79a5a59) +++ lib/logaling/repository.rb 2012-01-06 10:59:02 +0900 (f430e77) @@ -68,6 +68,16 @@ module Logaling terms end + def list(glossary, source_language, target_language) + raise GlossaryDBNotFound unless File.exist?(logaling_db_home) + + terms = [] + Logaling::GlossaryDB.open(logaling_db_home, "utf8") do |db| + terms = db.list(glossary, source_language, target_language) + end + terms + end + def index return if latest_index? projects = Dir[File.join(@path, "projects", "*")]