null+****@clear*****
null+****@clear*****
Thu Jan 12 11:52:39 JST 2012
SUZUKI Miho 2012-01-12 11:52:39 +0900 (Thu, 12 Jan 2012) New Revision: b1e36f56ddab9181d41093b80d17a2157264484f Log: [#12] improve checking duplicate Modified files: lib/logaling/command.rb lib/logaling/glossary.rb lib/logaling/glossary_db.rb lib/logaling/repository.rb spec/logaling/glossary_spec.rb Modified: lib/logaling/command.rb (+14 -0) =================================================================== --- lib/logaling/command.rb 2012-01-10 10:15:40 +0900 (5cbc52a) +++ lib/logaling/command.rb 2012-01-12 11:52:39 +0900 (d9988de) @@ -118,6 +118,13 @@ class Logaling::Command < Thor desc 'add [SOURCE TERM] [TARGET TERM] [NOTE(optional)]', 'Add term to glossary.' def add(source_term, target_term, note='') + config = load_config_and_merge_options + repository.index + + if repository.bilingual_pair_exists?(source_term, target_term, config["glossary"]) + raise Logaling::TermError, "term '#{source_term}: #{target_term}' already exists in '#{config["glossary"]}'" + end + glossary.add(source_term, target_term, note) rescue Logaling::CommandFailed, Logaling::TermError => e say e.message @@ -139,6 +146,13 @@ class Logaling::Command < Thor desc 'update [SOURCE TERM] [TARGET TERM] [NEW TARGET TERM], [NOTE(optional)]', 'Update term.' def update(source_term, target_term, new_target_term, note='') + config = load_config_and_merge_options + repository.index + + if repository.bilingual_pair_exists?(source_term, new_target_term, config["glossary"]) + raise Logaling::TermError, "term '#{source_term}: #{new_target_term}' already exists in '#{config["glossary"]}'" + end + glossary.update(source_term, target_term, new_target_term, note) rescue Logaling::CommandFailed, Logaling::TermError => e say e.message Modified: lib/logaling/glossary.rb (+0 -11) =================================================================== --- lib/logaling/glossary.rb 2012-01-10 10:15:40 +0900 (53898c8) +++ lib/logaling/glossary.rb 2012-01-12 11:52:39 +0900 (e1e9108) @@ -73,10 +73,6 @@ module Logaling FileUtils.touch(@path) unless File.exist?(@path) glossary = Glossary.load_glossary(@path) - if bilingual_pair_exists?(glossary, source_term, target_term) - raise TermError, "term '#{source_term}: #{target_term}' already exists in '#{@glossary}'" - end - glossary << build_term(source_term, target_term, note) dump_glossary(glossary) end @@ -85,9 +81,6 @@ module Logaling raise GlossaryNotFound unless File.exist?(@path) glossary = Glossary.load_glossary(@path) - if bilingual_pair_exists?(glossary, source_term, new_target_term) - raise TermError, "term '#{source_term}: #{target_term}' already exists in '#{@glossary}'" - end target_index = find_term_index(glossary, source_term, target_term) if target_index @@ -152,10 +145,6 @@ module Logaling end end - def bilingual_pair_exists?(glossary, source_term, target_term) - target_terms(glossary, source_term).any?{|data| data['target_term'] == target_term } - end - def target_terms(glossary, source_term) glossary.select {|term| term['source_term'] == source_term } end Modified: lib/logaling/glossary_db.rb (+21 -0) =================================================================== --- lib/logaling/glossary_db.rb 2012-01-10 10:15:40 +0900 (6a9119e) +++ lib/logaling/glossary_db.rb 2012-01-12 11:52:39 +0900 (477f1c0) @@ -119,6 +119,27 @@ module Logaling end end + def get_bilingual_pair(source_term, target_term, glossary) + records = Groonga["glossaries"].select do |record| + [ + record.name == glossary, + record.source_term == source_term, + record.target_term == target_term + ] + end + + 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 (+15 -0) =================================================================== --- lib/logaling/repository.rb 2012-01-10 10:15:40 +0900 (f430e77) +++ lib/logaling/repository.rb 2012-01-12 11:52:39 +0900 (e2f3719) @@ -106,6 +106,21 @@ module Logaling File.exist?(path) ? path : nil end + def bilingual_pair_exists?(source_term, target_term, glossary) + raise GlossaryDBNotFound unless File.exist?(logaling_db_home) + + terms = [] + Logaling::GlossaryDB.open(logaling_db_home, "utf8") do |db| + terms = db.get_bilingual_pair(source_term, target_term, glossary) + end + + if terms.size > 0 + true + else + false + end + end + private def latest_index? if File.exist?(index_at_file) Modified: spec/logaling/glossary_spec.rb (+0 -10) =================================================================== --- spec/logaling/glossary_spec.rb 2012-01-10 10:15:40 +0900 (33d3ca8) +++ spec/logaling/glossary_spec.rb 2012-01-12 11:52:39 +0900 (9812e81) @@ -43,16 +43,6 @@ module Logaling end end - context 'with arguments show existing bilingual pair' do - before do - glossary.add("user", "ユーザ", "ユーザーではない") - end - - it { - -> { glossary.add("user", "ユーザ", "ユーザーではない") }.should raise_error(Logaling::TermError) - } - end - context "when the glossary not found" do before do glossary.add("test", "テスト", "テスト")