Class: AnnotateRb::ConfigLoader

Inherits:
Object
  • Object
show all
Defined in:
lib/annotate_rb/config_loader.rb

Class Method Summary collapse

Class Method Details

.load_configObject



10
11
12
13
14
15
16
17
18
# File 'lib/annotate_rb/config_loader.rb', line 10

def load_config
  config_path = ConfigFinder.find_project_dotfile

  if config_path
    load_yaml_configuration(config_path)
  else
    {}
  end
end

.load_yaml_configuration(absolute_path) ⇒ Object

Method from Rubocop::ConfigLoader

Raises:

  • (TypeError)


21
22
23
24
25
26
27
28
29
30
31
# File 'lib/annotate_rb/config_loader.rb', line 21

def load_yaml_configuration(absolute_path)
  file_contents = read_file(absolute_path)

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

  # TODO: Print config if debug flag/option is set

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

  hash
end

.read_file(absolute_path) ⇒ Object

Read the specified file, or exit with a friendly, concise message on stderr. Care is taken to use the standard OS exit code for a “file not found” error.

Method from Rubocop::ConfigLoader



38
39
40
41
42
# File 'lib/annotate_rb/config_loader.rb', line 38

def read_file(absolute_path)
  File.read(absolute_path, encoding: Encoding::UTF_8)
rescue Errno::ENOENT
  raise ConfigNotFoundError, "Configuration file not found: #{absolute_path}"
end

.yaml_safe_load(yaml_code, filename) ⇒ Object

Method from Rubocop::ConfigLoader



45
46
47
48
49
50
51
52
53
# File 'lib/annotate_rb/config_loader.rb', line 45

def yaml_safe_load(yaml_code, filename)
  yaml_safe_load!(yaml_code, filename)
rescue
  if defined?(::SafeYAML)
    raise "SafeYAML is unmaintained, no longer needed and should be removed"
  end

  raise
end

.yaml_safe_load!(yaml_code, filename) ⇒ Object

Method from Rubocop::ConfigLoader



56
57
58
59
60
# File 'lib/annotate_rb/config_loader.rb', line 56

def yaml_safe_load!(yaml_code, filename)
  YAML.safe_load(
    yaml_code, permitted_classes: [Regexp, Symbol], aliases: true, filename: filename, symbolize_names: true
  )
end