null+****@clear*****
null+****@clear*****
Sat Feb 18 11:06:10 JST 2012
SHIMADA Koji 2012-02-18 11:06:10 +0900 (Sat, 18 Feb 2012) New Revision: b79d0b561817ec000f0aa2a9097e318f28a5eca8 Log: Move class methods of Config to instance method Modified files: lib/logaling/command/application.rb lib/logaling/config.rb Modified: lib/logaling/command/application.rb (+10 -6) =================================================================== --- lib/logaling/command/application.rb 2012-02-17 22:18:48 +0900 (b6e6b54) +++ lib/logaling/command/application.rb 2012-02-18 11:06:10 +0900 (1511c94) @@ -30,11 +30,13 @@ module Logaling::Command super @logaling_home = options["logaling-home"] ? options["logaling-home"] : LOGALING_HOME @repository = Logaling::Repository.new(@logaling_home) + @config = Logaling::Config.load(@repository.config_path) + @dotfile_path = options["logaling-config"] ? options["logaling-config"] : find_dotfile @project_config_path = File.join(@dotfile_path, '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) + @config.load(@project_config_path) + ensure + @config.merge(options) end map '-a' => :add, @@ -61,7 +63,8 @@ module Logaling::Command unless File.exist?(logaling_config_path) FileUtils.mkdir_p(File.join(logaling_config_path, "glossary")) - config = Logaling::Config.setup(project_name, source_language, target_language) + config = Logaling::Config.new("glossary" => project_name, "source-language" => source_language) + config.merge("target-language" => target_language) if target_language config.save(File.join(logaling_config_path, "config")) register unless options["no-register"] @@ -118,8 +121,9 @@ module Logaling::Command method_option "global", type: :boolean, default: false def config(key, value) config_path = options["global"] ? File.join(@logaling_home, "config") : @project_config_path - FileUtils.touch(config_path) unless File.exist?(config_path) - Logaling::Config.add(config_path, key, value) + config = Logaling::Config.load(config_path) + config.add(key, value) + config.save(config_path) say "Successfully set config." rescue Logaling::CommandFailed => e say e.message Modified: lib/logaling/config.rb (+28 -46) =================================================================== --- lib/logaling/config.rb 2012-02-17 22:18:48 +0900 (abd45d0) +++ lib/logaling/config.rb 2012-02-18 11:06:10 +0900 (953127d) @@ -16,52 +16,9 @@ module Logaling class Config class << self - def setup(project_name, source_language, target_language) - config = {"glossary" => project_name, "source-language" => source_language} - config["target-language"] = target_language if target_language - new(config) - end - - def add(config_path, key, value) - raise Logaling::CommandFailed, "#{key} is unsupported option" unless support?(key) - - config = load_config_and_merge_options(nil, config_path, {key => value}) - config.save(config_path) - end - - def load_config_and_merge_options(project_config_path, global_config_path, options) - project_config = project_config_path ? load_config(project_config_path) : load_config(global_config_path) - global_config = load_config(global_config_path) - - config = merge_options(project_config, global_config) - config = merge_options(options, 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 load_config(config_path=nil) - config ||= {} - if config_path && File.exist?(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 + def load(config_path) + config = new + config.load(config_path) config end end @@ -76,6 +33,27 @@ module Logaling end end + def merge(config) + keys.each do |key| + @config[key] = config[key] if config[key] + end + end + + def load(config_path=nil) + if config_path && File.exist?(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 + end + + def add(key, value) + raise Logaling::CommandFailed, "#{key} is unsupported option" unless support?(key) + merge(key => value) + end + def save(config_path) File.open(config_path, 'w') do |fp| keys.each do |key| @@ -100,5 +78,9 @@ module Logaling def keys %w(glossary source-language target-language) end + + def support?(key) + keys.include?(key) + end end end