Refactoring of config file reading

This commit is contained in:
kojix2 2023-01-05 11:08:38 +09:00
parent 1f41e79952
commit 83b1da9b96

View File

@ -30,14 +30,14 @@ module YouPlot
@params = Parameters.new @params = Parameters.new
if @config_file = find_config_file if find_config_file
ENV['MYYOUPLOTRC'] = @config_file read_config_file
@config = read_config_file(config_file) configure
configure(config)
end end
end end
def candidate_paths def config_file_candidate_paths
# keep the order of the paths
paths = [] paths = []
paths << ENV['MYYOUPLOTRC'] if ENV['MYYOUPLOTRC'] paths << ENV['MYYOUPLOTRC'] if ENV['MYYOUPLOTRC']
paths << '.youplot.yml' paths << '.youplot.yml'
@ -48,23 +48,23 @@ module YouPlot
end end
def find_config_file def find_config_file
config_file_path = nil config_file_candidate_paths.each do |file|
candidate_paths.each do |file|
path = File.expand_path(file) path = File.expand_path(file)
if File.exist?(path) if File.exist?(path)
config_file_path = path @config_file = path
break ENV['MYYOUPLOTRC'] = path
return @config_file
end end
end end
config_file_path nil
end end
def read_config_file(path) def read_config_file
require 'yaml' require 'yaml'
YAML.load_file(path) @config = YAML.load_file(config_file)
end end
def configure(config) def configure
option_members = @options.members option_members = @options.members
param_members = @params.members param_members = @params.members
# It would be more useful to be able to configure by plot type # It would be more useful to be able to configure by plot type