class RecommendMe::ConfigLoader
Attributes
explicit_config_file[RW]
Path to a config file requested by user, (e.g., via command line option). Can be nil
Public Class Methods
new(explicit_config_file, logger=nil)
click to toggle source
# File lib/recommend_me/config_loader.rb, line 20 def initialize(explicit_config_file, logger=nil) @explicit_config_file = explicit_config_file @recommend_config_dir = nil @config_location = nil @logger = logger || NullLogger.new end
Public Instance Methods
config_location()
click to toggle source
# File lib/recommend_me/config_loader.rb, line 31 def config_location @config_location ||= (explicit_config_file || locate_local_config) end
env()
click to toggle source
(Private API, public for test purposes)
# File lib/recommend_me/config_loader.rb, line 65 def env ENV end
load()
click to toggle source
# File lib/recommend_me/config_loader.rb, line 50 def load # Ignore it if there's no explicit_config_file and can't find one at a # default path. return false if config_location.nil? if explicit_config_file && !path_exists?(config_location) raise "Specified config file #{config_location} does not exist" end # Have to set Config.config_file b/c other config is derived from it. Config.config_file = config_location read_config(IO.read(config_location), config_location) end
no_config_found?()
click to toggle source
# File lib/recommend_me/config_loader.rb, line 27 def no_config_found? config_location.nil? end
path_exists?(path)
click to toggle source
(Private API, public for test purposes)
# File lib/recommend_me/config_loader.rb, line 70 def path_exists?(path) Pathname.new(path).expand_path.exist? end
recommend_config_dir()
click to toggle source
# File lib/recommend_me/config_loader.rb, line 35 def recommend_config_dir if @recommend_config_dir.nil? @recommend_config_dir = false full_path = working_directory.split(File::SEPARATOR) (full_path.length - 1).downto(0) do |i| candidate_directory = File.join(full_path[0..i] + [".recommend"]) if File.exist?(candidate_directory) && File.directory?(candidate_directory) @recommend_config_dir = candidate_directory break end end end @recommend_config_dir end
Private Instance Methods
have_config?(path)
click to toggle source
# File lib/recommend_me/config_loader.rb, line 76 def have_config?(path) if path_exists?(path) logger.info("Using config at #{path}") true else logger.debug("Config not found at #{path}, trying next option") false end end
highlight_config_error(file, line)
click to toggle source
# File lib/recommend_me/config_loader.rb, line 141 def highlight_config_error(file, line) config_file_lines = [] IO.readlines(file).each_with_index {|l, i| config_file_lines << "#{(i + 1).to_s.rjust(3)}: #{l.chomp}"} if line == 1 lines = config_file_lines[0..3] else lines = config_file_lines[Range.new(line - 2, line)] end "Relevant file content:\n" + lines.join("\n") + "\n" end
locate_local_config()
click to toggle source
# File lib/recommend_me/config_loader.rb, line 86 def locate_local_config candidate_configs = [] # Look for $RECOMMEND_ME_HOME/recommend.rb (allow multiple recommend config on same machine) if env['RECOMMEND_ME_HOME'] candidate_configs << File.join(env['RECOMMEND_ME_HOME'], 'recommend.rb') end # Look for $PWD/recommend.rb if Dir.pwd candidate_configs << File.join(Dir.pwd, 'recommend.rb') end # Look for $UPWARD/.recommend/recommend.rb if recommend_config_dir candidate_configs << File.join(recommend_config_dir, 'recommend.rb') end # Look for $HOME/.recommend/recommend.rb PathHelper.home('.recommend') do |dot_recommend_dir| candidate_configs << File.join(dot_recommend_dir, 'recommend.rb') end candidate_configs.find do | candidate_config | have_config?(candidate_config) end end
logger()
click to toggle source
# File lib/recommend_me/config_loader.rb, line 152 def logger @logger end
read_config(config_content, config_file_path)
click to toggle source
# File lib/recommend_me/config_loader.rb, line 115 def read_config(config_content, config_file_path) Config.from_string(config_content, config_file_path) rescue SignalException raise rescue SyntaxError => e message = "" message << "You have invalid ruby syntax in your config file #{config_file_path}\n\n" message << "#{e.class.name}: #{e.message}\n" if file_line = e.message[/#{Regexp.escape(config_file_path)}:[\d]+/] line = file_line[/:([\d]+)$/, 1].to_i message << highlight_config_error(config_file_path, line) end raise StandardError::ArgumentError, message rescue Exception => e message = "You have an error in your config file #{config_file_path}\n\n" message << "#{e.class.name}: #{e.message}\n" filtered_trace = e.backtrace.grep(/#{Regexp.escape(config_file_path)}/) filtered_trace.each {|bt_line| message << " " << bt_line << "\n" } if !filtered_trace.empty? line_nr = filtered_trace.first[/#{Regexp.escape(config_file_path)}:([\d]+)/, 1] message << highlight_config_error(config_file_path, line_nr.to_i) end raise StandardError::ArgumentError, message end
working_directory()
click to toggle source
# File lib/recommend_me/config_loader.rb, line 111 def working_directory Dir.pwd end