null+****@clear*****
null+****@clear*****
Tue Feb 14 18:59:33 JST 2012
SHIMADA Koji 2012-02-14 18:59:33 +0900 (Tue, 14 Feb 2012) New Revision: 7713c7b527be01d2b05631383f656cfefcb6cf08 Log: Extract Logaling::Config from Command::Application Added files: lib/logaling/config.rb Modified files: lib/logaling/command/application.rb Modified: lib/logaling/command/application.rb (+29 -93) =================================================================== --- lib/logaling/command/application.rb 2012-02-14 18:16:01 +0900 (8c9f5bc) +++ lib/logaling/command/application.rb 2012-02-14 18:59:33 +0900 (5b96d74) @@ -19,6 +19,7 @@ require 'thor' require 'rainbow' require "logaling/repository" require "logaling/glossary" +require "logaling/config" module Logaling::Command LOGALING_CONFIG = '.logaling' @@ -28,9 +29,13 @@ module Logaling::Command def initialize(*args) super @repository = Logaling::Repository.new(LOGALING_HOME) - @config = load_config_and_merge_options - @source_language = @config["source-language"] - @target_language = @config["target-language"] + project_config_path = File.join(find_dotfile, 'config') + @config = Logaling::Config.load_config_and_merge_options(project_config_path, @repository.config_path, options) + rescue Logaling::CommandFailed + @config = Logaling::Config.load_config_and_merge_options(nil, @repository.config_path, options) + ensure + @source_language =****@confi*****_language + @target_language =****@confi*****_language end map '-a' => :add, @@ -57,7 +62,7 @@ module Logaling::Command FileUtils.mkdir_p(File.join(LOGALING_CONFIG, "glossary")) config = {"glossary" => project_name, "source-language" => source_language} config["target-language"] = target_language if target_language - write_config(File.join(LOGALING_CONFIG, "config"), config) + Logaling::Config.new(config).write_config(File.join(LOGALING_CONFIG, "config")) register unless options["no-register"] say "Successfully created #{LOGALING_CONFIG}" @@ -85,43 +90,37 @@ module Logaling::Command def register logaling_path = find_dotfile - check_required_option(@config, {"glossary" => "input glossary name '-g <glossary name>'"}) + @config.check_required_option("glossary" => "input glossary name '-g <glossary name>'") - @repository.register(logaling_path, @config["glossary"]) + @repository.register(logaling_path, @config.glossary) @repository.index - say "#{@config['glossary']} is now registered to logaling." + say "#{@config.glossary} is now registered to logaling." rescue Logaling::CommandFailed => e say e.message say "Try 'loga new' first." rescue Logaling::GlossaryAlreadyRegistered => e - say "#{@config['glossary']} is already registered." + say "#{@config.glossary} is already registered." end desc 'unregister', 'Unregister .logaling' def unregister - check_required_option(@config, {"glossary" => "input glossary name '-g <glossary name>'"}) + @config.check_required_option("glossary" => "input glossary name '-g <glossary name>'") - @repository.unregister(@config["glossary"]) + @repository.unregister(@config.glossary) @repository.index - say "#{@config['glossary']} is now unregistered." + say "#{@config.glossary} is now unregistered." rescue Logaling::CommandFailed => e say e.message rescue Logaling::GlossaryNotFound => e - say "#{@config['glossary']} is not yet registered." + say "#{@config.glossary} is not yet registered." end desc 'config [KEY] [VALUE] [--global(optional)]', 'Set config.' method_option "global", type: :boolean, default: false def config(key, value) - support_keys = %w(glossary source-language target-language) - raise Logaling::CommandFailed, "#{key} is unsupported option" unless support_keys.include?(key) - config_path = options["global"] ? File.join(LOGALING_HOME, "config") : File.join(find_dotfile, "config") FileUtils.touch(config_path) unless File.exist?(config_path) - - config = load_config(config_path) - config = merge_options({key => value}, config) - write_config(config_path, config) + Logaling::Config.add(config_path, key, value) say "Successfully set config." rescue Logaling::CommandFailed => e say e.message @@ -131,8 +130,8 @@ module Logaling::Command def add(source_term, target_term, note='') @repository.index - if****@repos*****_pair_exists?(source_term, target_term, @config["glossary"]) - raise Logaling::TermError, "term '#{source_term}: #{target_term}' already exists in '#{@config["glossary"]}'" + if****@repos*****_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) @@ -158,8 +157,8 @@ module Logaling::Command def update(source_term, target_term, new_target_term, note='') @repository.index - if****@repos*****_pair_exists_and_has_same_note?(source_term, new_target_term, note, @config["glossary"]) - raise Logaling::TermError, "term '#{source_term}: #{new_target_term}' already exists in '#{@config["glossary"]}'" + if****@repos*****_pair_exists_and_has_same_note?(source_term, new_target_term, note, @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) @@ -175,7 +174,7 @@ module Logaling::Command def lookup(source_term) @repository.index terms =****@repos*****(source_term, @source_language, @target_language, - @config["glossary"]) + @config.glossary) unless terms.empty? max_str_size = terms.map{|term| term[:source_term].size}.sort.last run_pager @@ -186,7 +185,7 @@ module Logaling::Command glossary_name = "" if****@repos*****_counts > 1 glossary_name = term[:glossary_name] - if term[:glossary_name] == @config["glossary"] + if term[:glossary_name] ==****@confi***** glossary_name = glossary_name.foreground(:white).background(:green) end end @@ -213,9 +212,9 @@ module Logaling::Command "source-language" => "input source-language code '-S <source-language code>'", "target-language" => "input target-language code '-T <target-language code>'" } - check_required_option(@config, required_options) + @config.check_required_option(required_options) @repository.index - terms =****@repos*****_glossary(@config["glossary"], @config["source-language"], @config["target-language"]) + terms =****@repos*****_glossary(@config.glossary, @config.source_language, @config.target_language) unless terms.empty? run_pager max_str_size = terms.map{|term| term[:source_term].size}.sort.last @@ -225,7 +224,7 @@ module Logaling::Command printf(" %-#{max_str_size+10}s %s\n", term[:source_term], target_string) end else - "glossary <#{@config['glossary']}> not found" + "glossary <#{@config.glossary}> not found" end rescue Logaling::CommandFailed, Logaling::GlossaryDBNotFound => e @@ -260,8 +259,8 @@ module Logaling::Command "source-language" => "input source-language code '-S <source-language code>'", "target-language" => "input target-language code '-T <target-language code>'" } - check_required_option(@config, required_options) - @glossary = Logaling::Glossary.new(@config["glossary"], @config["source-language"], @config["target-language"]) + @config.check_required_option(required_options) + @glossary = Logaling::Glossary.new(@config.glossary, @config.source_language, @config.target_language) end end @@ -270,56 +269,6 @@ module Logaling::Command exit 1 end - def check_required_option(config, required={}) - required.each do |required_option, message| - raise(Logaling::CommandFailed, message) unless config[required_option] - end - end - - def load_config_and_merge_options - config_list ||= {} - find_config.each{|type, path| config_list[type] = load_config(path)} - global_config = config_list["global_config"] ? config_list["global_config"] : {} - project_config = config_list["project_config"] ? config_list["project_config"] : {} - - config = merge_options(project_config, global_config) - config = merge_options(options, config) - - config - end - - def merge_options(options, secondary_options) - config ||={} - config["glossary"] = options["glossary"] ? options["glossary"] : secondary_options["glossary"] - config["source-language"] = options["source-language"] ? options["source-language"] : secondary_options["source-language"] - config["target-language"] = options["target-language"] ? options["target-language"] : secondary_options["target-language"] - config - end - - def find_config - config ||= {} - config["project_config"] = File.join(find_dotfile, 'config') - config["global_config"] = global_config_path if global_config_path - config - rescue Logaling::CommandFailed - config ||= {} - config["project_config"] =****@repos*****_path if****@repos*****_path - config["global_config"] = global_config_path if global_config_path - config - end - - def load_config(config_path=nil) - config ||= {} - if config_path - File.readlines(config_path).map{|l| l.chomp.split " "}.each do |option| - key = option[0].sub(/^[\-]{2}/, "") - value = option[1] - config[key] = value - end - end - config - end - def find_dotfile dir = Dir.pwd searched_path = [] @@ -338,19 +287,6 @@ module Logaling::Command end end - def global_config_path - path = File.join(LOGALING_HOME, "config") - File.exist?(path) ? path : nil - end - - def write_config(config_path, config) - File.open(config_path, 'w') do |fp| - fp.puts "--glossary #{config['glossary']}" if config['glossary'] - fp.puts "--source-language #{config['source-language']}" if config['source-language'] - fp.puts "--target-language #{config['target-language']}" if config['target-language'] - end - end - # http://nex-3.com/posts/73-git-style-automatic-paging-in-ruby def run_pager return if options["no-pager"] Added: lib/logaling/config.rb (+99 -0) 100644 =================================================================== --- /dev/null +++ lib/logaling/config.rb 2012-02-14 18:59:33 +0900 (c227bcf) @@ -0,0 +1,99 @@ +# 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 + class Config + class << self + def add(config_path, key, value) + raise Logaling::CommandFailed, "#{key} is unsupported option" unless support?(key) + + config = load_config(config_path) + config = merge_options({key => value}, config) + Config.new(config).write_config(config_path) + end + + def load_config_and_merge_options(project_config_path, global_config_path, options) + config_list ||= {} + find_config(project_config_path, global_config_path).each{|type, path| config_list[type] = load_config(path)} + global_config = config_list["global_config"] ? config_list["global_config"] : {} + project_config = config_list["project_config"] ? config_list["project_config"] : {} + + config = merge_options(project_config, global_config) + config = merge_options(options, config) + + Config.new(config) + end + + private + def support?(key) + support_keys = %w(glossary source-language target-language) + support_keys.include?(key) + end + + def merge_options(options, secondary_options) + config ||={} + config["glossary"] = options["glossary"] ? options["glossary"] : secondary_options["glossary"] + config["source-language"] = options["source-language"] ? options["source-language"] : secondary_options["source-language"] + config["target-language"] = options["target-language"] ? options["target-language"] : secondary_options["target-language"] + config + end + + def find_config(project_config_path, global_config_path) + config ||= {} + if project_config_path + config["project_config"] = project_config_path + else + config["project_config"] = global_config_path + end + config["global_config"] = global_config_path if global_config_path + config + end + + def load_config(config_path=nil) + config ||= {} + if config_path + File.readlines(config_path).map{|l| l.chomp.split " "}.each do |option| + key = option[0].sub(/^[\-]{2}/, "") + value = option[1] + config[key] = value + end + end + config + end + end + attr_reader :glossary, :source_language, :target_language + + def initialize(config) + @glossary = config["glossary"] + @source_language = config["source-language"] + @target_language = config["target-language"] + @config = config + end + + def check_required_option(required={}) + required.each do |required_option, message| + raise(Logaling::CommandFailed, message) unless @config[required_option] + end + end + + def write_config(config_path) + File.open(config_path, 'w') do |fp| + fp.puts "--glossary #{@glossary}" if @glossary + fp.puts "--source-language #{@source_language}" if @source_language + fp.puts "--target-language #{@target_language}" if @target_language + end + end + end +end