From febb46a32cf3da9e96823164c054b38c5420fa24 Mon Sep 17 00:00:00 2001 From: Pistos Date: Mon, 20 Apr 2015 22:23:08 -0400 Subject: [PATCH] Refactor: Extract Method: legitimize_config_filename! . --- lib/diakonos/config.rb | 34 ++++++++++++++++++++++++++-------- 1 file changed, 26 insertions(+), 8 deletions(-) diff --git a/lib/diakonos/config.rb b/lib/diakonos/config.rb index 0df9233f..cbfc2f77 100644 --- a/lib/diakonos/config.rb +++ b/lib/diakonos/config.rb @@ -188,21 +188,39 @@ def map_key( arg, keymap = @modes['edit'].keymap ) end end - # @param [String] filename_ the config file to parse - # @param [String] including_filename the config file which calls include on this one - # @return an Array of problem descriptions (Strings) - def parse_configuration_file( filename_, including_filename = nil ) + # @return [String] nil if the prospective_filename is illegitimate in any way + def legitimize_config_filename!(prospective_filename) + return if ! File.exists?(prospective_filename) + begin - filename = File.realpath( filename_ ) + filename = File.realpath(prospective_filename) + return if @configs.any? { |c| c[:filename] == filename } + filename rescue Errno::ENOENT - s = "Configuration file #{filename_.inspect} was not found" + # We're rescuing here instead of just checking File.exists? because + s = "Configuration file #{prospective_filename.inspect} was not found" if including_filename s << " (referenced from #{including_filename.inspect}" end + # TODO: These @config_problems should probably not be floating around + # on their own like this. They should be on the ConfigFile class (see + # TODO comments below). @config_problems << s - return + + nil end - return if @configs.any? { |c| c[:filename] == filename } + end + + # @param [String] filename_ the config file to parse + # @param [String] including_filename the config file which calls include on this one + # @return an Array of problem descriptions (Strings) + def parse_configuration_file( filename_, including_filename = nil ) + filename = legitimize_config_filename!(filename_) + # TODO: Rewrite this nil check using Null Object pattern + return if filename.nil? + + # TODO: Turn this hash into its own class (ConfigFile?) + # and make changes accordingly below where filename is used. @configs << { filename: filename, source: including_filename } IO.readlines( filename ).each_with_index do |line,line_number|