class HamlLint::ConfigurationLoader

Manages configuration file loading.

Constants

AUTO_GENERATED_FILE
CONFIG_FILE_NAME
DEFAULT_CONFIG_PATH

Public Class Methods

default_configuration() click to toggle source

Loads the built-in default configuration.

# File lib/haml_lint/configuration_loader.rb, line 38
def default_configuration
  @default_configuration ||= load_from_file(DEFAULT_CONFIG_PATH)
end
default_path_to_config() click to toggle source

Path to the default config file, if it exists

# 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 = {}) click to toggle source

Load configuration file given the current working directory the application is running within. @param config_file [String] optional path to the config file to load @param options [Hash] @option options :exclude_files [Array<String>] files that should not

be loaded even if they're requested via inherits_from

@return [HamlLint::Configuration]

# 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 = {}) click to toggle source

Loads a configuration, ensuring it extends the default configuration.

@param file [String] @param context [Hash] @option context :loaded_files [Array<String>] any previously loaded

files in an inheritance chain

@option context :exclude_files [Array<String>] files that should not

be loaded even if they're requested via inherits_from

@return [HamlLint::Configuration]

# 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) click to toggle source

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

@param hash [Hash] @return [HamlLint::Configuration]

# File lib/haml_lint/configuration_loader.rb, line 69
def load_hash(hash)
  config = HamlLint::Configuration.new(hash)

  default_configuration.merge(config)
end

Private Class Methods

load_from_file(file) click to toggle source

Parses and loads a configuration from the given file.

@param file [String] @return [HamlLint::Configuration]

# File lib/haml_lint/configuration_loader.rb, line 81
def load_from_file(file)
  hash =
    if yaml = YAML.load_file(file)
      yaml.to_hash
    else
      {}
    end

  if hash.key?('inherit_from')
    hash['inherits_from'] ||= []
    hash['inherits_from'].concat(Array(hash.delete('inherit_from')))
  end

  HamlLint::Configuration.new(hash, file)
end
possible_config_files(directory) click to toggle source

Returns a list of possible configuration files given the context of the specified directory.

@param directory [String] @return [Array<Pathname>]

# File lib/haml_lint/configuration_loader.rb, line 102
def possible_config_files(directory)
  files = Pathname.new(directory)
                  .enum_for(:ascend)
                  .map { |path| path + CONFIG_FILE_NAME }
  files << Pathname.new(CONFIG_FILE_NAME)
end
resolve(file, context) click to toggle source

Resolves an inherited file and loads it.

@param file [String] the path to the file @param loaded_files [Array<String>] previously loaded files in the

inheritance chain

@return [HamlLint::Configuration, nil]

# File lib/haml_lint/configuration_loader.rb, line 115
def resolve(file, context)
  return unless File.exist?(file)
  return if context[:loaded_files].include?(file)
  return if context[:exclude_files].include?(file)

  context[:loaded_files] << file
  load_file(file, context)
end
resolve_inheritance(config, context) click to toggle source

Resolves the chain of `inherits_from` directives in a configuration.

@param config [HamlLint::Configuration] the pre-existing configuration @param loaded_files [Array<String>] any previously loaded files in an

inheritance chain

@return [HamlLint::Configuration]

# File lib/haml_lint/configuration_loader.rb, line 130
def resolve_inheritance(config, context)
  Array(config['inherits_from'])
    .map { |config_file| resolve(config_file, context) }
    .compact
    .reduce { |acc, elem| acc.merge(elem) } || config
end