null+****@clear*****
null+****@clear*****
Wed Feb 8 14:11:50 JST 2012
SHIMADA Koji 2012-02-08 14:11:50 +0900 (Wed, 08 Feb 2012) New Revision: 05ef75e80ef9c5ca40dc8d3e5b5b639430272af4 Merged 94f43d2: Merge pull request #38 from logaling/reduce-duplication Log: Use #check_required_option for check required options - remove duplication: calling #load_config_and_merge_options Modified files: lib/logaling/command/application.rb Modified: lib/logaling/command/application.rb (+24 -24) =================================================================== --- lib/logaling/command/application.rb 2012-02-08 14:00:30 +0900 (263a390) +++ lib/logaling/command/application.rb 2012-02-08 14:11:50 +0900 (94f62e7) @@ -85,31 +85,29 @@ module Logaling::Command def register logaling_path = find_dotfile - required_options = {"glossary" => "input glossary name '-g <glossary name>'"} - config = load_config_and_merge_options(required_options) + check_required_option(@config, {"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 - required_options = {"glossary" => "input glossary name '-g <glossary name>'"} - config = load_config_and_merge_options(required_options) + check_required_option(@config, {"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.' @@ -131,11 +129,10 @@ module Logaling::Command 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****@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) @@ -159,11 +156,10 @@ module Logaling::Command 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****@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) @@ -214,9 +210,9 @@ module Logaling::Command "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) + check_required_option(@config, 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 @@ -226,7 +222,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 +256,8 @@ module Logaling::Command "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) - @glossary = Logaling::Glossary.new(config["glossary"], config["source-language"], config["target-language"]) + check_required_option(@config, required_options) + @glossary = Logaling::Glossary.new(@config["glossary"], @config["source-language"], @config["target-language"]) end end @@ -270,6 +266,12 @@ 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(required={}) config_list ||= {} find_config.each{|type, path| config_list[type] = load_config(path)} @@ -279,9 +281,7 @@ module Logaling::Command config = merge_options(project_config, global_config) config = merge_options(options, config) - required.each do |required_option, message| - raise(Logaling::CommandFailed, message) unless config[required_option] - end + check_required_option(config, required) config end