Class: HamlLint::ConfigurationLoader

Inherits:
Object
  • Object
show all
Defined in:
lib/haml_lint/configuration_loader.rb

Overview

Manages configuration file loading.

Constant Summary collapse

AUTO_GENERATED_FILE =
'.haml-lint_todo.yml'
DEFAULT_CONFIG_PATH =
File.join(HamlLint::HOME, 'config', 'default.yml').freeze
CONFIG_FILE_NAME =
'.haml-lint.yml'

Class Method Summary collapse

Class Method Details

.default_configurationObject

Loads the built-in default configuration.



38
39
40
# File 'lib/haml_lint/configuration_loader.rb', line 38

def default_configuration
  @default_configuration ||= load_from_file(DEFAULT_CONFIG_PATH)
end

.default_path_to_configObject

Path to the default config file, if it exists



31
32
33
34
35
# File 'lib/haml_lint/configuration_loader.rb', line 31

def default_path_to_config
  directory = File.expand_path(Dir.pwd)
  config_file = possible_config_files(directory).find(&:file?)
  config_file ? config_file.to_path : nil
end

.load_applicable_config(config_file = nil, options = {}) ⇒ HamlLint::Configuration

Load configuration file given the current working directory the application is running within.

Parameters:

  • config_file (String) (defaults to: nil)

    optional path to the config file to load

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :exclude_files (Array<String>)

    files that should not be loaded even if they’re requested via inherits_from

Returns:



21
22
23
24
25
26
27
28
# File 'lib/haml_lint/configuration_loader.rb', line 21

def load_applicable_config(config_file = nil, options = {})
  config_file ||= default_path_to_config
  if config_file
    load_file(config_file, options)
  else
    default_configuration
  end
end

.load_file(file, context = {}) ⇒ HamlLint::Configuration

Loads a configuration, ensuring it extends the default configuration.

Parameters:

  • file (String)
  • context (Hash) (defaults to: {})

Options Hash (context):

  • :loaded_files (Array<String>)

    any previously loaded files in an inheritance chain

  • :exclude_files (Array<String>)

    files that should not be loaded even if they’re requested via inherits_from

Returns:



51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/haml_lint/configuration_loader.rb', line 51

def load_file(file, context = {})
  context[:loaded_files] ||= []
  context[:exclude_files] ||= []
  config = load_from_file(file)

  [default_configuration, resolve_inheritance(config, context), config]
    .reduce { |acc, elem| acc.merge(elem) }
rescue Psych::SyntaxError, Errno::ENOENT => e
  raise HamlLint::Exceptions::ConfigurationError,
        "Unable to load configuration from '#{file}': #{e}",
        e.backtrace
end

.load_hash(hash) ⇒ HamlLint::Configuration

Creates a configuration from the specified hash, ensuring it extends the default configuration.

Parameters:

  • hash (Hash)

Returns:



69
70
71
72
73
# File 'lib/haml_lint/configuration_loader.rb', line 69

def load_hash(hash)
  config = HamlLint::Configuration.new(hash)

  default_configuration.merge(config)
end