class Bookwatch::Config::Fetcher

Attributes

config[R]
config_class[R]
config_dir_path[R]
config_file_path[R]
configuration_validator[R]
loader[R]

Public Class Methods

new(configuration_validator, loader, config_class) click to toggle source
# File lib/bookwatch/config/fetcher.rb, line 7
def initialize(configuration_validator, loader, config_class)
  @loader = loader
  @configuration_validator = configuration_validator
  @config_class = config_class
end

Public Instance Methods

fetch_config() click to toggle source
# File lib/bookwatch/config/fetcher.rb, line 13
def fetch_config
  @base_config ||= read_config_file
  @optional_configs ||= read_optional_configs

  @config ||= validate(@base_config, @optional_configs)
end
set_config_dir_path(config_dir_path) click to toggle source
# File lib/bookwatch/config/fetcher.rb, line 20
def set_config_dir_path(config_dir_path)
  @config_dir_path = File.expand_path(config_dir_path)
end
set_config_file_path(config_file_path) click to toggle source
# File lib/bookwatch/config/fetcher.rb, line 24
def set_config_file_path(config_file_path)
  @config_file_path = File.expand_path(config_file_path)
end

Private Instance Methods

read_config_file() click to toggle source
# File lib/bookwatch/config/fetcher.rb, line 33
def read_config_file
  loader.load(config_file_path)

rescue FileNotFoundError => e
  raise "The configuration file specified does not exist. Please create a config #{e} file at #{config_file_path} and try again."
rescue InvalidSyntaxError => e
  raise syntax_error(e)
end
read_optional_configs() click to toggle source
# File lib/bookwatch/config/fetcher.rb, line 42
def read_optional_configs
  Dir["#{config_dir_path}/*.yml"].map do |config_file|
    loader.load(File.expand_path(config_file)) || {}
  end.reduce({}, :merge)
rescue InvalidSyntaxError => e
  raise syntax_error(e)
end
syntax_error(e) click to toggle source
# File lib/bookwatch/config/fetcher.rb, line 59
def syntax_error(e)
  "There is a syntax error in your config file: \n #{e}"
end
validate(base_hash, optional_hash) click to toggle source
# File lib/bookwatch/config/fetcher.rb, line 50
def validate(base_hash, optional_hash)
  raise 'Your config.yml appears to be empty. Please check and try again.' unless base_hash

  config_class.parse(base_hash.merge(optional_hash)).tap do |config|
    errors = configuration_validator.exceptions(config)
    raise errors.first if errors.any?
  end
end