Class: Nanoc::Core::ConfigLoader Private

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

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Defined Under Namespace

Classes: CyclicalConfigFileError, NoConfigFileFoundError, NoParentConfigFileFoundError

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.config_filename_for_cwdString

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:



31
32
33
34
35
# File 'lib/nanoc/core/config_loader.rb', line 31

def self.config_filename_for_cwd
  filenames = %w[nanoc.yaml config.yaml]
  candidate = filenames.find { |f| File.file?(f) }
  candidate && File.expand_path(candidate)
end

.cwd_is_nanoc_site?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)


26
27
28
# File 'lib/nanoc/core/config_loader.rb', line 26

def self.cwd_is_nanoc_site?
  !config_filename_for_cwd.nil?
end

Instance Method Details

#apply_parent_config(config, processed_paths = []) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/nanoc/core/config_loader.rb', line 61

def apply_parent_config(config, processed_paths = [])
  parent_path = config[:parent_config_file]
  return config if parent_path.nil?

  # Get absolute path
  parent_path = File.absolute_path(parent_path, File.dirname(processed_paths.last))
  unless File.file?(parent_path)
    raise NoParentConfigFileFoundError.new(parent_path)
  end

  # Check recursion
  if processed_paths.include?(parent_path)
    raise CyclicalConfigFileError.new(parent_path)
  end

  # Load
  parent_config = Nanoc::Core::Configuration.new(hash: load_file(parent_path), dir: config.dir)
  full_parent_config = apply_parent_config(parent_config, processed_paths + [parent_path])
  full_parent_config.merge(config.without(:parent_config_file))
end

#load_file(filename) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



56
57
58
# File 'lib/nanoc/core/config_loader.rb', line 56

def load_file(filename)
  YAML.load_file(filename)
end

#new_from_cwdObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/nanoc/core/config_loader.rb', line 37

def new_from_cwd
  # Determine path
  filename = self.class.config_filename_for_cwd
  raise NoConfigFileFoundError if filename.nil?

  # Read
  config =
    apply_parent_config(
      Nanoc::Core::Configuration.new(
        hash: load_file(filename),
        dir: File.dirname(filename),
      ),
      [filename],
    ).with_defaults

  # Load environment
  config.with_environment
end