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



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

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)


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

def load_yaml_configuration(absolute_path)
  file_contents = read_file(absolute_path)
  yaml_code = ERB.new(file_contents).result

  hash = yaml_safe_load(yaml_code, 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



41
42
43
44
45
# File 'lib/annotate_rb/config_loader.rb', line 41

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



48
49
50
51
52
53
54
55
56
# File 'lib/annotate_rb/config_loader.rb', line 48

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



59
60
61
62
63
# File 'lib/annotate_rb/config_loader.rb', line 59

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