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.



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

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



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

def default_path_to_config
  directory = File.expand_path(Dir.pwd)
  config_file = possible_config_files(directory).find(&:file?)
  config_file&.to_path
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:



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

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:



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/haml_lint/configuration_loader.rb', line 52

def load_file(file, context = {}) # rubocop:disable Metrics
  context[:loaded_files] ||= []
  context[:loaded_files].map! { |config_file| File.expand_path(config_file) }
  context[:exclude_files] ||= []
  context[:exclude_files].map! { |config_file| File.expand_path(config_file) }
  config = load_from_file(File.expand_path(file))

  configs = if context[:loaded_files].any?
              [resolve_inheritance(config, context), config]
            else
              [default_configuration, resolve_inheritance(config, context), config]
            end

  configs.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:



77
78
79
80
81
# File 'lib/haml_lint/configuration_loader.rb', line 77

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

  default_configuration.merge(config)
end