null+****@clear*****
null+****@clear*****
Tue Jun 19 21:31:15 JST 2012
SHIMADA Koji 2012-06-19 21:31:15 +0900 (Tue, 19 Jun 2012) New Revision: 2c35270448b7c5a4095bd06d4a54fea20f1d4ba3 https://github.com/logaling/logaling-command/commit/2c35270448b7c5a4095bd06d4a54fea20f1d4ba3 Merged 7228757: Merge pull request #60 from logaling/refactor-structure Log: Replace constructor of glossary source with Factory Mehthod Added files: lib/logaling/glossary_sources/base.rb lib/logaling/glossary_sources/glossary_csv_source.rb lib/logaling/glossary_sources/glossary_tsv_source.rb Copied files: lib/logaling/glossary_sources/glossary_yaml_source.rb (from lib/logaling/glossary_source.rb) Modified files: lib/logaling/glossary.rb lib/logaling/glossary_db.rb lib/logaling/glossary_source.rb lib/logaling/project.rb spec/logaling/glossary_spec.rb Modified: lib/logaling/glossary.rb (+2 -2) =================================================================== --- lib/logaling/glossary.rb 2012-06-19 20:08:22 +0900 (c235337) +++ lib/logaling/glossary.rb 2012-06-19 21:31:15 +0900 (57d20c1) @@ -71,7 +71,7 @@ module Logaling source_dir =****@proje*****_source_path FileUtils.mkdir_p(source_dir) source_path = File.join(source_dir, file_name) - Logaling::GlossarySource.new(source_path, self) + @glossary_source = Logaling::GlossarySource.create(source_path, self) end end @@ -102,7 +102,7 @@ module Logaling file_name = [self.to_s, type].join('.') File.join(@project.glossary_source_path, file_name) end - Dir.glob(glob_condition).map {|source_path| GlossarySource.new(source_path, self)} + Dir.glob(glob_condition).map {|source_path| GlossarySource.create(source_path, self)} end end end Modified: lib/logaling/glossary_db.rb (+3 -3) =================================================================== --- lib/logaling/glossary_db.rb 2012-06-19 20:08:22 +0900 (9557c05) +++ lib/logaling/glossary_db.rb 2012-06-19 21:31:15 +0900 (a828833) @@ -96,7 +96,7 @@ module Logaling add_glossary_source(glossary_source) add_glossary(glossary) - GlossarySource.load(glossary_source.source_path).each do |term| + glossary_source.load.each do |term| source_term = term['source_term'] target_term = term['target_term'] note = term['note'] @@ -235,7 +235,7 @@ module Logaling source_paths.map do |source_path| glossary_name, source_language, target_language = File.basename(source_path).split(/\./) glossary = Glossary.new(glossary_name, source_language, target_language) - GlossarySource.new(source_path, glossary) + GlossarySource.create(source_path, glossary) end end @@ -243,7 +243,7 @@ module Logaling records = Groonga["glossary_sources"].select do |record| [record.key =~ glossary.to_s] end - records.map{|record| GlossarySource.new(record.key.key, glossary) } + records.map{|record| GlossarySource.create(record.key.key, glossary) } end def get_all_glossary Modified: lib/logaling/glossary_source.rb (+11 -154) =================================================================== --- lib/logaling/glossary_source.rb 2012-06-19 20:08:22 +0900 (08d11dd) +++ lib/logaling/glossary_source.rb 2012-06-19 21:31:15 +0900 (65c1611) @@ -15,163 +15,20 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see <http://www.gnu.org/licenses/>. -begin - require 'psych' -rescue LoadError => e - raise LoadError unless e.message =~ /psych/ - puts "please install psych first." -end -require "yaml" -require "csv" -require "fileutils" +require "logaling/glossary_sources/glossary_yaml_source" +require "logaling/glossary_sources/glossary_csv_source" +require "logaling/glossary_sources/glossary_tsv_source" module Logaling class GlossarySource - class << self - def load(file) - load_glossary_source(file) - end - - def load_glossary_source(file) - case File.extname(file) - when ".csv" - load_glossary_source_csv(file) - when ".tsv" - load_glossary_source_tsv(file) - when ".yml" - load_glossary_source_yml(file) - end - end - - def load_glossary_source_yml(path) - YAML::load_file(path) || [] - end - - def load_glossary_source_tsv(path) - load_glossary_source_csv(path, "\t") - end - - def load_glossary_source_csv(path, sep=",") - glossary_source = [] - CSV.open(path, "r:utf-8", {:col_sep => sep}) do |csv| - csv.each do |row| - glossary_source << {"source_term" => row[0], "target_term" => row[1], "note" => ""} if row.size >= 2 - end - end - glossary_source - end - end - attr_reader :source_path, :glossary - - def initialize(source_path, glossary) - @source_path = source_path - @glossary = glossary - end - - def add(source_term, target_term, note) - FileUtils.touch(source_path) unless File.exist?(source_path) - - glossary_source = GlossarySource.load_glossary_source(source_path) - glossary_source << build_term(source_term, target_term, note) - dump_glossary_source(glossary_source) - rescue - raise GlossaryNotFound - end - - def update(source_term, target_term, new_target_term, note) - raise GlossaryNotFound unless File.exist?(source_path) - - glossary_source = GlossarySource.load_glossary_source(source_path) - - target_index = find_term_index(glossary_source, source_term, target_term) - if target_index - glossary_source[target_index] = rebuild_term(glossary_source[target_index], source_term, new_target_term, note) - dump_glossary_source(glossary_source) - else - raise TermError, "Can't found term '#{source_term}: #{target_term}' in '#{@glossary.name}'" - end - end - - def delete(source_term, target_term) - raise GlossaryNotFound unless File.exist?(source_path) - - glossary_source = GlossarySource.load_glossary_source(source_path) - target_index = find_term_index(glossary_source, source_term, target_term) - unless target_index - raise TermError, "Can't found term '#{source_term} #{target_term}' in '#{@glossary.name}'" unless target_index - end - - glossary_source.delete_at(target_index) - dump_glossary_source(glossary_source) - end - - def delete_all(source_term, force=false) - raise GlossaryNotFound unless File.exist?(source_path) - - glossary_source = GlossarySource.load_glossary_source(source_path) - delete_candidates = target_terms(glossary_source, source_term) - if delete_candidates.empty? - raise TermError, "Can't found term '#{source_term} in '#{@glossary.name}'" - end - - if delete_candidates.size == 1 || force - glossary_source.delete_if{|term| term['source_term'] == source_term } - dump_glossary_source(glossary_source) - else - raise TermError, "There are duplicate terms in glossary.\n" + - "If you really want to delete, please put `loga delete [SOURCE_TERM] --force`\n" + - " or `loga delete [SOURCE_TERM] [TARGET_TERM]`" - end - end - - def load - GlossarySource.load_glossary_source(@source_path) - end - - def eql?(other) - return false unless self.class == other.class - @source_path == other.source_path - end - - def hash - @source_path.hash - end - - def mtime - File.mtime(@source_path) - end - - private - def build_term(source_term, target_term, note) - note ||= '' - {'source_term' => source_term, 'target_term' => target_term, 'note' => note} - end - - def rebuild_term(current, source_term, target_term, note) - if current['target_term'] != target_term && (note.nil? || note == "") - note = current['note'] - end - target_term = current['target_term'] if target_term == "" - build_term(source_term, target_term, note) - end - - def find_term_index(glossary_source, source_term, target_term='') - glossary_source.find_index do |term| - if target_term.empty? - term['source_term'] == source_term - else - term['source_term'] == source_term && term['target_term'] == target_term - end - end - end - - def target_terms(glossary_source, source_term) - glossary_source.select {|term| term['source_term'] == source_term } - end - - def dump_glossary_source(glossary_source) - File.open(source_path, "w") do |f| - f.puts(glossary_source.to_yaml) + def self.create(source_path, glossary) + case File.extname(source_path) + when ".csv" + GlossarySources::GlossaryCsvSource.new(source_path, glossary) + when ".tsv" + GlossarySources::GlossaryTsvSource.new(source_path, glossary) + when ".yml" + GlossarySources::GlossaryYamlSource.new(source_path, glossary) end end end Added: lib/logaling/glossary_sources/base.rb (+42 -0) 100644 =================================================================== --- /dev/null +++ lib/logaling/glossary_sources/base.rb 2012-06-19 21:31:15 +0900 (9dfced5) @@ -0,0 +1,42 @@ +# -*- coding: utf-8 -*- +# +# Copyright (C) 2012 Koji SHIMADA <koji.****@enish*****> +# +# 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 + module GlossarySources + class Base + attr_reader :source_path, :glossary + + def initialize(source_path, glossary) + @source_path = source_path + @glossary = glossary + end + + def eql?(other) + return false unless self.class == other.class + @source_path == other.source_path + end + + def hash + @source_path.hash + end + + def mtime + File.mtime(@source_path) + end + end + end +end Added: lib/logaling/glossary_sources/glossary_csv_source.rb (+33 -0) 100644 =================================================================== --- /dev/null +++ lib/logaling/glossary_sources/glossary_csv_source.rb 2012-06-19 21:31:15 +0900 (a111bc2) @@ -0,0 +1,33 @@ +# -*- coding: utf-8 -*- +# +# Copyright (C) 2012 Koji SHIMADA <koji.****@enish*****> +# +# 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/>. + +require "logaling/glossary_sources/base" +require "csv" + +module Logaling::GlossarySources + class GlossaryCsvSource < Base + def load + glossary_source = [] + CSV.open(source_path, "r:utf-8", {:col_sep => ','}) do |csv| + csv.each do |row| + glossary_source << {"source_term" => row[0], "target_term" => row[1], "note" => ""} if row.size >= 2 + end + end + glossary_source + end + end +end Added: lib/logaling/glossary_sources/glossary_tsv_source.rb (+33 -0) 100644 =================================================================== --- /dev/null +++ lib/logaling/glossary_sources/glossary_tsv_source.rb 2012-06-19 21:31:15 +0900 (44f6a48) @@ -0,0 +1,33 @@ +# -*- coding: utf-8 -*- +# +# Copyright (C) 2012 Koji SHIMADA <koji.****@enish*****> +# +# 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/>. + +require "logaling/glossary_sources/base" +require "csv" + +module Logaling::GlossarySources + class GlossaryTsvSource < Base + def load + glossary_source = [] + CSV.open(source_path, "r:utf-8", {:col_sep => "\t"}) do |tsv| + tsv.each do |row| + glossary_source << {"source_term" => row[0], "target_term" => row[1], "note" => ""} if row.size >= 2 + end + end + glossary_source + end + end +end Copied: lib/logaling/glossary_sources/glossary_yaml_source.rb (+18 -73) 59% =================================================================== --- lib/logaling/glossary_source.rb 2012-06-19 20:08:22 +0900 (08d11dd) +++ lib/logaling/glossary_sources/glossary_yaml_source.rb 2012-06-19 21:31:15 +0900 (2fa0110) @@ -1,6 +1,6 @@ # -*- coding: utf-8 -*- # -# Copyright (C) 2011 Miho SUZUKI +# Copyright (C) 2012 Koji SHIMADA <koji.****@enish*****> # # 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 @@ -15,6 +15,7 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see <http://www.gnu.org/licenses/>. +require "logaling/glossary_sources/base" begin require 'psych' rescue LoadError => e @@ -22,83 +23,44 @@ rescue LoadError => e puts "please install psych first." end require "yaml" -require "csv" require "fileutils" -module Logaling - class GlossarySource - class << self - def load(file) - load_glossary_source(file) - end - - def load_glossary_source(file) - case File.extname(file) - when ".csv" - load_glossary_source_csv(file) - when ".tsv" - load_glossary_source_tsv(file) - when ".yml" - load_glossary_source_yml(file) - end - end - - def load_glossary_source_yml(path) - YAML::load_file(path) || [] - end - - def load_glossary_source_tsv(path) - load_glossary_source_csv(path, "\t") - end - - def load_glossary_source_csv(path, sep=",") - glossary_source = [] - CSV.open(path, "r:utf-8", {:col_sep => sep}) do |csv| - csv.each do |row| - glossary_source << {"source_term" => row[0], "target_term" => row[1], "note" => ""} if row.size >= 2 - end - end - glossary_source - end - end - attr_reader :source_path, :glossary - - def initialize(source_path, glossary) - @source_path = source_path - @glossary = glossary +module Logaling::GlossarySources + class GlossaryYamlSource < Base + def load + YAML::load_file(source_path) || [] end def add(source_term, target_term, note) FileUtils.touch(source_path) unless File.exist?(source_path) - glossary_source = GlossarySource.load_glossary_source(source_path) + glossary_source = self.load glossary_source << build_term(source_term, target_term, note) dump_glossary_source(glossary_source) rescue - raise GlossaryNotFound + raise Logaling::GlossaryNotFound end def update(source_term, target_term, new_target_term, note) - raise GlossaryNotFound unless File.exist?(source_path) - - glossary_source = GlossarySource.load_glossary_source(source_path) + raise Logaling::GlossaryNotFound unless File.exist?(source_path) + glossary_source = self.load target_index = find_term_index(glossary_source, source_term, target_term) if target_index glossary_source[target_index] = rebuild_term(glossary_source[target_index], source_term, new_target_term, note) dump_glossary_source(glossary_source) else - raise TermError, "Can't found term '#{source_term}: #{target_term}' in '#{@glossary.name}'" + raise Logaling::TermError, "Can't found term '#{source_term}: #{target_term}' in '#{@glossary.name}'" end end def delete(source_term, target_term) - raise GlossaryNotFound unless File.exist?(source_path) + raise Logaling::GlossaryNotFound unless File.exist?(source_path) - glossary_source = GlossarySource.load_glossary_source(source_path) + glossary_source = self.load target_index = find_term_index(glossary_source, source_term, target_term) unless target_index - raise TermError, "Can't found term '#{source_term} #{target_term}' in '#{@glossary.name}'" unless target_index + raise Logaling::TermError, "Can't found term '#{source_term} #{target_term}' in '#{@glossary.name}'" unless target_index end glossary_source.delete_at(target_index) @@ -106,41 +68,24 @@ module Logaling end def delete_all(source_term, force=false) - raise GlossaryNotFound unless File.exist?(source_path) + raise Logaling::GlossaryNotFound unless File.exist?(source_path) - glossary_source = GlossarySource.load_glossary_source(source_path) + glossary_source = self.load delete_candidates = target_terms(glossary_source, source_term) if delete_candidates.empty? - raise TermError, "Can't found term '#{source_term} in '#{@glossary.name}'" + raise Logaling::TermError, "Can't found term '#{source_term} in '#{@glossary.name}'" end if delete_candidates.size == 1 || force glossary_source.delete_if{|term| term['source_term'] == source_term } dump_glossary_source(glossary_source) else - raise TermError, "There are duplicate terms in glossary.\n" + + raise Logaling::TermError, "There are duplicate terms in glossary.\n" + "If you really want to delete, please put `loga delete [SOURCE_TERM] --force`\n" + " or `loga delete [SOURCE_TERM] [TARGET_TERM]`" end end - def load - GlossarySource.load_glossary_source(@source_path) - end - - def eql?(other) - return false unless self.class == other.class - @source_path == other.source_path - end - - def hash - @source_path.hash - end - - def mtime - File.mtime(@source_path) - end - private def build_term(source_term, target_term, note) note ||= '' Modified: lib/logaling/project.rb (+2 -2) =================================================================== --- lib/logaling/project.rb 2012-06-19 20:08:22 +0900 (0568e6e) +++ lib/logaling/project.rb 2012-06-19 21:31:15 +0900 (3670bbe) @@ -42,7 +42,7 @@ module Logaling all_glossary_source_path = Dir.glob(File.join(glossary_source_path, "*")) all_glossary_source_path.map do |source_path| name, source_language, target_language, type = File.basename(source_path).split(/\./) - GlossarySource.new(source_path, find_glossary(source_language, target_language)) + GlossarySource.create(source_path, find_glossary(source_language, target_language)) end end end @@ -54,7 +54,7 @@ module Logaling def glossary_sources name, source_language, target_language, type = File.basename(@path).split(/\./) - [GlossarySource.new(@path, find_glossary(source_language, target_language))] + [GlossarySource.create(@path, find_glossary(source_language, target_language))] end end end Modified: spec/logaling/glossary_spec.rb (+1 -1) =================================================================== --- spec/logaling/glossary_spec.rb 2012-06-19 20:08:22 +0900 (5566383) +++ spec/logaling/glossary_spec.rb 2012-06-19 21:31:15 +0900 (97ffdd5) @@ -152,7 +152,7 @@ module Logaling glossary.add("delete_logaling", "てすと1", "備考") glossary.add("delete_logaling", "てすと2", "備考") glossary.delete_all("delete_logaling", true) - @result = Logaling::GlossarySource.load_glossary_source_yml(glossary_source_path) + @result = Logaling::GlossarySource.create(glossary_source_path, glossary).load end it { -------------- next part -------------- An HTML attachment was scrubbed... 다운로드