module Sapience::ConfigLoader

This class represents the configuration of the RuboCop application and all its cops. A Config is associated with a YAML configuration file from which it was read. Several different Configs can be used during a run of the sapience program, if files in several directories are inspected.

Constants

DEFAULT_FILE
SAPIENCE_FILE
SAPIENCE_HOME

Public Class Methods

load_from_file() click to toggle source
# File lib/sapience/config_loader.rb, line 18
def self.load_from_file
  default_config     = load_yaml_configuration(config_file_path)
  application_config = load_yaml_configuration(application_config_file)
  merge_configs(default_config, application_config)
end
merge_configs(left_config = {}, right_config = {}) click to toggle source
# File lib/sapience/config_loader.rb, line 24
def self.merge_configs(left_config = {}, right_config = {})
  left_config.each do |key, config = {}|
    right = right_config.fetch(key) { {} }
    config.merge!(right)
  end

  (right_config.keys - left_config.keys).each do |left_over_key|
    left_config[left_over_key] = right_config[left_over_key]
  end
  left_config
end

Private Class Methods

application_config_file() click to toggle source
# File lib/sapience/config_loader.rb, line 43
def application_config_file
  File.join(root_dir, "config", SAPIENCE_FILE)
end
config_file_path() click to toggle source
# File lib/sapience/config_loader.rb, line 39
def config_file_path
  File.absolute_path(DEFAULT_FILE)
end
load_yaml_configuration(absolute_path) click to toggle source
# File lib/sapience/config_loader.rb, line 56
def load_yaml_configuration(absolute_path)
  return {} unless File.exist?(absolute_path)
  text      = IO.read(absolute_path, encoding: "UTF-8")
  erb       = ERB.new(text)
  yaml_code = erb.result

  hash = yaml_safe_load(yaml_code, absolute_path) || {}

  fail(TypeError, "Malformed configuration in #{absolute_path}") unless hash.is_a?(Hash)

  hash
end
root_dir() click to toggle source
# File lib/sapience/config_loader.rb, line 47
def root_dir
  @root_dir ||=
    if defined?(::Rack::Directory)
      Rack::Directory.new("").root
    else
      Dir.pwd
    end
end
yaml_safe_load(yaml_code, filename) click to toggle source
# File lib/sapience/config_loader.rb, line 69
def yaml_safe_load(yaml_code, filename)
  if YAML.respond_to?(:safe_load) # Ruby 2.1+
    if defined?(SafeYAML) && SafeYAML.respond_to?(:load)
      SafeYAML.load(yaml_code, filename,
        whitelisted_tags: %w(!ruby/regexp),
        aliases: true)
    else
      YAML.safe_load(yaml_code, [Regexp], [], true, filename)
    end
  else
    YAML.safe_load(yaml_code, filename, aliases: true)
  end
end