class Nanoc::Core::Configuration

Represents the site configuration.

Constants

DEFAULT_CONFIG

The default configuration for a site. A site's configuration overrides these options: when a {Nanoc::Core::Site} is created with a configuration that lacks some options, the default value will be taken from `DEFAULT_CONFIG`.

DEFAULT_DATA_SOURCE_CONFIG

The default configuration for a data source. A data source's configuration overrides these options.

ENVIRONMENTS_CONFIG_KEY

Configuration environments property key

NANOC_ENV
NANOC_ENV_DEFAULT

Attributes

dir[R]
env_name[R]

@return [String, nil] The active environment for the configuration

Public Class Methods

new(hash: {}, dir:, env_name: nil) click to toggle source
# File lib/nanoc/core/configuration.rb, line 48
def initialize(hash: {}, dir:, env_name: nil)
  @env_name = env_name
  @wrapped = hash.__nanoc_symbolize_keys_recursively
  @dir = dir

  validate
end

Public Instance Methods

[](key) click to toggle source
# File lib/nanoc/core/configuration.rb, line 95
def [](key)
  @wrapped[key]
end
[]=(key, value) click to toggle source
# File lib/nanoc/core/configuration.rb, line 118
def []=(key, value)
  @wrapped[key] = value
end
action_provider() click to toggle source
# File lib/nanoc/core/configuration.rb, line 157
def action_provider
  self[:action_provider].to_sym
end
attributes() click to toggle source
# File lib/nanoc/core/configuration.rb, line 85
def attributes
  to_h
end
dig(*keys) click to toggle source
# File lib/nanoc/core/configuration.rb, line 100
def dig(*keys)
  @wrapped.dig(*keys)
end
each() { |k, v| ... } click to toggle source
# File lib/nanoc/core/configuration.rb, line 139
def each
  @wrapped.each { |k, v| yield(k, v) }
  self
end
fetch(key, fallback = Nanoc::Core::UNDEFINED) { |key| ... } click to toggle source
# File lib/nanoc/core/configuration.rb, line 105
def fetch(key, fallback = Nanoc::Core::UNDEFINED, &_block)
  @wrapped.fetch(key) do
    if !Nanoc::Core::UNDEFINED.equal?(fallback)
      fallback
    elsif block_given?
      yield(key)
    else
      raise KeyError, "key not found: #{key.inspect}"
    end
  end
end
freeze() click to toggle source
Calls superclass method
# File lib/nanoc/core/configuration.rb, line 145
def freeze
  super
  @wrapped.__nanoc_freeze_recursively
  self
end
inspect() click to toggle source
# File lib/nanoc/core/configuration.rb, line 175
def inspect
  "<#{self.class}>"
end
key?(key) click to toggle source
# File lib/nanoc/core/configuration.rb, line 90
def key?(key)
  @wrapped.key?(key)
end
merge(hash) click to toggle source
# File lib/nanoc/core/configuration.rb, line 123
def merge(hash)
  self.class.new(hash: merge_recursively(@wrapped, hash.to_h), dir: @dir, env_name: @env_name)
end
output_dir() click to toggle source
# File lib/nanoc/core/configuration.rb, line 152
def output_dir
  make_absolute(self[:output_dir]).freeze
end
output_dirs() click to toggle source
# File lib/nanoc/core/configuration.rb, line 162
def output_dirs
  envs = @wrapped.fetch(ENVIRONMENTS_CONFIG_KEY, {})
  res = [output_dir] + envs.values.map { |v| make_absolute(v[:output_dir]) }
  res.uniq.compact
end
reference() click to toggle source

Returns an object that can be used for uniquely identifying objects.

@return [Object] An unique reference to this object

# File lib/nanoc/core/configuration.rb, line 171
def reference
  'configuration'
end
to_h() click to toggle source
# File lib/nanoc/core/configuration.rb, line 79
def to_h
  @wrapped
end
update(hash) click to toggle source
# File lib/nanoc/core/configuration.rb, line 133
def update(hash)
  @wrapped.update(hash)
  self
end
with_defaults() click to toggle source
# File lib/nanoc/core/configuration.rb, line 57
def with_defaults
  new_wrapped = DEFAULT_CONFIG.merge(@wrapped)
  new_wrapped[:data_sources] = new_wrapped[:data_sources].map do |ds|
    DEFAULT_DATA_SOURCE_CONFIG.merge(ds)
  end

  self.class.new(hash: new_wrapped, dir: @dir, env_name: @env_name)
end
with_environment() click to toggle source
# File lib/nanoc/core/configuration.rb, line 66
def with_environment
  return self unless @wrapped.key?(ENVIRONMENTS_CONFIG_KEY)

  # Set active environment
  env_name = @env_name || ENV.fetch(NANOC_ENV, NANOC_ENV_DEFAULT)

  # Load given environment configuration
  env_config = @wrapped[ENVIRONMENTS_CONFIG_KEY].fetch(env_name.to_sym, {})

  self.class.new(hash: @wrapped, dir: @dir, env_name: env_name).merge(env_config)
end
without(key) click to toggle source
# File lib/nanoc/core/configuration.rb, line 128
def without(key)
  self.class.new(hash: @wrapped.reject { |k, _v| k == key }, dir: @dir, env_name: @env_name)
end

Private Instance Methods

make_absolute(path) click to toggle source
# File lib/nanoc/core/configuration.rb, line 181
def make_absolute(path)
  path && @dir && File.absolute_path(path, @dir).encode('UTF-8')
end
merge_recursively(config1, config2) click to toggle source
# File lib/nanoc/core/configuration.rb, line 185
def merge_recursively(config1, config2)
  config1.merge(config2) do |_, value1, value2|
    if value1.is_a?(Hash) && value2.is_a?(Hash)
      merge_recursively(value1, value2)
    else
      value2
    end
  end
end
validate() click to toggle source
# File lib/nanoc/core/configuration.rb, line 195
def validate
  dir = File.dirname(__FILE__)
  schema_data = JSON.parse(File.read(dir + '/configuration-schema.json'))
  schema = JsonSchema.parse!(schema_data)
  schema.expand_references!
  schema.validate!(@wrapped.__nanoc_stringify_keys_recursively)
end